diff options
-rw-r--r-- | runcmd.py | 0 | ||||
-rw-r--r-- | runcmd.yaml | 1 | ||||
-rw-r--r-- | src/ast.rs | 70 | ||||
-rw-r--r-- | src/bin/sp-meta.rs | 11 | ||||
-rw-r--r-- | src/codegen.rs | 10 | ||||
-rw-r--r-- | subplot.md | 28 | ||||
-rw-r--r-- | templates/bash/template.sh | 2 | ||||
-rw-r--r-- | templates/python/template.py | 2 |
8 files changed, 74 insertions, 50 deletions
diff --git a/runcmd.py b/runcmd.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/runcmd.py diff --git a/runcmd.yaml b/runcmd.yaml new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/runcmd.yaml @@ -0,0 +1 @@ +[] @@ -143,11 +143,11 @@ impl<'a> Document { pub fn sources(&mut self) -> Vec<PathBuf> { let mut names = vec![]; - if let Some(x) = self.meta().bindings_filename() { + for x in self.meta().bindings_filenames() { names.push(PathBuf::from(x)) } - if let Some(x) = self.meta().functions_filename() { + for x in self.meta().functions_filenames() { names.push(PathBuf::from(x)) } @@ -426,9 +426,9 @@ mod test_extract { pub struct Metadata { title: String, date: Option<String>, - bindings_filename: Option<PathBuf>, + bindings_filenames: Vec<PathBuf>, bindings: Bindings, - functions_filename: Option<PathBuf>, + functions_filenames: Vec<PathBuf>, template: Option<String>, bibliographies: Vec<PathBuf>, /// Extra class names which should be considered 'correct' for this document @@ -443,21 +443,21 @@ impl Metadata { { let title = get_title(&doc.meta)?; let date = get_date(&doc.meta); - let bindings_filename = get_bindings_filename(basedir.as_ref(), &doc.meta)?; - let functions_filename = get_functions_filename(basedir.as_ref(), &doc.meta)?; + let bindings_filenames = get_bindings_filenames(basedir.as_ref(), &doc.meta); + let functions_filenames = get_functions_filenames(basedir.as_ref(), &doc.meta); let template = get_template_name(&doc.meta)?; let mut bindings = Bindings::new(); - if let Some(ref filename) = bindings_filename { - get_bindings(filename, &mut bindings)?; - } + let bibliographies = get_bibliographies(basedir.as_ref(), &doc.meta); let classes = get_classes(&doc.meta); + + get_bindings(&bindings_filenames, &mut bindings)?; Ok(Metadata { title, date, - bindings_filename, + bindings_filenames, bindings, - functions_filename, + functions_filenames, template, bibliographies, classes, @@ -479,19 +479,16 @@ impl Metadata { } /// Return filename where bindings are specified. - pub fn bindings_filename(&self) -> Option<&Path> { - match &self.bindings_filename { - Some(filename) => Some(&filename), - None => None, - } + pub fn bindings_filenames(&self) -> Vec<&Path> { + self.bindings_filenames.iter().map(|f| f.as_ref()).collect() } /// Return filename where functions are specified. - pub fn functions_filename(&self) -> Option<&Path> { - match &self.functions_filename { - Some(filename) => Some(&filename), - None => None, - } + pub fn functions_filenames(&self) -> Vec<&Path> { + self.functions_filenames + .iter() + .map(|f| f.as_ref()) + .collect() } /// Return the name of the code template, if specified. @@ -536,24 +533,18 @@ fn get_date(map: &Mapp) -> Option<String> { } } -fn get_bindings_filename<P>(basedir: P, map: &Mapp) -> Result<Option<PathBuf>> +fn get_bindings_filenames<P>(basedir: P, map: &Mapp) -> Vec<PathBuf> where P: AsRef<Path>, { - match get_path(map, "bindings") { - None => Ok(None), - Some(path) => Ok(Some(basedir.as_ref().join(path))), - } + get_paths(basedir, map, "bindings") } -fn get_functions_filename<P>(basedir: P, map: &Mapp) -> Result<Option<PathBuf>> +fn get_functions_filenames<P>(basedir: P, map: &Mapp) -> Vec<PathBuf> where P: AsRef<Path>, { - match get_path(map, "functions") { - None => Ok(None), - Some(path) => Ok(Some(basedir.as_ref().join(path))), - } + get_paths(basedir, map, "functions") } fn get_template_name(map: &Mapp) -> Result<Option<String>> { @@ -563,10 +554,13 @@ fn get_template_name(map: &Mapp) -> Result<Option<String>> { } } -fn get_path(map: &Mapp, field: &str) -> Option<PathBuf> { - match get_string(map, field) { - None => None, - Some(s) => Some(Path::new(&s).to_path_buf()), +fn get_paths<P>(basedir: P, map: &Mapp, field: &str) -> Vec<PathBuf> +where + P: AsRef<Path>, +{ + match map.get(field) { + None => vec![], + Some(v) => pathbufs(basedir, v), } } @@ -687,11 +681,13 @@ mod test_join { } } -fn get_bindings<P>(filename: P, bindings: &mut Bindings) -> Result<()> +fn get_bindings<P>(filenames: &[P], bindings: &mut Bindings) -> Result<()> where P: AsRef<Path>, { - bindings.add_from_file(filename)?; + for filename in filenames { + bindings.add_from_file(filename)?; + } Ok(()) } diff --git a/src/bin/sp-meta.rs b/src/bin/sp-meta.rs index 979f18a..cfa3b3d 100644 --- a/src/bin/sp-meta.rs +++ b/src/bin/sp-meta.rs @@ -22,8 +22,15 @@ fn main() -> Result<()> { } println!("title: {}", doc.meta().title()); - println!("bindings: {}", filename(doc.meta().bindings_filename())); - println!("functions: {}", filename(doc.meta().functions_filename())); + + for filename in doc.meta().bindings_filenames() { + println!("bindings: {}", filename.display()); + } + + for filename in doc.meta().functions_filenames() { + println!("functions: {}", filename.display()); + } + for bib in doc.meta().bibliographies().iter() { println!("bibliography: {}", filename(Some(bib))); } diff --git a/src/codegen.rs b/src/codegen.rs index 70d1966..db31765 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -41,12 +41,12 @@ fn context(doc: &mut Document) -> Result<Context> { context.insert("scenarios", &doc.matched_scenarios()?); context.insert("files", doc.files()); - let (funcs_filename, funcs) = match doc.meta().functions_filename() { - Some(filename) => (filename, cat(filename)?), - None => (Path::new(""), "".to_string()), - }; + let funcs_filenames = doc.meta().functions_filenames(); + let mut funcs = String::new(); + for filename in funcs_filenames { + funcs.push_str(&cat(filename)?); + } context.insert("functions", &funcs); - context.insert("functions_filename", funcs_filename); Ok(context) } @@ -1456,17 +1456,22 @@ or functions files. ~~~scenario given file images.md and file b.yaml +and file other.yaml and file f.py +and file other.py and file foo.bib and file bar.bib when I run sp-meta images.md then output matches /source: images.md/ and output matches /source: b.yaml/ +and output matches /source: other.yaml/ and output matches /source: f.py/ +and output matches /source: other.py/ and output matches /source: foo.bib/ and output matches /source: bar.bib/ and output matches /source: image.gif/ and output matches /bindings: b.yaml/ +and output matches /bindings: other.yaml/ and output matches /functions: f.py/ ~~~ @@ -1474,14 +1479,25 @@ and output matches /functions: f.py/ ~~~{#images.md .file .markdown .numberLines} --- title: Document refers to external images -bindings: b.yaml -functions: f.py +bindings: +- b.yaml +- other.yaml +functions: +- f.py +- other.py bibliography: [foo.bib, bar.bib] ... ![alt text](image.gif) ~~~ +~~~{#other.yaml .file .yaml .numberLines} +[] +~~~ + +~~~{#other.py .file .python .numberLines} +~~~ + ~~~{#foo.bib .file .numberLines} @book{foo2020, author = "James Random", @@ -1827,6 +1843,10 @@ This content is foobarish title: "Subplot" author: The Subplot project date: work in progress -bindings: subplot.yaml -functions: subplot.py +bindings: +- subplot.yaml +- runcmd.yaml +functions: +- subplot.py +- runcmd.py ... diff --git a/templates/bash/template.sh b/templates/bash/template.sh index bc6a51e..286e56f 100644 --- a/templates/bash/template.sh +++ b/templates/bash/template.sh @@ -3,7 +3,7 @@ set -eu -o pipefail ############################################################################# -# Functions that implement steps. From {{ functions_filename }}. +# Functions that implement steps. {{ functions }} diff --git a/templates/python/template.py b/templates/python/template.py index 76d8239..e430b1b 100644 --- a/templates/python/template.py +++ b/templates/python/template.py @@ -1,5 +1,5 @@ ############################################################################# -# Functions that implement steps. From {{ functions_filename }}. +# Functions that implement steps. {{ functions }} |