Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass context along to every part of the pipeline #403

Merged
merged 6 commits into from
Apr 30, 2024

Conversation

gjtorikian
Copy link
Owner

@gjtorikian gjtorikian commented Apr 26, 2024

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
Copy link

@nekketsuuu nekketsuuu left a comment

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.

end
end

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

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.

@@ -25,6 +25,6 @@ def call(input, context: {}, result: {})
# bolds any instance of the word yeH
class YehBolderFilter < HTMLPipeline::TextFilter
def call(input, context: {}, result: {})
input.gsub("yeH", "**yeH**")
input.gsub("yeH", "**yeH**") unless context[:no_bolding] == false

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
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
3 checks passed
@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