From 6ca305d000360de74e686d801c638086c876d08a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 20 May 2020 08:25:56 +0300 Subject: feat!: allow multiple bindings and functions files Where previously the document metadata could specify a single bindings file, and a single functions file, it can now specify one or more. If a single value is given, that's the filename. Otherwise, it can be a YAML list of filenames. Drop the functions_filename template variable, which was effectively unused, and for which there is no recorded use case at this time. --- runcmd.py | 0 runcmd.yaml | 1 + src/ast.rs | 70 +++++++++++++++++++++----------------------- src/bin/sp-meta.rs | 11 +++++-- src/codegen.rs | 10 +++---- subplot.md | 28 +++++++++++++++--- templates/bash/template.sh | 2 +- templates/python/template.py | 2 +- 8 files changed, 74 insertions(+), 50 deletions(-) create mode 100644 runcmd.py create mode 100644 runcmd.yaml diff --git a/runcmd.py b/runcmd.py new file mode 100644 index 0000000..e69de29 diff --git a/runcmd.yaml b/runcmd.yaml new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/runcmd.yaml @@ -0,0 +1 @@ +[] diff --git a/src/ast.rs b/src/ast.rs index bd6640b..012e05a 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -143,11 +143,11 @@ impl<'a> Document { pub fn sources(&mut self) -> Vec { 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, - bindings_filename: Option, + bindings_filenames: Vec, bindings: Bindings, - functions_filename: Option, + functions_filenames: Vec, template: Option, bibliographies: Vec, /// 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 { } } -fn get_bindings_filename

(basedir: P, map: &Mapp) -> Result> +fn get_bindings_filenames

(basedir: P, map: &Mapp) -> Vec where P: AsRef, { - 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

(basedir: P, map: &Mapp) -> Result> +fn get_functions_filenames

(basedir: P, map: &Mapp) -> Vec where P: AsRef, { - 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> { @@ -563,10 +554,13 @@ fn get_template_name(map: &Mapp) -> Result> { } } -fn get_path(map: &Mapp, field: &str) -> Option { - match get_string(map, field) { - None => None, - Some(s) => Some(Path::new(&s).to_path_buf()), +fn get_paths

(basedir: P, map: &Mapp, field: &str) -> Vec +where + P: AsRef, +{ + match map.get(field) { + None => vec![], + Some(v) => pathbufs(basedir, v), } } @@ -687,11 +681,13 @@ mod test_join { } } -fn get_bindings

(filename: P, bindings: &mut Bindings) -> Result<()> +fn get_bindings

(filenames: &[P], bindings: &mut Bindings) -> Result<()> where P: AsRef, { - 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.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) } diff --git a/subplot.md b/subplot.md index f0dfa9b..6459e5f 100644 --- a/subplot.md +++ b/subplot.md @@ -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 }} -- cgit v1.2.1