Skip to content

Pass context along to every part of the pipeline#403

Merged
gjtorikian merged 6 commits into
mainfrom
pass-context-along-fully
Apr 30, 2024
Merged

Pass context along to every part of the pipeline#403
gjtorikian merged 6 commits into
mainfrom
pass-context-along-fully

Conversation

@gjtorikian

@gjtorikian gjtorikian commented Apr 26, 2024

Copy link
Copy Markdown
Owner

This PR allows for more correct abstractions around contexts for the pipeline. Context for each filter can now be passed when the filter is instantiated, or, when the pipeline is called. This allows for the same filters and pipelines to execute in different ways, depending on the context at runtime.

Closes #402

@gjtorikian gjtorikian force-pushed the pass-context-along-fully branch from 84ac3d0 to 6f51e52 Compare April 26, 2024 14:29

@nekketsuuu nekketsuuu left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this patch! This works out for my use case.

Comment thread lib/html_pipeline.rb

unless @node_filters.empty?
instrument("call_node_filters.html_pipeline", payload) do
@node_filters.each { |filter| filter.context = (filter.context || {}).merge(context) }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO: This line might not align with the text_filters and convert_filter, as they pass the new context directly without merging. However this can be a non-problem since it's up to each text_filter and convert_filter to decide whether to overwrite the default context with the passed context or to merge the passed one to the default one.

Comment thread test/test_helper.rb Outdated
class YehBolderFilter < HTMLPipeline::TextFilter
def call(input, context: {}, result: {})
input.gsub("yeH", "**yeH**")
input.gsub("yeH", "**yeH**") unless context[:no_bolding] == false

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits

  • I think it's more natural if yeH is converted only if context[:no_bolding] is false.
  • unless XXX == false is difficult to read.
  • We can safely remove == true or == false, and nil should be treated as falsy.
Suggested change
input.gsub("yeH", "**yeH**") unless context[:no_bolding] == false
input.gsub("yeH", "**yeH**") unless context[:no_bolding]

Test cases are also affected.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you’re quite right. I was hurriedly putting together a test here!

@gjtorikian gjtorikian changed the title Pass context along fully Pass context along to every part of the pipeline Apr 30, 2024
@gjtorikian gjtorikian merged commit d4a5269 into main Apr 30, 2024
@gjtorikian gjtorikian deleted the pass-context-along-fully branch April 30, 2024 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context & result of filters except for text_filters aren't overwritten on call time

2 participants