-
Notifications
You must be signed in to change notification settings - Fork 12.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
Use Tera templates for rustdoc. #86157
Conversation
Some changes occurred in HTML/CSS/JS. |
(rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
@@ -1482,6 +1482,17 @@ dependencies = [ | |||
"regex", | |||
] | |||
|
|||
[[package]] | |||
name = "globwalk" |
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.
Oh oof I forgot tera uses globwalk :( it's extremely slow: rust-lang/docs.rs@9c9647d
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.
We don't actually use globwalk
in this PR, we are manually loading files the same way the linked docs.rs PR does.
For now, I don't see much advantages to this change, but at least it's a good start. What about the size of generated files and performance comparison? |
@GuillaumeGomez the advantage is it's much easier to read than repeated write calls |
In this case not really since only the big one has been moved. |
It's true this is mainly an initial setup to make subsequent templatizations possible, and to give a feel for what the template language will feel like. And the one format call here is the most template-like thing we currently have, since it already has named fields for interpolation. But even in this initial change you can see some of the benefits in clarity and readability. Consider: Old:
New:
That's 20 lines vs 11 lines, and the 11 line version also removes some redundancy. Also, the HTML in the template version is easier to read without backslashes in front of all the quotes. Performance: The difference so far will be quite negligible, because the amount of code moved in this PR is small. As we discussed in #84419, we may need to take a bit of a leap of faith performance-wise since the overall performance of templatizing everything won't be clear until we're much further along. Byte size: struct.String.html is 54,713 bytes gzipped, vs 54,594 on master. Note that this branch doesn't have special treatment for removing excess whitespace. I used |
Yes please. I'm honestly very afraid of the whole "remove whitespaces from HTML". In this case, we even need to re-parse it. Even though I really like the whole templating idea, the downside is really worrying me. But I may be completely wrong. So I'd prefer to have the whitespaces removed from HTML before merging. |
@GuillaumeGomez are you worried about reparsing? or about the change in file size? If you're just worried about reparsing, I don't see why this has to be blocked on removing the whitespace. |
Alright, with the latest updated, No parsing required! The trick was to add This is still not completely optimal, since it incentivizes putting a whole HTML tag on a single line, even when you might prefer to split out attributes on their own line. If you split out attributes on their own line, you're forced to choose between putting One thing that might be cool longer term is to see about getting Django's spaceless feature implemented in Tera (though I think we'd also want a special mode that's aware of HTML templates and collapses N whitespaces into 1 space character). |
That would be amazing! Can you open an issue on tera? |
Replaces a format!() call in layout::render with a template expansion. Introduces a `templates` field in SharedContext so parts of rustdoc can share pre-rendered templates. This currently builds in a copy of the single template available, like with static files. However, future work can make this live-loadable with a perma-unstable flag, to make rustdoc developers' work easier.
Done: Keats/tera#641 I also figured out how to do single whitespace for attributes: |
<meta name="keywords" content="{{page.keywords}}"> {#- -#} | ||
<title>{{page.title}}</title> {#- -#} | ||
<link rel="stylesheet" type="text/css" {# -#} | ||
href="{{static_root_path | safe}}normalize{{page.resource_suffix}}.css"> {#- -#} |
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.
This is weird to have two different syntaxes depending if we're in a tag or not. Oh well...
Well, the final result is really not great if we have to add Let's just hope the "whitespace cleaner" feature will be implemented shortly in tera. :) In any case, thanks a lot for working on this, it'll allow to improve the code a lot! @bors: r=jyn514,GuillaumeGomez |
📌 Commit cd0f931 has been approved by |
☀️ Test successful - checks-actions |
In rust-lang#86157 cd0f931 Use Tera templates for rustdoc. dropped the following transformation from the keys of the default settings element's `data-` attribute names: .map(|(k, v)| format!(r#" data-{}="{}""#, k.replace('-', "_"), Escape(v))) The `Escape` part is indeed no longer needed, because Tera does that for us. But the massaging of `-` to `_` is needed, for the (bizarre) reasons explained in the new comments. I have tested that the default theme function works again for me. I have also verified that passing --default-theme="zork&" escapes the value in the HTML. Closes rust-lang#87263. CC: Jacob Hoffman-Andrews <[email protected]> Signed-off-by: Ian Jackson <[email protected]>
…Gomez rustdoc: Restore --default-theme, etc, by restoring varname escaping In rust-lang#86157 cd0f931 Use Tera templates for rustdoc. dropped the following transformation from the keys of the default settings element's `data-` attribute names: .map(|(k, v)| format!(r#" data-{}="{}""#, k.replace('-', "_"), Escape(v))) The `Escape` part is indeed no longer needed, because Tera does that for us. But the massaging of `-` to `_` is needed, for the (bizarre) reasons explained in the new comments. I have tested that the default theme function works again for me. I have also verified that passing (in shell syntax) '--default-theme="zork&"' escapes the value in the HTML. Closes rust-lang#87263
Replaces a format!() call in layout::render with a template
expansion. Introduces a
templates
field in SharedContext so partsof rustdoc can share pre-rendered templates.
This currently builds in a copy of the single template available, like
with static files. However, future work can make this live-loadable with
a perma-unstable flag, to make rustdoc developers' work easier.
Part of #84419.
Demo at https://hoffman-andrews.com/rust/tera/std/string/struct.String.html.