diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-09-03 10:31:30 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-09-03 10:31:30 +0300 |
commit | 32a3e2a805c2470bd460d74f36f0b49b51453e62 (patch) | |
tree | 7bca0eb8d35d2ec634097551c6df55f9b56ad5b3 /src | |
parent | 7a500b07a7a272c6813b52df954b2e9466a25901 (diff) | |
download | subplot-32a3e2a805c2470bd460d74f36f0b49b51453e62.tar.gz |
refactor: delay use of pandoc_ast::Map to as late as possible
Use YamlMetadata instead. I find this to be clearer and that it
reduces the coupling with pandoc_ast a little. This should help us
when we implement document metadata in a separate YAML file instead of
embedding it in the Markdown.
Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r-- | src/ast.rs | 14 |
1 files changed, 6 insertions, 8 deletions
@@ -25,21 +25,21 @@ lazy_static! { #[derive(Debug)] pub struct AbstractSyntaxTree { blocks: Vec<Block>, - meta: Map<String, MetaValue>, + meta: YamlMetadata, } impl AbstractSyntaxTree { // Create a new AST. // // Note that this is not public. - fn new(meta: Map<String, MetaValue>, blocks: Vec<Block>) -> Self { + fn new(meta: YamlMetadata, blocks: Vec<Block>) -> Self { Self { blocks, meta } } /// Return a Pandoc-compatible AST. pub fn to_pandoc(&self) -> Pandoc { Pandoc { - meta: self.meta.clone(), + meta: self.meta.to_map(), blocks: self.blocks.clone(), pandoc_api_version: vec![1, 20], } @@ -54,18 +54,18 @@ impl std::str::FromStr for AbstractSyntaxTree { trace!("Parsing markdown"); let ast = if let Some((yaml, markdown)) = get_yaml(&LEADING_YAML_PATTERN, markdown) { trace!("Found leading YAML: {:?}", yaml); - let meta = YamlMetadata::new(yaml)?.to_map(); + let meta = YamlMetadata::new(yaml)?; let blocks = parse_blocks(markdown); AbstractSyntaxTree::new(meta, blocks) } else if let Some((yaml, _markdown)) = get_yaml(&TRAILING_YAML_PATTERN, markdown) { trace!("Found trailing YAML: {:?}", yaml); - let meta = YamlMetadata::new(yaml)?.to_map(); + let meta = YamlMetadata::new(yaml)?; let blocks = parse_blocks(markdown); AbstractSyntaxTree::new(meta, blocks) } else { trace!("No YAML to be found"); let blocks = parse_blocks(markdown); - AbstractSyntaxTree::new(Map::new(), blocks) + AbstractSyntaxTree::new(YamlMetadata::default(), blocks) }; trace!("Parsing markdown: OK"); Ok(ast) @@ -404,7 +404,6 @@ mod test { let ast = AbstractSyntaxTree::from_str("").unwrap(); let doc = ast.to_pandoc(); assert!(doc.blocks.is_empty()); - assert!(doc.meta.is_empty()); assert!(!doc.pandoc_api_version.is_empty()); } @@ -419,7 +418,6 @@ mod test { ) .unwrap(); let doc = ast.to_pandoc(); - assert!(doc.meta.is_empty()); assert!(!doc.pandoc_api_version.is_empty()); let attr = ("".to_string(), vec![], vec![]); |