-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Introduce Jinja2 filters for converting links in additional fields #2164
Conversation
9264711
to
8343912
Compare
I'm very wary of {{ article|expand_link('cover') }} But I have to note that, this has the caveat of having to modify templates, whereas yours do not. |
I did this to be consistent with the I like your proposal -- I'll look into how Jinja could be extended and modify the patch accordingly.
Actually, this would make it nicely self-contained in the template, as one doesn't need to put any additional stuff into the settings. Now, what about doing something similar for |
7ddb811
to
1e27cfa
Compare
I reworked the code based on your proposal, liking it much better now. The original commits are still there for archival purposes, I can remove them after. |
1e27cfa
to
c506578
Compare
Any follow-up comments from @getpelican/reviewers ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to have a some tests for these filters. You should be able to copy the format of the tests in TestDateFormatter
, which basically just creates a dummy template containing the filter, calls the generator, and checks the output directly.
I might be missing something obvious, but what's the difference between |
50bb2b7
to
acb4290
Compare
I moved the logic into classes in The difference is the following and I don't think these two filters could be consolidated:
The tests are unfortunately not just about creating a dummy template (and that's why I didn't do them, as I mentioned in the PR description) -- the filter is accessing internals of a particular |
Ah, thank you for the clarification, and much appreciate the refactoring as well. Regarding the tests, perhaps some other @getpelican/reviewers have some ideas on what to do here. |
@mosra All you need is a custom jinja |
03098d7
to
f9554f9
Compare
Until now, the link replacing worked only on article and page contents or summaries. With this patch, if one needes to replace links in custom fields, there are two new Jinja2 filters that can do that. For fields that are referenced in the `FORMATTED_FIELDS` setting, one can use the `expand_links` Jinja2 filter in the template, passing the field name as a parameter: {{ article|expand_links('legal') }} If the custom field consists of just one link (for example a link to article cover image for a social meta tag), one can use the `expand_link` Jinja2 filter: {{ article|expand_link('cover') }} With the above being in a template and `FORMATTED_FIELDS` setting containing the `'legal'` field, a RST article making use of both fields could look like this: An article ########## 📅 2017-06-22 :legal: This article is released under `CC0 {filename}/license.rst`. :cover: {filename}/img/article-cover.jpg
f9554f9
to
b4a0994
Compare
Great, thank you, that was actually quite easy to do. Tests are done now. (Sorry for the delay, was on vacation the past weeks.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for making these changes (and to @avaris for the assistance)!
I momentarily had the thought that the filters would be more intuitive if the usage looked something like |
Is there anything left on my side preventing this to be merged? :) |
i can have a look at it over the weekend earliest. if someone else can do it earlier please do so @getpelican/reviewers |
would it make sense to obsolete how we access metadata right now to
This would remove the need for filters or themes implementing them. |
@ingwinlu This would mean there would need to be additional settings next to Personally, I am more in favor of the theme implementing a filter for given field than requiring the user to manually add such fields to |
if you do have the same functionality in two different places (auto expansion of content and expansion of metadata) with different parts having responsibility (one time pelican-dore, one time theme) of handling the replacement i would hardly ever call that 'self-contained'. my point it is probably a way better solution and less tangled if we could have one responsible party and one place where expansion happens. either everything in themes, or everything in pelican-core. The way todo both of those things is either:
|
True :) I meant more like self-contained from user PoV -- "I install a theme and it just works, yay!".
Yup, this is probably a no-go if we want to preserve backwards compatibility.
This would be nice. How would the dictionary approach look, then? I see one potentially problematic thing here, by having the expansion implicit for all fields, how one makes sure that it doesn't do unwanted things? For example:
|
Ookay. I see that this cuts deeper into the design than I originally thought, so I came up with a stripped-down idea that could be integrated and released in 3.8 without much hassle (and without providing any new user-visible functionality). In short -- those Jinja2 filters can be easily implemented as a plugin that doesn't affect Pelican core at all, it just needs to have access to the So what about this: I strip down the PR to just extracting the Would this be better for now? EDIT: Well, I just got a need for a third filter which takes an arbitrary string and expands it via the |
This depends on part of getpelican/pelican#2164 (in particular having the _link_expander() function), everything else from the PR is implemented here.
Any chance of having at least the "link replacer" function made public (and discarding the rest of this PR)? I have a few plugins/patches that need to call it from outside. Thank you. |
Reworked to reflect the review comment, see below for the original proposal.
Until now, the link replacing worked only on article and page contents or summaries. With this patch, if one needes to replace links in custom fields, there are two new Jinja2 filters that can do that. For fields that are referenced in the
FORMATTED_FIELDS
setting, one can use theexpand_links
Jinja2 filter in the template, passing the field name as a parameter:If the custom field consists of just one link (for example a link to article cover image for a social meta tag), one can use the
expand_link
Jinja2 filter:With the above being in a template and
FORMATTED_FIELDS
setting containing the'legal'
field, a RST article making use of both fields could look like this:Disclaimer: I'm a Python noob and pretty new to Pelican as well, so it's possible I did something terribly wrong here. I am happy to incorporate changes based on your reviews. Things I'm not sure about:
FORMATTED_FIELDS
as well -- however I was not able to find any test covering this setting.Fixes #1169 (which is closed now).
Introduce INTERNAL_LINK_FIELDS setting (obsoleted by the above)
Metadata fields with links to internal content listed in this array will be properly converted to absolute URLs the same way as they do in page/article content and summary. This allows themes to specify custom metadata fields to be used for linking to internal content such as social media tags, page background etc. For example, a theme can define this for an article page:
And then a reST article can include an image as an additional metadata field:
With the following in the settings:
The generated article page will have this: