From 768d1af3a1da2bc594aa0808b22178f0da638ae1 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Wed, 31 May 2023 16:24:42 -0500 Subject: [PATCH] edited: pandoc header to run on pattern files --- files/README/source/docs/header.meta | 6 +++--- files/README/source/index.meta | 3 ++- files/test_site/pattern/test/pandoc.meta | 3 +++ .../source/unit_tests/header/expandoc.meta | 3 +++ src/lib.rs | 2 +- src/metafile/file.rs | 13 ++++++++++--- src/metafile/file/patterns.rs | 6 +++++- src/metafile/file/source.rs | 5 +++-- src/metafile/header.rs | 6 +++--- src/tests.rs | 10 ++++++++-- 10 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 files/test_site/pattern/test/pandoc.meta create mode 100644 files/test_site/source/unit_tests/header/expandoc.meta diff --git a/files/README/source/docs/header.meta b/files/README/source/docs/header.meta index 5df7eb2..48dca52 100644 --- a/files/README/source/docs/header.meta +++ b/files/README/source/docs/header.meta @@ -37,12 +37,12 @@ normal definition blocks. - blank = **BOOL** - if true, stops parsing and returns an empty string - panic_default = **BOOL** - if true, panics on an undefined default pattern - panic_undefined = **BOOL** - if true, panics on an undefined variable or array +- source = **STRING** - change the the filetype of the source file +- filetype = **STRING** - change the filetype of the output file - equal_arrays = **BOOL** - if true, panics if arrays in the same pattern have different sizes +- pandoc = **BOOL** - toggles if pandoc is ran on this file to convert between filetypes, defaults to *true* in **source** dir, and *false* in **pattern** dir. ### source - ignore = **BOOL** - stops parsing and skips this file, useful for ignoring directories with scoped definitions -- source = **STRING** - change the the filetype of the source file -- filetype = **STRING** - change the filetype of the output file -- pandoc = **BOOL** - toggles if pandoc is ran on this file to convert between filetypes - copy_only = **BOOL** - copys file or directory without processing anything diff --git a/files/README/source/index.meta b/files/README/source/index.meta index 34fde86..dfbb4bd 100644 --- a/files/README/source/index.meta +++ b/files/README/source/index.meta @@ -25,8 +25,9 @@ you rebuild it. - spaces are correctly preserved between directly adjacent substitutions - copy_only header directive +- pandoc header now works on pattern files ## versions -- 0.1.3: grammar update +- 0.1.3: grammar update, new header directives - 0.1.2: multithreading - 0.1.1: initial release diff --git a/files/test_site/pattern/test/pandoc.meta b/files/test_site/pattern/test/pandoc.meta new file mode 100644 index 0000000..6d3869a --- /dev/null +++ b/files/test_site/pattern/test/pandoc.meta @@ -0,0 +1,3 @@ +#{ pandoc = true } + +# GOOD diff --git a/files/test_site/source/unit_tests/header/expandoc.meta b/files/test_site/source/unit_tests/header/expandoc.meta new file mode 100644 index 0000000..aa6c241 --- /dev/null +++ b/files/test_site/source/unit_tests/header/expandoc.meta @@ -0,0 +1,3 @@ +&{test = 'pandoc'} + +source diff --git a/src/lib.rs b/src/lib.rs index 1f34105..696d5eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,7 +83,7 @@ pub fn single_file(opts: &Options) -> Result { })), }?; - let file = parse_string(source, opts)?; + let mut file = parse_string(source, opts)?; Ok(file.construct()?) } diff --git a/src/metafile/file.rs b/src/metafile/file.rs index 4e7a51b..b865709 100644 --- a/src/metafile/file.rs +++ b/src/metafile/file.rs @@ -10,6 +10,7 @@ use pandoc::{InputFormat, InputKind, OutputFormat, OutputKind, Pandoc}; use std::{collections::HashMap, path::PathBuf}; use super::*; + #[derive(Debug, Clone)] pub struct MetaFile<'a> { pub opts: &'a Options, @@ -54,7 +55,7 @@ impl<'a> MetaFile<'a> { Ok(metafile) } - pub fn construct(&self) -> Result> { + pub fn construct(&mut self) -> Result> { log!(self.opts, format!("building {}", self.path.display()), 1); if self.header.blank { @@ -70,7 +71,12 @@ impl<'a> MetaFile<'a> { return Err(Box::new(MetaError::Ignored)); } - let html = self.to_html().map_err(MetaError::from)?; + let src_str: String; + if self.header.pandoc.map_or(true, |x| x) { + src_str = self.pandoc().map_err(MetaError::from)?; + } else { + src_str = self.get_source().map_err(MetaError::from)?; + } let pattern = self.get_pattern("base").map_err(MetaError::from)?; let mut base = parse_string(pattern, self.opts).map_err(|e| MetaError::ParserError { @@ -79,7 +85,8 @@ impl<'a> MetaFile<'a> { })?; base.merge(self); - base.patterns.insert(Scope::create_global("SOURCE"), html); + base.patterns + .insert(Scope::create_global("SOURCE"), src_str); let mut base_path = self.opts.pattern.join("base").join( self.patterns .get(&Scope::create_global("base")) diff --git a/src/metafile/file/patterns.rs b/src/metafile/file/patterns.rs index d019484..f50fd6f 100644 --- a/src/metafile/file/patterns.rs +++ b/src/metafile/file/patterns.rs @@ -69,6 +69,10 @@ impl<'a> MetaFile<'a> { // copy over maps for expanding contained variables pattern.merge(self); - pattern.get_source() + if pattern.header.pandoc.unwrap_or(false) { + pattern.pandoc() + } else { + pattern.get_source() + } } } diff --git a/src/metafile/file/source.rs b/src/metafile/file/source.rs index 6ffad58..3aca3b4 100644 --- a/src/metafile/file/source.rs +++ b/src/metafile/file/source.rs @@ -1,10 +1,10 @@ use super::*; impl<'a> MetaFile<'a> { - pub fn to_html(&self) -> Result { + pub fn pandoc(&mut self) -> Result { let string = self.get_source()?; - if self.opts.no_pandoc || !self.header.pandoc || string.is_empty() { + if self.opts.no_pandoc || string.is_empty() { return Ok(string); } @@ -28,6 +28,7 @@ impl<'a> MetaFile<'a> { .set_output_format(output, vec![]); if let pandoc::PandocOutput::ToBuffer(s) = pandoc.execute()? { + self.header.pandoc = Some(false); Ok(s) } else { Err(MetaError::Pandoc { file: self.name()? }.into()) diff --git a/src/metafile/header.rs b/src/metafile/header.rs index 7f88674..d99d5c4 100644 --- a/src/metafile/header.rs +++ b/src/metafile/header.rs @@ -8,7 +8,7 @@ pub struct Header { pub equal_arrays: bool, pub filetype: String, pub source: String, - pub pandoc: bool, + pub pandoc: Option, pub ignore: bool, pub copy_only: bool, } @@ -22,7 +22,7 @@ impl Header { equal_arrays: false, filetype: String::from("html"), source: String::from("markdown"), - pandoc: true, + pandoc: None, ignore: false, copy_only: false, } @@ -38,7 +38,7 @@ impl From> for Header { "panic_default" => header.panic_default = val == "true", "panic_undefined" => header.panic_undefined = val == "true", "equal_arrays" => header.equal_arrays = val == "true", - "pandoc" => header.pandoc = val == "true", + "pandoc" => header.pandoc = Some(val == "true"), "filetype" => header.filetype = val.to_string(), "source" => header.source = val.to_string(), "ignore" => header.ignore = val == "true", diff --git a/src/tests.rs b/src/tests.rs index f265a7f..b0086f6 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -17,7 +17,7 @@ macro_rules! unit_test ( let test_dir = opts.source.join("unit_tests"); let mut path = test_dir.join($file); path.set_extension("meta"); - let file = MetaFile::build(path, &opts)?; + let mut file = MetaFile::build(path, &opts)?; let str = match file.construct() { Ok(f) => f, @@ -50,7 +50,7 @@ macro_rules! panic_test ( let test_dir = opts.source.join("unit_tests"); let mut path = test_dir.join($file); path.set_extension("meta"); - let file = MetaFile::build(path, &opts).unwrap(); + let mut file = MetaFile::build(path, &opts).unwrap(); assert_eq!(file.construct().unwrap(), $test); } }; @@ -141,6 +141,12 @@ unit_test!( r#"variable: ${this} should get copied verbatim"# ); +unit_test!( + expandoc, + "header/expandoc", + "\n

GOOD

\n\n\n\n" +); + panic_test!(ignore, "ignore.meta", ""); #[test] -- 2.45.2