Skip to content

Commit 60c159b

Browse files
committed
Update to lol-html 0.3
This makes the RewritingError type Send + Sync, which is necessary to convert it to an anyhow error so we can send it to sentry. Also, it's nice to have updated dependencies.
1 parent 3868e79 commit 60c159b

File tree

3 files changed

+35
-43
lines changed

3 files changed

+35
-43
lines changed

Cargo.lock

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ path-slash = "0.1.3"
5353
once_cell = { version = "1.4.0", features = ["parking_lot"] }
5454
base64 = "0.13"
5555
strum = { version = "0.18.0", features = ["derive"] }
56-
lol_html = "0.2"
56+
lol_html = "0.3"
5757
font-awesome-as-a-crate = { path = "crates/font-awesome-as-a-crate" }
5858
dashmap = "3.11.10"
5959
string_cache = "0.8.0"

src/utils/html.rs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::web::page::TemplateData;
2+
use lol_html::element;
23
use lol_html::errors::RewritingError;
34
use tera::Context;
45

@@ -15,21 +16,14 @@ pub(crate) fn rewrite_lol(
1516
templates: &TemplateData,
1617
) -> Result<Vec<u8>, RewritingError> {
1718
use lol_html::html_content::{ContentType, Element};
18-
use lol_html::{ElementContentHandlers, HtmlRewriter, MemorySettings, Settings};
19+
use lol_html::{HtmlRewriter, MemorySettings, Settings};
1920

2021
let templates = templates.templates.load();
2122
let tera_head = templates.render("rustdoc/head.html", &ctx).unwrap();
2223
let tera_vendored_css = templates.render("rustdoc/vendored.html", &ctx).unwrap();
2324
let tera_body = templates.render("rustdoc/body.html", &ctx).unwrap();
2425
let tera_rustdoc_topbar = templates.render("rustdoc/topbar.html", &ctx).unwrap();
2526

26-
// Append `style.css` stylesheet after all head elements.
27-
let head_handler = |head: &mut Element| {
28-
head.append(&tera_head, ContentType::Html);
29-
30-
Ok(())
31-
};
32-
3327
// Before: <body> ... rustdoc content ... </body>
3428
// After:
3529
// ```html
@@ -64,35 +58,24 @@ pub(crate) fn rewrite_lol(
6458
Ok(())
6559
};
6660

67-
// Append `vendored.css` before `rustdoc.css`, so that the duplicate copy of
68-
// `normalize.css` will be overridden by the later version.
69-
let first_stylesheet_handler = |head: &mut Element| {
70-
head.before(&tera_vendored_css, ContentType::Html);
71-
72-
Ok(())
73-
};
74-
75-
let (head_selector, body_selector, first_stylesheet_selector) = (
76-
"head".parse().unwrap(),
77-
"body".parse().unwrap(),
78-
"link[type='text/css'][href*='rustdoc']".parse().unwrap(),
79-
);
80-
let element_content_handlers = vec![
81-
(
82-
&head_selector,
83-
ElementContentHandlers::default().element(head_handler),
84-
),
85-
(
86-
&body_selector,
87-
ElementContentHandlers::default().element(body_handler),
88-
),
89-
(
90-
&first_stylesheet_selector,
91-
ElementContentHandlers::default().element(first_stylesheet_handler),
92-
),
93-
];
9461
let settings = Settings {
95-
element_content_handlers,
62+
element_content_handlers: vec![
63+
// Append `style.css` stylesheet after all head elements.
64+
element!("head", |head: &mut Element| {
65+
head.append(&tera_head, ContentType::Html);
66+
Ok(())
67+
}),
68+
element!("body", body_handler),
69+
// Append `vendored.css` before `rustdoc.css`, so that the duplicate copy of
70+
// `normalize.css` will be overridden by the later version.
71+
element!(
72+
"link[type='text/css'][href*='rustdoc']",
73+
|rustdoc_css: &mut Element| {
74+
rustdoc_css.before(&tera_vendored_css, ContentType::Html);
75+
Ok(())
76+
}
77+
),
78+
],
9679
memory_settings: MemorySettings {
9780
max_allowed_memory_usage,
9881
..MemorySettings::default()
@@ -103,10 +86,9 @@ pub(crate) fn rewrite_lol(
10386
// The input and output are always strings, we just use `&[u8]` so we only have to validate once.
10487
let mut buffer = Vec::new();
10588
// TODO: Make the rewriter persistent?
106-
let mut writer = HtmlRewriter::try_new(settings, |bytes: &[u8]| {
89+
let mut writer = HtmlRewriter::new(settings, |bytes: &[u8]| {
10790
buffer.extend_from_slice(bytes);
108-
})
109-
.expect("utf8 is a valid encoding");
91+
});
11092

11193
writer.write(html)?;
11294
writer.end()?;

0 commit comments

Comments
 (0)