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

Possible to append to default filter chain? #407

Open
beporter opened this issue Jan 8, 2022 · 5 comments
Open

Possible to append to default filter chain? #407

beporter opened this issue Jan 8, 2022 · 5 comments

Comments

@beporter
Copy link

beporter commented Jan 8, 2022

I've been digging around in the source code, looking for a way to append (or prepend) an additional filter to the default list.

[:YAML, :BibTeX, :PlainText, :CriticMarkup, :TOC, :RemoteCode, :Code, :Macro, :Emoji, :Sanitize, :PlantUML, :Tags, :PandocBib, :Render]

But as far as I can tell, the only choice is to copy (and hardcode) the default list with my own filter added to it.

I suspect even converting the list to a constant would make this much more robust. Imagine this wiki_options:

wiki_options = {
  # ...
  filter_chain: Gollum::Wiki.DEFAULT_FILTERS + [ My::CustomFilter ]
}

Would a PR for this be welcome? Do you have any modifications to the suggested code? Is there an alternative I'm not thinking of?

(Another option might be to expose methods like def prepend_filter(filter) on the Wiki class to manage the filter chain. This seems less than ideal though. An instance of the Wiki class isn't intended to be "manipulated" on a meta level but rather "used".)

@beporter
Copy link
Author

beporter commented Jan 8, 2022

Looked at the code more and realized you currently have to define your custom filters in the Gollum::Filter:: namespace:

Gollum::Filter.const_get(filter_sym).new(self)

It might also be nice to allow implementors to stay out of the Gollum namespace like in my example above?

@dometto
Copy link
Member

dometto commented Jan 11, 2022

It might also be nice to allow implementors to stay out of the Gollum namespace like in my example above?

I personally think it's neat to have all filters be part of the same namespace, but if the consensus is this is very counter intuitive I am happy to yield. :) Substantively it would only require modifying the line you quote, I think. Something like:

filter_sym.is_a?(Class) ? fiter_sym.new(self) :  Gollum::Filter.const_get(filter_sym).new(self) 

But my personal feeling is this complicates the code for a minimal gain in enduser-friendliness. Perhaps it's more important that we document clearly how to define a custom filter?

@dometto
Copy link
Member

dometto commented May 10, 2022

Sorry @beporter, I just realized I never responded to this more verbosely. I do really like the example snippet you give and we would be open to considering a PR that makes something like that possible! If I see things correctly, that functionality would be independent of the namespace issue, no?

Thanks for chipping in regardless!

@beporter
Copy link
Author

beporter commented May 11, 2022

No worries on timing!

And yes I believe it would remove the current namespacing requirement. EDIT: I may have interpreted this wrong. JUST making the default list a constant would NOT remove the namespace requirement without the additional snippet you suggested. I'm happy to compromise to at least enable the prepend/append functionality.

Quite similarly to you: I'm quite happy to do a PR, but no promises on timing. The nature of open source work is that sometimes it's months between good opportunities to meaningfully contribute. I have a gollum search plugin still sitting on the back burner too. 🤦‍♂️

@dometto
Copy link
Member

dometto commented May 12, 2022

JUST making the default list a constant would NOT remove the namespace requirement without the additional snippet you suggested. I'm happy to compromise to at least enable the prepend/append functionality.

Right, so there seem to be two options:

  1. Add the snippet and be able to do Gollum::Wiki::DEFAULT_FILTERS + [ My::CustomFilter ]
  2. Don't add the snippet and only be able to do Gollum::Wiki::DEFAULT_FILTERS + :MyCustomFilter, where MyCustomFilter is under the Gollum::Filter namespace

I'm alright with either approach, 1 means more flexibility for the end user, whereas 2 is less complex in our codebase. My own initial preference was for one, but I'm happy to yield.

By the way, since the order of execution for the filters is important, I guess a typical usage would be something like:

Gollum::Wiki::DEFAULT_FILTERS.insert(Gollum::Wiki::DEFAULT_FILTERS.rindex(:Code), My::CustomFilter)

Maybe we could even come up with a more user friendly solution for that.

And: absolutely understand about the timing! Thanks for taking the time to contribute.

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

No branches or pull requests

2 participants