summaryrefslogtreecommitdiff
path: root/src/ast.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-09-03 13:47:16 +0300
committerLars Wirzenius <liw@liw.fi>2022-09-06 07:24:31 +0300
commit765b2e1d4d94b2274de28d4efd24bfe77e8d93ac (patch)
treea7c22c35da5811f19fa02772ecc6498d87d63030 /src/ast.rs
parentc230684f3bab80154d5224d4f2f71eafd00fd100 (diff)
downloadsubplot-765b2e1d4d94b2274de28d4efd24bfe77e8d93ac.tar.gz
feat! read document metadata from a YAML file
This is a huge change all in one commit, sorry. However, as it changes a fundamental part of the command line interface (namely, what constitutes as the input file), there doesn't seem a way to break this into a tidy series of small commits. Most of the diff is in subplot.md, where every scenario that invokes Subplot needs multiple changes, thus touching much of the file. The overall change is that where we previously had document metadata in embedded YAML in the Markdown file, we now have it in a separate YAML file. The Markdown file is named in the YAML file. We still parse the Markdown with Pandoc for everything, except codegen. Switching from Pandoc to pulldown_cmark for parsing will be another big change that I didn't want to include in this huge change set. Sponsored-by: author
Diffstat (limited to 'src/ast.rs')
-rw-r--r--src/ast.rs38
1 files changed, 14 insertions, 24 deletions
diff --git a/src/ast.rs b/src/ast.rs
index f02fdd1..7efe836 100644
--- a/src/ast.rs
+++ b/src/ast.rs
@@ -279,7 +279,7 @@ pub enum Error {
/// block we can work with, in any input file. By being strict here we
/// make it easier to tell the user when a metadata block has, say, a
/// misspelled field.
-#[derive(Debug, Default, Deserialize)]
+#[derive(Debug, Default, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct YamlMetadata {
title: String,
@@ -288,6 +288,7 @@ pub struct YamlMetadata {
date: Option<String>,
classes: Option<Vec<String>>,
bibliography: Option<Vec<PathBuf>>,
+ markdowns: Vec<PathBuf>,
bindings: Option<Vec<PathBuf>>,
documentclass: Option<String>,
#[serde(default)]
@@ -301,7 +302,13 @@ impl YamlMetadata {
Ok(meta)
}
- fn to_map(&self) -> Map<String, MetaValue> {
+ /// Name of file with the Markdown for the subplot document.
+ pub fn markdown(&self) -> &Path {
+ &self.markdowns[0]
+ }
+
+ /// Convert into a pandoc_ast::Map.
+ pub fn to_map(&self) -> Map<String, MetaValue> {
trace!("Creating metadata map from parsed YAML");
let mut map: Map<String, MetaValue> = Map::new();
@@ -367,8 +374,8 @@ fn meta_path_bufs(v: &[PathBuf]) -> MetaValue {
#[cfg(test)]
mod test {
- use super::{extract_metadata, parse_code_block_attrs, AbstractSyntaxTree, YamlMetadata};
- use std::path::PathBuf;
+ use super::{parse_code_block_attrs, YamlMetadata};
+ use std::path::{Path, PathBuf};
#[test]
fn code_block_attrs() {
@@ -392,26 +399,6 @@ mod test {
}
#[test]
- fn parses_leading_meta() {
- let markdown = "\n\n---\ntitle: Foo Bar\n...\nfoobar\n";
- let (meta, markdown) = extract_metadata(markdown).unwrap();
- let ast = AbstractSyntaxTree::new(meta, markdown);
- let doc = ast.to_pandoc();
- let keys: Vec<String> = doc.meta.keys().cloned().collect();
- assert_eq!(keys, ["title"]);
- }
-
- #[test]
- fn parses_trailing_meta() {
- let markdown = "foobar\n---\ntitle: Foo Bar\n...\n\n\n";
- let (meta, markdown) = extract_metadata(markdown).unwrap();
- let ast = AbstractSyntaxTree::new(meta, markdown);
- let doc = ast.to_pandoc();
- let keys: Vec<String> = doc.meta.keys().cloned().collect();
- assert_eq!(keys, ["title"]);
- }
-
- #[test]
fn full_meta() {
let meta = YamlMetadata::new(
"\
@@ -425,6 +412,8 @@ impls:
bibliography:
- foo.bib
- bar.bib
+markdowns:
+- test.md
bindings:
- foo.yaml
- bar.yaml
@@ -438,6 +427,7 @@ bindings:
meta.bibliography.unwrap(),
&[path("foo.bib"), path("bar.bib")]
);
+ assert_eq!(meta.markdowns, vec![Path::new("test.md")]);
assert_eq!(
meta.bindings.unwrap(),
&[path("foo.yaml"), path("bar.yaml")]