From 5d526af0aca0780824d7ef5f319b8b2c8cba39f7 Mon Sep 17 00:00:00 2001 From: Ruben Moor Date: Mon, 6 Jul 2020 09:18:10 -0500 Subject: [PATCH] template logic --- src/preprocess/links.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index 4fe06a9d..b52d9f64 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -140,6 +140,7 @@ enum LinkType<'a> { Title(&'a str), Template(PathBuf, HashMap), Title(&'a str), + Template(PathBuf, HashMap), } #[derive(PartialEq, Debug, Clone)] @@ -213,6 +214,7 @@ impl<'a> LinkType<'a> { LinkType::Title(_) => None, LinkType::Template(p, _) => Some(return_relative_path(base, &p)), LinkType::Title(_) => None, + LinkType::Template(p, _) => Some(return_relative_path(base, &p)), } } } @@ -431,6 +433,23 @@ impl<'a> Link<'a> { *chapter_title = title.to_owned(); Ok(String::new()) } + + LinkType::Template(ref pat, ref dict) => { + let target = base.join(pat); + fs::read_to_string(&target) + .map(|s| { + dict.iter().fold(s, |r, (key, value)| { + r.replace(format!("{{ {} }}", key).as_str(), value) + }) + }) + .with_context(|| { + format!( + "Could not read file for template {} ({})", + self.link_text, + target.display(), + ) + }) + } } } }