summaryrefslogtreecommitdiff
path: root/src/ast.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/ast.rs')
-rw-r--r--src/ast.rs73
1 files changed, 66 insertions, 7 deletions
diff --git a/src/ast.rs b/src/ast.rs
index 012e05a..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))
}
@@ -970,11 +976,8 @@ pub struct DataFile {
}
impl DataFile {
- fn new(filename: &str, contents: &str) -> DataFile {
- DataFile {
- filename: filename.to_string(),
- contents: contents.to_string(),
- }
+ fn new(filename: String, contents: String) -> DataFile {
+ DataFile { filename, contents }
}
pub fn filename(&self) -> &str {
@@ -1010,8 +1013,18 @@ impl MutVisitor for DataFiles {
match block {
Block::CodeBlock(attr, contents) => {
if is_class(attr, "file") {
- self.files
- .push(DataFile::new(&get_filename(attr), &contents));
+ let add_newline = match find_attr_kv(&attr, "add-newline").next() {
+ None | Some("auto") => !contents.ends_with('\n'),
+ Some("yes") => true,
+ Some("no") => false,
+ _ => unreachable!(),
+ };
+ let contents = if add_newline {
+ format!("{}\n", contents)
+ } else {
+ contents.clone()
+ };
+ self.files.push(DataFile::new(get_filename(attr), contents));
}
}
_ => {
@@ -1070,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
@@ -1081,3 +1129,14 @@ pub fn get_basedir_from(filename: &Path) -> Result<PathBuf> {
};
Ok(dirname)
}
+
+/// Utility function to find key/value pairs from an attribute
+fn find_attr_kv<'a>(attr: &'a Attr, key: &'static str) -> impl Iterator<Item = &'a str> {
+ attr.2.iter().flat_map(move |(key_, value)| {
+ if key == key_ {
+ Some(value.as_ref())
+ } else {
+ None
+ }
+ })
+}