summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2020-05-21 16:31:36 +0100
committerLars Wirzenius <liw@liw.fi>2020-05-24 19:03:40 +0300
commite889ebdef94434109e63847e39d0c979569c44c0 (patch)
treea597852b99eba92d40e996646ce47e251a4f5917
parent5683a8e252f47b45db9cc5d94c5284ff0f4b6fd5 (diff)
downloadsubplot-e889ebdef94434109e63847e39d0c979569c44c0.tar.gz
feat: Add linting for add-newline attribute
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
-rw-r--r--src/ast.rs41
-rw-r--r--src/error.rs14
2 files changed, 55 insertions, 0 deletions
diff --git a/src/ast.rs b/src/ast.rs
index f627178..bc3547d 100644
--- a/src/ast.rs
+++ b/src/ast.rs
@@ -89,6 +89,12 @@ impl<'a> Document {
{
let mut ast: Pandoc = serde_json::from_str(json)?;
let meta = Metadata::new(basedir, &ast)?;
+ let mut linter = LintingVisitor::default();
+ linter.walk_pandoc(&mut ast);
+ if !linter.issues.is_empty() {
+ // 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);
Ok(Document::new(markdowns, ast, meta, files))
}
@@ -1077,6 +1083,41 @@ impl MutVisitor for BlockClassVisitor {
}
}
+#[derive(Default)]
+struct LintingVisitor {
+ issues: Vec<SubplotError>,
+}
+
+impl MutVisitor for LintingVisitor {
+ fn visit_vec_block(&mut self, vec_block: &mut Vec<Block>) {
+ for block in vec_block {
+ match block {
+ Block::CodeBlock(attr, _) => {
+ if is_class(attr, "file") {
+ let newlines: Vec<_> = find_attr_kv(&attr, "add-newline").collect();
+ match newlines.len() {
+ 0 => {}
+ 1 => match newlines[0].to_ascii_lowercase().as_ref() {
+ "auto" | "yes" | "no" => {}
+ _ => self.issues.push(SubplotError::UnrecognisedAddNewline(
+ get_filename(&attr),
+ newlines[0].to_owned(),
+ )),
+ },
+ _ => self.issues.push(SubplotError::RepeatedAddNewlineAttribute(
+ get_filename(&attr),
+ )),
+ }
+ }
+ }
+ _ => {
+ self.visit_block(block);
+ }
+ }
+ }
+ }
+}
+
/// Get the base directory given the name of the markdown file.
///
/// All relative filename, such as bindings files, are resolved
diff --git a/src/error.rs b/src/error.rs
index 60c850e..0057f37 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -126,6 +126,20 @@ pub enum SubplotError {
#[error("Duplicate embedded file name: {0}")]
DuplicateEmbeddedFilename(String),
+ /// Embedded file has more than one `add-newline` attribute
+ ///
+ /// The `add-newline` attribute can only be specified once for any given
+ /// embedded file
+ #[error("Embedded file {0} has more than one `add-newline` attribute")]
+ RepeatedAddNewlineAttribute(String),
+
+ /// Unrecognised `add-newline` attribute value on an embedded file
+ ///
+ /// The `add-newline` attribute can only take the values `auto`, `yes`,
+ /// and `no`.
+ #[error("Embedded file {0} has unrecognised `add-newline={}` - valid values are auto/yes/no")]
+ UnrecognisedAddNewline(String, String),
+
/// Couldn't determine base directory from input file name.
///
/// Subplot needs to to determine the base directory for files