diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-11-12 10:59:10 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-11-19 20:19:00 +0000 |
commit | 03f4bff558807c3bf6b6ebd65aa1e547b9286461 (patch) | |
tree | 59c8b2ed142f7625916f2610713727f1537d03f2 /src/doc.rs | |
parent | 73e7188b2397d1afb5c550cf8863157c9174a4e5 (diff) | |
download | subplot-03f4bff558807c3bf6b6ebd65aa1e547b9286461.tar.gz |
subplot: Rework for impls not template/functions
As the next step in polyglot documents, this reworks the internals to
expect the metadata of documents to contain an impls mapping from
template name to function filenames for that template. Sadly this does
mean that if there're no function files, the document author will have
to still specify an empty list, but that seems acceptable.
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/doc.rs')
-rw-r--r-- | src/doc.rs | 34 |
1 files changed, 23 insertions, 11 deletions
@@ -73,7 +73,7 @@ static KNOWN_PANDOC_CLASSES: &[&str] = &["numberLines", "noNumberLines"]; /// use subplot; /// let basedir = std::path::Path::new("."); /// let style = subplot::Style::default(); -/// let doc = subplot::Document::from_file(&basedir, filename, style).unwrap(); +/// let doc = subplot::Document::from_file(&basedir, filename, style, None).unwrap(); /// assert_eq!(doc.files(), &[]); /// ~~~~ #[derive(Debug)] @@ -108,11 +108,12 @@ impl<'a> Document { markdowns: Vec<PathBuf>, mut ast: Pandoc, style: Style, + template: Option<&str>, ) -> Result<Document> where P: AsRef<Path> + Debug, { - let meta = Metadata::new(basedir, &ast)?; + let meta = Metadata::new(basedir, &ast, template)?; let mut linter = LintingVisitor::default(); event!(Level::TRACE, "Walking AST for linting..."); linter.walk_pandoc(&mut ast); @@ -133,13 +134,14 @@ impl<'a> Document { markdowns: Vec<PathBuf>, json: &str, style: Style, + template: Option<&str>, ) -> Result<Document> where P: AsRef<Path> + Debug, { event!(Level::TRACE, "Parsing document..."); let ast: Pandoc = serde_json::from_str(json)?; - Self::from_ast(basedir, markdowns, ast, style) + Self::from_ast(basedir, markdowns, ast, style, template) } /// Construct a Document from a named file. @@ -148,7 +150,12 @@ impl<'a> Document { /// Pandoc to parse the file into an AST, so it can be a little /// slow. #[instrument(level = "trace")] - pub fn from_file(basedir: &Path, filename: &Path, style: Style) -> Result<Document> { + pub fn from_file( + basedir: &Path, + filename: &Path, + style: Style, + template: Option<&str>, + ) -> Result<Document> { let markdowns = vec![filename.to_path_buf()]; let mut pandoc = pandoc::new(); @@ -168,7 +175,7 @@ impl<'a> Document { pandoc::PandocOutput::ToBuffer(o) => o, _ => return Err(SubplotError::NotJson), }; - let doc = Document::from_json(basedir, markdowns, &output, style)?; + let doc = Document::from_json(basedir, markdowns, &output, style, template)?; event!(Level::TRACE, "Loaded document OK"); Ok(doc) } @@ -183,6 +190,7 @@ impl<'a> Document { basedir: &Path, filename: &Path, style: Style, + template: Option<&str>, ) -> Result<Document> { event!( Level::TRACE, @@ -194,7 +202,7 @@ impl<'a> Document { let ast = ast::AbstractSyntaxTree::from_str(&markdown)?; event!(Level::TRACE, "Parsed document OK"); - Self::from_ast(basedir, vec![filename], ast.to_pandoc(), style) + Self::from_ast(basedir, vec![filename], ast.to_pandoc(), style, template) } /// Return the AST of a Document, serialized as JSON. @@ -451,7 +459,7 @@ impl<'a> Document { /// /// This version uses Pandoc to parse the Markdown. #[instrument(level = "trace")] -pub fn load_document<P>(filename: P, style: Style) -> Result<Document> +pub fn load_document<P>(filename: P, style: Style, template: Option<&str>) -> Result<Document> where P: AsRef<Path> + Debug, { @@ -466,7 +474,7 @@ where filename.display(), style ); - let doc = Document::from_file(&base_path, filename, style)?; + let doc = Document::from_file(&base_path, filename, style, template)?; event!(Level::TRACE, "Loaded doc from file OK"); Ok(doc) @@ -476,7 +484,11 @@ where /// /// This version uses the `cmark-pullmark` crate to parse Markdown. #[instrument(level = "trace")] -pub fn load_document_with_pullmark<P>(filename: P, style: Style) -> Result<Document> +pub fn load_document_with_pullmark<P>( + filename: P, + style: Style, + template: Option<&str>, +) -> Result<Document> where P: AsRef<Path> + Debug, { @@ -492,7 +504,7 @@ where style ); crate::resource::add_search_path(filename.parent().unwrap()); - let doc = Document::from_file_with_pullmark(&base_path, filename, style)?; + let doc = Document::from_file_with_pullmark(&base_path, filename, style, template)?; event!(Level::TRACE, "Loaded doc from file OK"); Ok(doc) } @@ -502,7 +514,7 @@ pub fn codegen(filename: &Path, output: &Path, template: Option<&str>) -> Result let span = span!(Level::TRACE, "codegen"); let _enter = span.enter(); - let mut doc = load_document_with_pullmark(filename, Style::default())?; + let mut doc = load_document_with_pullmark(filename, Style::default(), template)?; doc.lint()?; let template = template .map(Ok) |