-
-
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
Normalize formatting of sample theme Jinja templates #2379
base: master
Are you sure you want to change the base?
Conversation
If this PR isn't isomorphic, I will eat my own hat (metaphorically). 🎩 |
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.
Not my favorite type of review 😕 . I briefly look over it and looks OK. I guess...
269f4db
to
c3286d6
Compare
Hi Allen. Many thanks for your formatting improvements to the Jinja templates in the My apologies for the extra work. My goal is to release Pelican 4.0 within the next 24 hours, so if you could update your PR within that time frame, we should be able to include it in the release. 😅 |
bdfb910
to
f6f5087
Compare
I used the tool [htmlbeautify][1] like so htmlbeautifier -t 4 -b 1 pelican/themes/simple/templates/*.html htmlbeautifier -t 4 -b 1 pelican/themes/notmyidea/templates/*.html and then manually adjusted the output. The final result follows the following rules. - 4-space indentation - Multiline nested tags like <tag1><tag2> ... nested content ... </tag2></tag1> are broken up into <tag1> <tag2> ... nested content ... </tag2> </tag1> but if everything is on one like this <tag1><tag2>... nested content ...</tag2></tag1> it's OK. - Treat new blocks like tags when indenting. E.g. {% if cond %} <p>hello</p> {% endif %} However, observe the following exceptions: - Don't indent inside a {% block %}, {% macro %}, or if the block takes up the entire file. - If a {% for ... %} block takes up the entire content of an HTML tag, don't indent it. E.g. <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul> I tried to follow the examples in the official Jinja documentation as closely as possible: <http://jinja.pocoo.org/docs/2.10/templates/>. Used `git diff --check` to perform basic whitespace checks. [1]: [https://github.com/threedaymonk/htmlbeautifier]
f6f5087
to
59750b3
Compare
Hi @justinmayer, sorry for the delay; I've rebased everything as requested. Additionally, I also took a closer look at the official Jinja template documentation and realized there were some indentation rules I didn't see before, so I've fixed this. I put a complete list of indentation rules in the commit message of 59750b3. |
<h1><a href="/">A Pelican Blog </a></h1> | ||
<nav> | ||
<ul> | ||
<li><a href="/tag/oh.html">Oh Oh Oh</a></li> |
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.
<li><a href="/tag/oh.html">Oh Oh Oh</a></li> | |
<li><a href="/tag/oh.html">Oh Oh Oh</a></li> |
If we're going for an OCD-correct indentation, this list should go four spaces back I think ;)
|
||
</footer><!-- /.post-info --> <p>You're mutually oblivious.</p> | ||
</footer><!-- /.post-info --> <p>You're mutually oblivious.</p> |
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.
Maybe a newline should go here as well? Not sure about the indent tho.
</footer><!-- /.post-info --> <p>You're mutually oblivious.</p> | |
</footer><!-- /.post-info --> | |
<p>You're mutually oblivious.</p> |
(Disclaimer: I'm an OCD freak when it comes to formatting. Take with a grain of salt 😆 ) In my themes I'm not indenting the The indentation mostly makes sense, except for the two cases I mentioned above. The generated output still has quite a lot of (what seem to be rather random) empty lines, not sure what to do about that. Maybe remove them all? Some things coming from My thinking is that if we're going to do such a huge change in the formatting, we might as well finish it to the extreme to avoid doing similar work again in the future. (Because this always results in many coflicts for everyone who's modifying the themes as well.) It also makes testing easier, since with clean markup it's clearer to see what a particular change did to the theme. |
Hi @mosra, thanks for your comments! It makes me feel like someone cares about all the indentation work I had to do, haha. First, it's important to note that we basically can't edit the generated output directly; it has to exactly match what is generated, because there are some tests that assert this (there's a little more info here). So sadly, I can't apply any of your suggested changes to Second, it would be nice to do, but I have pretty much given up trying to make the HTML output look pretty; last time I looked at it, it seems like Jinja doesn't have a concept of indenting content substituted in from blocks and includes. For example, {# template_parent.html #}
<tag1>
<tag2>
<tag3>
<p>From parent</p>
{% block content %}
{% endblock %}
</tag3>
</tag2>
</tag1> {# template_child.html #}
{% extends template_parent.html %}
{% block content %}
Hi everyone!
{% endblock %} <!-- Output -->
<tag1>
<tag2>
<tag3>
<p>From parent</p>
Hi everyone!
</tag3>
</tag2>
</tag1> Note that the child template is basically just substituted in line-wise, with no awareness of indentation. And of course, it would be a Bad Thing to couple child templates to how much indentation is in their parents. Hopefully, people will be looking at the templates that generated the output much more often rather than the output itself. But if there were some sort of setting that adds the same indentation that the template was on into the file, that might be really nice. We could also run it through a prettifier/minifier, but that is only something the assets plugin can do, and not native Pelican. I believe changing indentation from 4 spaces to 2 spaces would actually be fairly simple; it'll just a |
Of course ;) To make myself clear, I was looking at the output to see what's wrong in the templates (and so all my suggestions are meant to be "this output looks wrong and the source template should be fixed").
No, please not, that's a one-way ticket to hell. I know too many projects that started applying minifiers to their test results instead of fixing the root causes and it went only downhill from there.
This can be For the blocks I'm afraid I don't have a solution, I was always explicitly indenting the content of <tag1>
<tag2>
<tag3>
<p>From parent</p>
<!-- content -->
{% block content %}
{% endblock %}
<!-- /content -->
</tag3>
</tag2>
</tag1> For includes (which can happen at multiple places and so the indentation is not always clear), I have a workaround -- been using this myself on a few places: <footer class="m-container">
<div class="m-row">
<div class="m-col-m-10 m-push-m-1 m-nopadb">
{% macro footer() %}{% include "article_footer.html" %}{% endmacro %}
{{ footer()|indent(10) }}
… |
For this kind of heavy formatting, I propose to automate that task to lower the burden of maintenance. Here is for example how I enforce Jinja and HTML format on my Plumage theme for Pelican: https://github.com/kdeldycke/plumage/blob/1bbf9f9c4dbfea3eea2dc151192ee1f31c6de686/.github/workflows/lint.yaml#L11-L25 . It is based on the fantastic |
Description
This PR normalizes the formatting of the Jinja templates in the
simple
andnotmyidea
themes, making them easier to read and indentation rules clearer for future contributors. The diff is huge, but the changes are entirely mechanical/whitespace-related. As far as I'm aware, only the following changes are applied.4-space indentation (there was a small amount of 2-space indentation). No "exceptions" like content inside
<body>
or<section>
.Conform to the conventions that are used in the examples in the official Jinja template documentation. I have listed the rules as I understand them in the commit message of 59750b3.
Multiline nested tags like
are broken up into
but nested tags on one line like
are untouched/allowed.
Other stuff like CSS is untouched.
To maintain your sanity, I would recommend focusing on just the templates in the themes themselves (grouped together at the bottom of the diffs and also separated into one commit for each theme) and not the html output, then doing a pass with
git diff -w
or checking "Hide whitepsace changes" on the Github diff to ignore whitespace changes (which will only show changes like rule 3).Note that this doesn't make the output html pretty at all due to the fact that nested Jinja templates are not aware of indentation level at all.
Testing
I tested this by generating the output for both the simple and notmyidea themes and running the diff tests, then also inspecting the output in a browser to make sure everything looks the same.
Implementation note
Unfortunately, there doesn't seem to be a good standalone Jinja template formatter (the most recommended option I found when searching was atom-beautify, which is part of Atom, of course). I used the tool htmlbeautifier, but had to apply a lot of Jinja-specific rules manually on top.