misc latex fixes and improvements

master
Kenton Hamaluik 4 years ago
parent 464134953a
commit 48c4fc9629

1
Cargo.lock generated

@ -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)",

@ -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"

@ -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<Path>>(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<Path>>(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 == "<kbd>" {
output.push_str(r"\keys{");
}
else if source == "</kbd>" {
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{");

@ -54,6 +54,9 @@
% force floats
\usepackage{float}
% keyboard symbols
\usepackage{menukeys}
\title{ {{ front.title }} }
\author{ {{ front.author }} }
\date{ {{ front.pubdate|human_date }} }

Loading…
Cancel
Save