From e889ebdef94434109e63847e39d0c979569c44c0 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Thu, 21 May 2020 16:31:36 +0100 Subject: feat: Add linting for add-newline attribute Signed-off-by: Daniel Silverstone --- src/ast.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/error.rs | 14 ++++++++++++++ 2 files changed, 55 insertions(+) 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, +} + +impl MutVisitor for LintingVisitor { + fn visit_vec_block(&mut self, vec_block: &mut Vec) { + 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 -- cgit v1.2.1