Skip to content

Filter _Template content. Resolves #1603 and #1640. #1612

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

Merged
merged 7 commits into from
Dec 26, 2020

Conversation

bartkamphorst
Copy link
Member

This is a proposed solution to the question in #1603 . Basically, we just allow users to create a Gollum::TemplateFilter in their config.rb and let them manipulate the contents of _Template however they want at runtime. What do you think about this approach @dometto? A very simple example of such a filter could be the following:

module Gollum
  class TemplateFilter
    def self.filter(data)
      return data.gsub!('{{current_date}}', Time.now.strftime("%d/%m/%Y"))
    end
  end
end

(Where _Template.md would obviously need to include something like Date: {{current_date}}.)

@bartkamphorst bartkamphorst requested a review from dometto August 25, 2020 15:00
@dometto
Copy link
Member

dometto commented Aug 25, 2020

I like the way this works on the user side, and the customizability! See comment.

@bartkamphorst
Copy link
Member Author

I processed your comments and used your API proposal for the new version. Filters can now be added in config.rb like so:

Gollum::TemplateFilter.add_filter('{{current_date}}') do
  Time.now.strftime("%d/%m/%Y")
end

Gollum::TemplateFilter.add_filter('{{current_time}}') do
  Time.now.strftime("%T")
end

@dometto
Copy link
Member

dometto commented Dec 11, 2020

Very cool! Looks super elegant now.

@bartkamphorst
Copy link
Member Author

This latest commit adds support for relative templates (e.g., subdir/_Template.md), with fallback to /_Template.{ext}. Relates to #1640 . @dometto Please let me know if you think this behavior is desirable.

@dometto
Copy link
Member

dometto commented Dec 20, 2020

I think this is a nifty feature, yes, and the implementation is nice and clean -- so by all means!

@bartkamphorst bartkamphorst changed the title [WIP] Filter _Template content. Proposed solution to #1603. Filter _Template content. Resolves #1603 and #1640. Dec 26, 2020
@bartkamphorst bartkamphorst merged commit 6c07967 into master Dec 26, 2020
@bartkamphorst bartkamphorst deleted the templatefilter branch December 26, 2020 13:10
@tevino
Copy link
Contributor

tevino commented Dec 26, 2020

Can't wait for the release!

@tevino
Copy link
Contributor

tevino commented Feb 8, 2022

Found no document about this nifty feature, I just documented it with an example here:

https://github.com/gollum/gollum/wiki/Home/_compare/257834c879687ac819a0993019acdedca60c0e48

@bartkamphorst @dometto PTAL as I barely understand the difference between a proc and a block, the example works, but I'm not sure if it's the best practice.

Thanks for the work!

@tevino
Copy link
Contributor

tevino commented Feb 20, 2022

@bartkamphorst This may sound straightforward to you: is it possible to access @page.name within a template filter?

@@ -639,6 +636,11 @@ def show_file(file)
end
end

def load_template(path)
template_page = wiki_page(::File.join(path, '_Template')).page || wiki_page('/_Template').page
template_page ? Gollum::TemplateFilter.apply_filters(template_page.raw_data) : nil
Copy link
Contributor

Choose a reason for hiding this comment

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

@bartkamphorst @dometto How about passing path to apply_filters so that it could be accessed in the replacement?

This makes a filter like {{page_name}} possible.

Copy link
Member

Choose a reason for hiding this comment

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

This makes sense to me, if you're wiling to open a PR for this @tevino! What do you think @bartkamphorst?

Copy link
Contributor

Choose a reason for hiding this comment

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

@dometto I just submitted a PR #1818, please help to review it, thanks!

@dometto
Copy link
Member

dometto commented Mar 23, 2022

Thanks for adding the documentation @tevino!

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.

3 participants