diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
commit | 218caa410aa38c29984be31a5229b9fa717560ee (patch) | |
tree | c54bd55eeb6e4c508940a30e94c0032fbd45d677 /vendor/mdbook/src/renderer | |
parent | Releasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/mdbook/src/renderer')
7 files changed, 94 insertions, 92 deletions
diff --git a/vendor/mdbook/src/renderer/html_handlebars/hbs_renderer.rs b/vendor/mdbook/src/renderer/html_handlebars/hbs_renderer.rs index b933a359a..1b648dac1 100644 --- a/vendor/mdbook/src/renderer/html_handlebars/hbs_renderer.rs +++ b/vendor/mdbook/src/renderer/html_handlebars/hbs_renderer.rs @@ -14,7 +14,10 @@ use std::path::{Path, PathBuf}; use crate::utils::fs::get_404_output_file; use handlebars::Handlebars; +use log::{debug, trace, warn}; +use once_cell::sync::Lazy; use regex::{Captures, Regex}; +use serde_json::json; #[derive(Default)] pub struct HtmlHandlebars; @@ -337,6 +340,7 @@ impl HtmlHandlebars { ); handlebars.register_helper("previous", Box::new(helpers::navigation::previous)); handlebars.register_helper("next", Box::new(helpers::navigation::next)); + // TODO: remove theme_option in 0.5, it is not needed. handlebars.register_helper("theme_option", Box::new(helpers::theme::theme_option)); } @@ -627,6 +631,7 @@ fn make_data( ); } + // TODO: remove default_theme in 0.5, it is not needed. let default_theme = match html_config.default_theme { Some(ref theme) => theme.to_lowercase(), None => "light".to_string(), @@ -764,9 +769,8 @@ fn make_data( /// Goes through the rendered HTML, making sure all header tags have /// an anchor respectively so people can link to sections directly. fn build_header_links(html: &str) -> String { - lazy_static! { - static ref BUILD_HEADER_LINKS: Regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap(); - } + static BUILD_HEADER_LINKS: Lazy<Regex> = + Lazy::new(|| Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap()); let mut id_counter = HashMap::new(); @@ -807,10 +811,8 @@ fn insert_link_into_header( // ``` // This function replaces all commas by spaces in the code block classes fn fix_code_blocks(html: &str) -> String { - lazy_static! { - static ref FIX_CODE_BLOCKS: Regex = - Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap(); - } + static FIX_CODE_BLOCKS: Lazy<Regex> = + Lazy::new(|| Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap()); FIX_CODE_BLOCKS .replace_all(html, |caps: &Captures<'_>| { @@ -833,10 +835,9 @@ fn add_playground_pre( playground_config: &Playground, edition: Option<RustEdition>, ) -> String { - lazy_static! { - static ref ADD_PLAYGROUND_PRE: Regex = - Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap(); - } + static ADD_PLAYGROUND_PRE: Lazy<Regex> = + Lazy::new(|| Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap()); + ADD_PLAYGROUND_PRE .replace_all(html, |caps: &Captures<'_>| { let text = &caps[1]; @@ -899,18 +900,19 @@ fn add_playground_pre( } fn hide_lines(content: &str) -> String { - lazy_static! { - static ref BORING_LINES_REGEX: Regex = Regex::new(r"^(\s*)#(.?)(.*)$").unwrap(); - } + static BORING_LINES_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\s*)#(.?)(.*)$").unwrap()); let mut result = String::with_capacity(content.len()); - for line in content.lines() { + let mut lines = content.lines().peekable(); + while let Some(line) = lines.next() { + // Don't include newline on the last line. + let newline = if lines.peek().is_none() { "" } else { "\n" }; if let Some(caps) = BORING_LINES_REGEX.captures(line) { if &caps[2] == "#" { result += &caps[1]; result += &caps[2]; result += &caps[3]; - result += "\n"; + result += newline; continue; } else if &caps[2] != "!" && &caps[2] != "[" { result += "<span class=\"boring\">"; @@ -919,13 +921,13 @@ fn hide_lines(content: &str) -> String { result += &caps[2]; } result += &caps[3]; - result += "\n"; + result += newline; result += "</span>"; continue; } } result += line; - result += "\n"; + result += newline; } result } @@ -1005,19 +1007,19 @@ mod tests { fn add_playground() { let inputs = [ ("<code class=\"language-rust\">x()</code>", - "<pre class=\"playground\"><code class=\"language-rust\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}</span></code></pre>"), ("<code class=\"language-rust\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust\">fn main() {}</code></pre>"), ("<code class=\"language-rust editable\">let s = \"foo\n # bar\n\";</code>", - "<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n<span class=\"boring\"> bar\n</span>\";\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n<span class=\"boring\"> bar\n</span>\";</code></pre>"), ("<code class=\"language-rust editable\">let s = \"foo\n ## bar\n\";</code>", - "<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n\";\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n # bar\n\";</code></pre>"), ("<code class=\"language-rust editable\">let s = \"foo\n # bar\n#\n\";</code>", - "<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n<span class=\"boring\"> bar\n</span><span class=\"boring\">\n</span>\";\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust editable\">let s = \"foo\n<span class=\"boring\"> bar\n</span><span class=\"boring\">\n</span>\";</code></pre>"), ("<code class=\"language-rust ignore\">let s = \"foo\n # bar\n\";</code>", - "<code class=\"language-rust ignore\">let s = \"foo\n<span class=\"boring\"> bar\n</span>\";\n</code>"), + "<code class=\"language-rust ignore\">let s = \"foo\n<span class=\"boring\"> bar\n</span>\";</code>"), ("<code class=\"language-rust editable\">#![no_std]\nlet s = \"foo\";\n #[some_attr]</code>", - "<pre class=\"playground\"><code class=\"language-rust editable\">#![no_std]\nlet s = \"foo\";\n #[some_attr]\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust editable\">#![no_std]\nlet s = \"foo\";\n #[some_attr]</code></pre>"), ]; for (src, should_be) in &inputs { let got = add_playground_pre( @@ -1035,13 +1037,13 @@ mod tests { fn add_playground_edition2015() { let inputs = [ ("<code class=\"language-rust\">x()</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2015\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2015\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}</span></code></pre>"), ("<code class=\"language-rust\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}</code></pre>"), ("<code class=\"language-rust edition2015\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}</code></pre>"), ("<code class=\"language-rust edition2018\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}</code></pre>"), ]; for (src, should_be) in &inputs { let got = add_playground_pre( @@ -1059,13 +1061,13 @@ mod tests { fn add_playground_edition2018() { let inputs = [ ("<code class=\"language-rust\">x()</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2018\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2018\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}</span></code></pre>"), ("<code class=\"language-rust\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}</code></pre>"), ("<code class=\"language-rust edition2015\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}</code></pre>"), ("<code class=\"language-rust edition2018\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}</code></pre>"), ]; for (src, should_be) in &inputs { let got = add_playground_pre( @@ -1083,13 +1085,13 @@ mod tests { fn add_playground_edition2021() { let inputs = [ ("<code class=\"language-rust\">x()</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2021\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}\n</span></code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2021\"><span class=\"boring\">#![allow(unused)]\n</span><span class=\"boring\">fn main() {\n</span>x()\n<span class=\"boring\">}</span></code></pre>"), ("<code class=\"language-rust\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2021\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2021\">fn main() {}</code></pre>"), ("<code class=\"language-rust edition2015\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2015\">fn main() {}</code></pre>"), ("<code class=\"language-rust edition2018\">fn main() {}</code>", - "<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}\n</code></pre>"), + "<pre class=\"playground\"><code class=\"language-rust edition2018\">fn main() {}</code></pre>"), ]; for (src, should_be) in &inputs { let got = add_playground_pre( diff --git a/vendor/mdbook/src/renderer/html_handlebars/helpers/navigation.rs b/vendor/mdbook/src/renderer/html_handlebars/helpers/navigation.rs index 65929bbfc..b184c4410 100644 --- a/vendor/mdbook/src/renderer/html_handlebars/helpers/navigation.rs +++ b/vendor/mdbook/src/renderer/html_handlebars/helpers/navigation.rs @@ -4,6 +4,8 @@ use std::path::Path; use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError, Renderable}; use crate::utils; +use log::{debug, trace}; +use serde_json::json; type StringMap = BTreeMap<String, String>; @@ -146,15 +148,12 @@ fn render( trace!("Render template"); - _h.template() - .ok_or_else(|| RenderError::new("Error with the handlebars template")) - .and_then(|t| { - let local_ctx = Context::wraps(&context)?; - let mut local_rc = rc.clone(); - t.render(r, &local_ctx, &mut local_rc, out) - })?; - - Ok(()) + let t = _h + .template() + .ok_or_else(|| RenderError::new("Error with the handlebars template"))?; + let local_ctx = Context::wraps(&context)?; + let mut local_rc = rc.clone(); + t.render(r, &local_ctx, &mut local_rc, out) } pub fn previous( diff --git a/vendor/mdbook/src/renderer/html_handlebars/helpers/theme.rs b/vendor/mdbook/src/renderer/html_handlebars/helpers/theme.rs index 809ee1176..83aba6774 100644 --- a/vendor/mdbook/src/renderer/html_handlebars/helpers/theme.rs +++ b/vendor/mdbook/src/renderer/html_handlebars/helpers/theme.rs @@ -1,4 +1,5 @@ use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError}; +use log::trace; pub fn theme_option( h: &Helper<'_, '_>, diff --git a/vendor/mdbook/src/renderer/html_handlebars/helpers/toc.rs b/vendor/mdbook/src/renderer/html_handlebars/helpers/toc.rs index 0884d30ad..e96e6ef64 100644 --- a/vendor/mdbook/src/renderer/html_handlebars/helpers/toc.rs +++ b/vendor/mdbook/src/renderer/html_handlebars/helpers/toc.rs @@ -117,35 +117,35 @@ impl HelperDef for RenderToc { } // Link - let path_exists = if let Some(path) = - item.get("path") - .and_then(|p| if p.is_empty() { None } else { Some(p) }) - { - out.write("<a href=\"")?; - - let tmp = Path::new(item.get("path").expect("Error: path should be Some(_)")) - .with_extension("html") - .to_str() - .unwrap() - // Hack for windows who tends to use `\` as separator instead of `/` - .replace('\\', "/"); - - // Add link - out.write(&utils::fs::path_to_root(¤t_path))?; - out.write(&tmp)?; - out.write("\"")?; - - if path == ¤t_path || is_first_chapter { - is_first_chapter = false; - out.write(" class=\"active\"")?; - } + let path_exists: bool; + match item.get("path") { + Some(path) if !path.is_empty() => { + out.write("<a href=\"")?; + let tmp = Path::new(path) + .with_extension("html") + .to_str() + .unwrap() + // Hack for windows who tends to use `\` as separator instead of `/` + .replace('\\', "/"); + + // Add link + out.write(&utils::fs::path_to_root(¤t_path))?; + out.write(&tmp)?; + out.write("\"")?; + + if path == ¤t_path || is_first_chapter { + is_first_chapter = false; + out.write(" class=\"active\"")?; + } - out.write(">")?; - true - } else { - out.write("<div>")?; - false - }; + out.write(">")?; + path_exists = true; + } + _ => { + out.write("<div>")?; + path_exists = false; + } + } if !self.no_section_label { // Section does not necessarily exist diff --git a/vendor/mdbook/src/renderer/html_handlebars/search.rs b/vendor/mdbook/src/renderer/html_handlebars/search.rs index c3b944c9d..a9e2f5ca6 100644 --- a/vendor/mdbook/src/renderer/html_handlebars/search.rs +++ b/vendor/mdbook/src/renderer/html_handlebars/search.rs @@ -3,6 +3,7 @@ use std::collections::{HashMap, HashSet}; use std::path::Path; use elasticlunr::{Index, IndexBuilder}; +use once_cell::sync::Lazy; use pulldown_cmark::*; use crate::book::{Book, BookItem}; @@ -10,7 +11,7 @@ use crate::config::Search; use crate::errors::*; use crate::theme::searcher; use crate::utils; - +use log::{debug, warn}; use serde::Serialize; const MAX_WORD_LENGTH_TO_INDEX: usize = 80; @@ -266,21 +267,19 @@ fn write_to_json(index: Index, search_config: &Search, doc_urls: Vec<String>) -> } fn clean_html(html: &str) -> String { - lazy_static! { - static ref AMMONIA: ammonia::Builder<'static> = { - let mut clean_content = HashSet::new(); - clean_content.insert("script"); - clean_content.insert("style"); - let mut builder = ammonia::Builder::new(); - builder - .tags(HashSet::new()) - .tag_attributes(HashMap::new()) - .generic_attributes(HashSet::new()) - .link_rel(None) - .allowed_classes(HashMap::new()) - .clean_content_tags(clean_content); - builder - }; - } + static AMMONIA: Lazy<ammonia::Builder<'static>> = Lazy::new(|| { + let mut clean_content = HashSet::new(); + clean_content.insert("script"); + clean_content.insert("style"); + let mut builder = ammonia::Builder::new(); + builder + .tags(HashSet::new()) + .tag_attributes(HashMap::new()) + .generic_attributes(HashSet::new()) + .link_rel(None) + .allowed_classes(HashMap::new()) + .clean_content_tags(clean_content); + builder + }); AMMONIA.clean(html).to_string() } diff --git a/vendor/mdbook/src/renderer/markdown_renderer.rs b/vendor/mdbook/src/renderer/markdown_renderer.rs index bd5def1f4..13bd05cc3 100644 --- a/vendor/mdbook/src/renderer/markdown_renderer.rs +++ b/vendor/mdbook/src/renderer/markdown_renderer.rs @@ -2,7 +2,7 @@ use crate::book::BookItem; use crate::errors::*; use crate::renderer::{RenderContext, Renderer}; use crate::utils; - +use log::trace; use std::fs; #[derive(Default)] diff --git a/vendor/mdbook/src/renderer/mod.rs b/vendor/mdbook/src/renderer/mod.rs index 15465fbce..1c97f8f22 100644 --- a/vendor/mdbook/src/renderer/mod.rs +++ b/vendor/mdbook/src/renderer/mod.rs @@ -27,6 +27,7 @@ use std::process::{Command, Stdio}; use crate::book::Book; use crate::config::Config; use crate::errors::*; +use log::{error, info, trace, warn}; use toml::Value; use serde::{Deserialize, Serialize}; |