diff options
-rw-r--r-- | src/ast.rs | 36 | ||||
-rw-r--r-- | subplot.md | 26 |
2 files changed, 61 insertions, 1 deletions
@@ -4,7 +4,8 @@ use pandoc_ast::{Attr, Block, Inline, Map, MetaValue, Pandoc}; use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag}; use regex::Regex; use serde::Deserialize; -use std::collections::BTreeMap; +use serde_yaml::{Mapping, Value}; +use std::collections::{BTreeMap, HashMap}; use std::path::{Path, PathBuf}; lazy_static! { @@ -293,6 +294,7 @@ pub struct YamlMetadata { documentclass: Option<String>, #[serde(default)] impls: BTreeMap<String, Vec<PathBuf>>, + pandoc: Option<HashMap<String, Value>>, } impl YamlMetadata { @@ -351,11 +353,43 @@ impl YamlMetadata { map.insert("documentclass".into(), meta_string(v)); } + if let Some(pandoc) = &self.pandoc { + for (key, value) in pandoc.iter() { + map.insert(key.to_string(), value_to_pandoc(value)); + } + } + trace!("Created metadata map from parsed YAML"); map } } +fn mapping_to_pandoc(mapping: &Mapping) -> MetaValue { + let mut map = Map::new(); + for (key, value) in mapping.iter() { + let key = if let MetaValue::MetaString(s) = value_to_pandoc(&key) { + s + } else { + panic!("key not a string: {:?}", key); + }; + map.insert(key, Box::new(value_to_pandoc(value))); + } + + MetaValue::MetaMap(map) +} + +fn value_to_pandoc(data: &Value) -> MetaValue { + match data { + Value::Null => unreachable!("null not OK"), + Value::Number(_) => unreachable!("number not OK"), + Value::Sequence(_) => unreachable!("sequence not OK"), + + Value::Bool(b) => MetaValue::MetaBool(*b), + Value::String(s) => MetaValue::MetaString(s.clone()), + Value::Mapping(mapping) => mapping_to_pandoc(mapping), + } +} + fn meta_string(s: &str) -> MetaValue { MetaValue::MetaString(s.to_string()) } @@ -2474,6 +2474,32 @@ and file mtime.html contains "Geoffrey Butler" and file mtime.html contains "2020-02-26 07:53" ~~~ +### Pandoc metadata + +~~~scenario +given file pandoc.subplot +given file pandoc.md +and an installed subplot +when I run subplot docgen pandoc.subplot -o pandoc.html +when I run cat pandoc.html +then file pandoc.html exists +and file pandoc.html contains "<title>The Fabulous Title</title>" +and file pandoc.html contains "Superlative Subtitle" +~~~ + +~~~{#pandoc.subplot .file .yaml .numberLines} +title: The Fabulous Title +markdowns: +- pandoc.md +pandoc: + subtitle: Superlative Subtitle +~~~ + +~~~{#pandoc.md .file .markdown .numberLines} +# Introduction +This is a test document. That's all. +~~~ + ### Missing bindings file If a bindings file is missing, the error message should name the |