summaryrefslogtreecommitdiff
path: root/src/metadata.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/metadata.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/metadata.rs')
-rw-r--r--src/metadata.rs26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/metadata.rs b/src/metadata.rs
index 5f5e183..dee0b50 100644
--- a/src/metadata.rs
+++ b/src/metadata.rs
@@ -1,11 +1,11 @@
-use crate::{Bindings, SubplotError, TemplateSpec};
+use crate::{Bindings, SubplotError, TemplateSpec, YamlMetadata};
use std::collections::HashMap;
use std::fmt::Debug;
use std::ops::Deref;
use std::path::{Path, PathBuf};
-use pandoc_ast::{Inline, Map, MetaValue, Pandoc};
+use pandoc_ast::{Inline, Map, MetaValue};
use log::trace;
@@ -14,6 +14,7 @@ use log::trace;
pub struct Metadata {
title: String,
date: Option<String>,
+ markdown_filename: PathBuf,
bindings_filenames: Vec<PathBuf>,
bindings: Bindings,
impls: HashMap<String, DocumentImpl>,
@@ -32,22 +33,23 @@ impl Metadata {
/// Construct a Metadata from a Document, if possible.
pub fn new<P>(
basedir: P,
- doc: &Pandoc,
+ meta: &YamlMetadata,
template: Option<&str>,
) -> Result<Metadata, SubplotError>
where
P: AsRef<Path> + Debug,
{
- let title = get_title(&doc.meta);
- let date = get_date(&doc.meta);
- let bindings_filenames = get_bindings_filenames(&doc.meta);
- let bibliographies = get_bibliographies(basedir.as_ref(), &doc.meta);
- let classes = get_classes(&doc.meta);
+ let map = meta.to_map();
+ let title = get_title(&map);
+ let date = get_date(&map);
+ let bindings_filenames = get_bindings_filenames(&map);
+ let bibliographies = get_bibliographies(basedir.as_ref(), &map);
+ let classes = get_classes(&map);
trace!("Loaded basic metadata");
let mut impls = HashMap::new();
- if let Some(raw_impls) = doc.meta.get("impls") {
+ if let Some(raw_impls) = map.get("impls") {
match raw_impls {
MetaValue::MetaMap(raw_impls) => {
for (impl_name, functions_filenames) in raw_impls.iter() {
@@ -74,6 +76,7 @@ impl Metadata {
Ok(Metadata {
title,
date,
+ markdown_filename: meta.markdown().into(),
bindings_filenames,
bindings,
impls,
@@ -92,6 +95,11 @@ impl Metadata {
self.date.as_deref()
}
+ /// Return filename of the markdown file.
+ pub fn markdown_filename(&self) -> &Path {
+ &self.markdown_filename
+ }
+
/// Return filename where bindings are specified.
pub fn bindings_filenames(&self) -> Vec<&Path> {
self.bindings_filenames.iter().map(|f| f.as_ref()).collect()