From 6e133b25e02edba228c488a4239334885b5ebc85 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Sun, 28 Nov 2021 16:37:04 +0100 Subject: config: add way for setting pandoc default options and update Cargo.lock --- src/build.rs | 13 ++++++++++++- src/config.rs | 30 +++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/build.rs b/src/build.rs index 1c12476..cf34e2f 100644 --- a/src/build.rs +++ b/src/build.rs @@ -14,12 +14,22 @@ const HTML_TEMPLATE: &str = include_str!("../res/template.html"); pub fn do_build(config: &crate::config::Config) -> Result<()> { let tmpdir = tempfile::tempdir().wrap_err("Could not create temporary directory")?; - debug!("Created temporary directory at: '{}'", tmpdir.path().display()); + debug!( + "Created temporary directory at: '{}'", + tmpdir.path().display() + ); + let template_path = tmpdir.path().join("template.html"); trace!("Writing HTML template to: '{}'", template_path.display()); std::fs::write(&template_path, HTML_TEMPLATE) .wrap_err("Could not save HTML template in temporary directory")?; + let defaults_path = tmpdir.path().join("defaults.yaml"); + debug!("Generating file: '{}'", defaults_path.display()); + let defaults = + std::fs::File::create(&defaults_path).wrap_err("Could not create defaults.yaml")?; + serde_json::to_writer(defaults, &config.pandoc).wrap_err("Could not create defaults.yaml")?; + let source_root = Path::new(&config.book.summary) .parent() .expect("Summary has no parent"); @@ -99,6 +109,7 @@ pub fn do_build(config: &crate::config::Config) -> Result<()> { .set_output_format(pandoc::OutputFormat::Html5, vec![]) .add_options(&[ pandoc::PandocOption::Css(style_path.to_string()), + pandoc::PandocOption::Defaults(defaults_path.clone()), pandoc::PandocOption::SectionDivs, pandoc::PandocOption::Standalone, pandoc::PandocOption::Template(template_path.clone()), diff --git a/src/config.rs b/src/config.rs index 53922b0..9b95f9a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use log::debug; use serde::{Deserialize, Serialize}; @@ -7,6 +9,21 @@ pub struct Config { pub book: BookConfig, #[serde(default)] pub build: BuildConfig, + #[serde(default)] + pub pandoc: HashMap, +} + +impl Config { + pub fn new(config_file: &str) -> Result { + let mut s = config::Config::default(); + + debug!("Parsing config file: {}", config_file); + s.merge(config::File::with_name(config_file))?; + debug!("Parsing config from environment"); + s.merge(config::Environment::with_prefix("PANDOC_DOCBOOK").separator("_"))?; + + s.try_into() + } } #[derive(Debug, Clone, Deserialize, Serialize)] @@ -44,16 +61,3 @@ impl Default for BuildConfig { fn default_build_dir() -> String { "pdbook".to_string() } - -impl Config { - pub fn new(config_file: &str) -> Result { - let mut s = config::Config::default(); - - debug!("Parsing config file: {}", config_file); - s.merge(config::File::with_name(config_file))?; - debug!("Parsing config from environment"); - s.merge(config::Environment::with_prefix("PANDOC_DOCBOOK").separator("_"))?; - - s.try_into() - } -} -- cgit v1.2.3