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 --- Cargo.lock | 21 +++++++++++---------- Cargo.toml | 1 + src/build.rs | 13 ++++++++++++- src/config.rs | 30 +++++++++++++++++------------- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81f5135..57fb5a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,9 +72,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "cc" -version = "1.0.71" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79c2681d6594606957bbb8631c4b90a7fcaaa72cdb714743a437b156d6a7eedd" +checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" [[package]] name = "cfg-if" @@ -284,9 +284,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.106" +version = "0.2.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a60553f9a9e039a333b4e9b20573b9e9b9c0bb3a11e201ccc48ef4283456d673" +checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" [[package]] name = "linked-hash-map" @@ -380,9 +380,9 @@ checksum = "2386b4ebe91c2f7f51082d4cefa145d030e33a1842a96b12e4885cc3c01f7a55" [[package]] name = "pandoc" -version = "0.8.6" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c5ab1abdd81ed62e852d412d18964925e42c8f154c61e89c6296b1a06b6daeb" +checksum = "0eac785b7de8de25c5ec48b3a9df1be552de03906f99145ed6d7da3d696c0dbb" dependencies = [ "itertools", ] @@ -401,6 +401,7 @@ dependencies = [ "pandoc", "pandoc_ast", "serde 1.0.130", + "serde_json", "tempfile", ] @@ -601,9 +602,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.69" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e466864e431129c7e0d3476b92f20458e5879919a0596c6472738d9fa2d342f8" +checksum = "d0ffa0837f2dfa6fb90868c2b5468cad482e175f7dad97e7421951e663f2b527" dependencies = [ "itoa", "ryu", @@ -633,9 +634,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "1.0.81" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966" +checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index e8783c8..bd6ae48 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,4 +20,5 @@ pandoc = "0.8" # TODO: change this when new version is released pandoc_ast = { git = "https://github.com/oli-obk/pandoc-ast", rev = "d73a7d0a065f568d60bcee6cff2340f75065273e" } serde = { version = "1", features = [ "derive" ] } +serde_json = "1" tempfile = "3" 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