From 48c4fc9629cdcc5b21d4e1dd6f0ddaeaad8aaefe Mon Sep 17 00:00:00 2001 From: Kenton Hamaluik Date: Thu, 19 Dec 2019 20:31:02 -0700 Subject: [PATCH] misc latex fixes and improvements --- Cargo.lock | 1 + Cargo.toml | 1 + src/latex.rs | 52 +++++++++++++++++++++++++++++----------------- templates/book.tex | 3 +++ 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed2aa09..8c22b77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -959,6 +959,7 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "md5 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "notify 4.0.14 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.22 (registry+https://github.com/rust-lang/crates.io-index)", "sass-rs 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index d60140f..49999b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ log = { version = "0.4", features = ["max_level_debug", "release_max_level_info" ignore = "0.4" reqwest = "0.9" md5 = "0.7" +regex = "1.3" [build-dependencies] sass-rs = "0.2" diff --git a/src/latex.rs b/src/latex.rs index 54fcc02..2a6d04d 100644 --- a/src/latex.rs +++ b/src/latex.rs @@ -27,6 +27,8 @@ lazy_static! { ext_description_lists: true, ..ComrakOptions::default() }; + + static ref ESCAPE_BACKSLASH_REGEX: regex::Regex = regex::Regex::new(r"\\[^_\#\$%\&\{\}]").expect("valid regex"); } #[derive(Template)] @@ -38,14 +40,18 @@ struct BookTemplate<'a, 'b, 'c> { } fn escape_text(text: &str) -> String { - text - .replace(r"\", r"\textbackslash{}") + let text = text + .replace(r"_", r"\_") .replace(r"#", r"\#") .replace(r"$", r"\$") .replace(r"%", r"\%") .replace(r"&", r"\&") .replace(r"{", r"\{") - .replace(r"}", r"\}") + .replace(r"}", r"\}"); + + let text = ESCAPE_BACKSLASH_REGEX.replace_all(&text, r"\textbackslash{}"); + + text .replace(r"^", r"\textasciicircum{}") .replace(r"~", r"\textasciitilde{}") } @@ -58,13 +64,13 @@ fn format_text<'a>(node: &'a comrak::nodes::AstNode<'a>, output: &mut String) { output.push_str(&escape_text(text)); } }, - //NodeValue::Code(text) => { - // if let Ok(text) = std::str::from_utf8(text) { - // output.push_str("\\texttt{"); - // output.push_str(&escape_text(text)); - // output.push_str("}"); - // } - //}, + NodeValue::Code(text) => { + if let Ok(text) = std::str::from_utf8(text) { + output.push_str("\\texttt{"); + output.push_str(&escape_text(text)); + output.push_str("}"); + } + }, NodeValue::Emph => { output.push_str("\\emph{"); for child in node.children() { format_text(child, output); } @@ -129,7 +135,7 @@ fn format_node<'a, P: AsRef>(section_offset: u32, dest_path: P, node: &'a let mut term: String = String::default(); for child in node.children() { format_text(child, &mut term); } output.push_str(term.trim()); - output.push_str("] "); + output.push_str("] \\hfill \\\\ "); }, NodeValue::DescriptionDetails => { for child in node.children() { format_node(section_offset, dest_path.as_ref(), child, output); } @@ -275,18 +281,26 @@ fn format_node<'a, P: AsRef>(section_offset: u32, dest_path: P, node: &'a }, NodeValue::Code(text) => { if let Ok(text) = std::str::from_utf8(text) { - output.push_str("\\verb|"); - //output.push_str(&escape_text(text)); - output.push_str(text); - output.push_str("|"); + output.push_str("\\texttt{"); + output.push_str(&escape_text(text)); + output.push_str("}"); } }, NodeValue::HtmlInline(text) => { let source = std::str::from_utf8(&text).expect("valid utf-8"); - log::warn!("can't handle inline html `{}`, rendering it as syntax...", source); - output.push_str("\\mintinline{html}{"); - output.push_str(&escape_text(source)); - output.push_str("}"); + + if source == "" { + output.push_str(r"\keys{"); + } + else if source == "" { + output.push_str(r"}"); + } + else { + log::warn!("can't handle inline html `{}`, rendering it as syntax...", source); + output.push_str("\\mintinline{html}{"); + output.push_str(&escape_text(source)); + output.push_str("}"); + } }, NodeValue::Emph => { output.push_str("\\emph{"); diff --git a/templates/book.tex b/templates/book.tex index 96417b7..1d22f03 100644 --- a/templates/book.tex +++ b/templates/book.tex @@ -54,6 +54,9 @@ % force floats \usepackage{float} +% keyboard symbols +\usepackage{menukeys} + \title{ {{ front.title }} } \author{ {{ front.author }} } \date{ {{ front.pubdate|human_date }} }