summaryrefslogtreecommitdiff
path: root/src/doc.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/doc.rs')
-rw-r--r--src/doc.rs45
1 files changed, 18 insertions, 27 deletions
diff --git a/src/doc.rs b/src/doc.rs
index 8a70306..38e14b5 100644
--- a/src/doc.rs
+++ b/src/doc.rs
@@ -2,8 +2,8 @@ use crate::ast;
use crate::generate_test_program;
use crate::get_basedir_from;
use crate::visitor;
-use crate::DataFile;
-use crate::DataFiles;
+use crate::EmbeddedFile;
+use crate::EmbeddedFiles;
use crate::LintingVisitor;
use crate::MatchedScenario;
use crate::Metadata;
@@ -20,7 +20,6 @@ use std::default::Default;
use std::fmt::Debug;
use std::ops::Deref;
use std::path::{Path, PathBuf};
-use std::str::FromStr;
use pandoc_ast::{MutVisitor, Pandoc};
@@ -84,7 +83,7 @@ pub struct Document {
markdowns: Vec<PathBuf>,
ast: Pandoc,
meta: Metadata,
- files: DataFiles,
+ files: EmbeddedFiles,
style: Style,
warnings: Warnings,
}
@@ -94,7 +93,7 @@ impl Document {
markdowns: Vec<PathBuf>,
ast: Pandoc,
meta: Metadata,
- files: DataFiles,
+ files: EmbeddedFiles,
style: Style,
) -> Document {
Document {
@@ -130,28 +129,12 @@ impl Document {
// Currently we can't really return more than one error so return one
return Err(linter.issues.remove(0));
}
- let files = DataFiles::new(&mut ast);
+ let files = EmbeddedFiles::new(&mut ast);
let doc = Document::new(markdowns, ast, meta, files, style);
trace!("Loaded from JSON OK");
Ok(doc)
}
- /// Construct a Document from a JSON AST
- pub fn from_json<P>(
- basedir: P,
- markdowns: Vec<PathBuf>,
- json: &str,
- style: Style,
- template: Option<&str>,
- ) -> Result<Document, SubplotError>
- where
- P: AsRef<Path> + Debug,
- {
- trace!("Parsing document...");
- let ast: Pandoc = serde_json::from_str(json).map_err(SubplotError::AstJson)?;
- Self::from_ast(basedir, markdowns, ast, style, template)
- }
-
/// Construct a Document from a named file.
///
/// The file can be in any format Pandoc understands. This runs
@@ -182,13 +165,20 @@ impl Document {
// Add external Pandoc filters.
crate::policy::add_citeproc(&mut pandoc);
- trace!("Invoking Pandoc to parse document {:?}", filename);
- let output = match pandoc.execute().map_err(SubplotError::Pandoc)? {
+ trace!(
+ "Invoking Pandoc to parse document {:?} into AST as JSON",
+ filename
+ );
+ let json = match pandoc.execute().map_err(SubplotError::Pandoc)? {
pandoc::PandocOutput::ToBuffer(o) => o,
_ => return Err(SubplotError::NotJson),
};
trace!("Pandoc was happy");
- let doc = Document::from_json(basedir, markdowns, &output, style, template)?;
+
+ trace!("Parsing document AST as JSON...");
+ let ast: Pandoc = serde_json::from_str(&json).map_err(SubplotError::AstJson)?;
+ let doc = Self::from_ast(basedir, markdowns, ast, style, template)?;
+
trace!("Loaded document OK");
Ok(doc)
}
@@ -208,7 +198,8 @@ impl Document {
let filename = filename.to_path_buf();
let markdown = std::fs::read_to_string(&filename)
.map_err(|err| SubplotError::ReadFile(filename.clone(), err))?;
- let ast = ast::AbstractSyntaxTree::from_str(&markdown)?;
+ let (meta, markdown) = ast::extract_metadata(&markdown)?;
+ let ast = ast::AbstractSyntaxTree::new(meta, markdown);
trace!("Parsed document OK");
Self::from_ast(basedir, vec![filename], ast.to_pandoc(), style, template)
@@ -273,7 +264,7 @@ impl Document {
}
/// Return list of files embeddedin the document.
- pub fn files(&self) -> &[DataFile] {
+ pub fn files(&self) -> &[EmbeddedFile] {
self.files.files()
}