summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2023-05-10 07:42:52 +0300
committerLars Wirzenius <liw@liw.fi>2023-05-10 08:26:11 +0300
commit0a81c16df535853c68d5c833a213a81c2a1a9388 (patch)
tree6bae05da8feb06162cab9351918858c50dd1e77a /src
parent37c46ae17cb3f75a4b34c9e2104110c20fb2cb59 (diff)
downloadsubplot-0a81c16df535853c68d5c833a213a81c2a1a9388.tar.gz
refactor: move document metadata out of Markdown struct
It's _document_ metadata, not metadata for a specific markdown file. It belongs in Document, not Markdown. Make it so. Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r--src/doc.rs49
-rw-r--r--src/md.rs62
-rw-r--r--src/metadata.rs7
3 files changed, 56 insertions, 62 deletions
diff --git a/src/doc.rs b/src/doc.rs
index 459b0e7..0b0b9e7 100644
--- a/src/doc.rs
+++ b/src/doc.rs
@@ -1,8 +1,8 @@
use crate::bindings::CaptureType;
use crate::generate_test_program;
use crate::get_basedir_from;
-use crate::html::Element;
use crate::html::HtmlPage;
+use crate::html::{Content, Element, ElementTag};
use crate::md::Markdown;
use crate::EmbeddedFile;
use crate::EmbeddedFiles;
@@ -128,8 +128,7 @@ impl Document {
let mdfile = meta.markdown();
let mdfile = basedir.join(mdfile);
- let mut md = Markdown::load_file(mdfile.as_path())?;
- md.set_metadata(&meta);
+ let md = Markdown::load_file(mdfile.as_path())?;
let doc = Self::from_ast(basedir, filename.into(), &meta, md, style, template)?;
@@ -144,12 +143,52 @@ impl Document {
title.push_child(crate::html::Content::Text(self.meta().title().into()));
head.push_child(crate::html::Content::Elt(title));
- self.md.set_date(date.into());
+ self.meta.set_date(date.into());
- let page = HtmlPage::new(head, self.md.to_html());
+ let mut body = self.typeset_meta();
+ body.push_child(Content::Elt(self.md.root_element().clone()));
+ let page = HtmlPage::new(head, body);
page.serialize().unwrap() // FIXME
}
+ fn typeset_meta(&self) -> Element {
+ let mut div = Element::new(ElementTag::Div);
+ div.push_child(Content::Elt(Self::title(self.meta.title())));
+ if let Some(authors) = self.meta.authors() {
+ div.push_child(Content::Elt(Self::authors(authors)));
+ }
+ if let Some(date) = self.meta.date() {
+ div.push_child(Content::Elt(Self::date(date)));
+ }
+ div
+ }
+
+ fn title(title: &str) -> Element {
+ let mut e = Element::new(ElementTag::H1);
+ e.push_child(Content::Text(title.into()));
+ e
+ }
+
+ fn authors(authors: &[String]) -> Element {
+ let mut list = Element::new(ElementTag::P);
+ list.push_child(Content::Text("By: ".into()));
+ let mut first = true;
+ for a in authors {
+ if !first {
+ list.push_child(Content::Text(", ".into()));
+ }
+ list.push_child(Content::Text(a.into()));
+ first = false;
+ }
+ list
+ }
+
+ fn date(date: &str) -> Element {
+ let mut e = Element::new(ElementTag::P);
+ e.push_child(Content::Text(date.into()));
+ e
+ }
+
/// Return the document's metadata.
pub fn meta(&self) -> &Metadata {
&self.meta
diff --git a/src/md.rs b/src/md.rs
index 9ad30bc..5e56198 100644
--- a/src/md.rs
+++ b/src/md.rs
@@ -3,7 +3,7 @@
use crate::{
html::{parse, Attribute, Content, Element, ElementTag},
parse_scenario_snippet, Bindings, EmbeddedFile, EmbeddedFiles, Scenario, ScenarioStep, Style,
- SubplotError, Warnings, YamlMetadata,
+ SubplotError, Warnings,
};
use log::trace;
use std::collections::HashSet;
@@ -13,7 +13,6 @@ use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct Markdown {
html: Element,
- meta: Option<YamlMetadata>,
}
impl Markdown {
@@ -32,63 +31,12 @@ impl Markdown {
}
fn new(html: Element) -> Self {
- Self { html, meta: None }
+ Self { html }
}
- /// Set document metadata from subplot.
- pub fn set_metadata(&mut self, meta: &YamlMetadata) {
- self.meta = Some(meta.clone());
- }
-
- /// Set date.
- pub fn set_date(&mut self, date: String) {
- if let Some(meta) = &mut self.meta {
- meta.set_date(date);
- }
- }
-
- /// Return parsed HTML of the markdown.
- pub fn to_html(&self) -> Element {
- if let Some(meta) = &self.meta {
- let mut div = Element::new(ElementTag::Div);
- div.push_child(Content::Elt(Self::title(meta.title())));
- if let Some(authors) = meta.authors() {
- div.push_child(Content::Elt(Self::authors(authors)));
- }
- if let Some(date) = meta.date() {
- div.push_child(Content::Elt(Self::date(date)));
- }
- div.push_child(Content::Elt(self.html.clone()));
- div
- } else {
- self.html.clone()
- }
- }
-
- fn title(title: &str) -> Element {
- let mut e = Element::new(ElementTag::H1);
- e.push_child(Content::Text(title.into()));
- e
- }
-
- fn authors(authors: &[String]) -> Element {
- let mut list = Element::new(ElementTag::P);
- list.push_child(Content::Text("By: ".into()));
- let mut first = true;
- for a in authors {
- if !first {
- list.push_child(Content::Text(", ".into()));
- }
- list.push_child(Content::Text(a.into()));
- first = false;
- }
- list
- }
-
- fn date(date: &str) -> Element {
- let mut e = Element::new(ElementTag::P);
- e.push_child(Content::Text(date.into()));
- e
+ /// Return root element of markdown.
+ pub fn root_element(&self) -> &Element {
+ &self.html
}
/// Find included images.
diff --git a/src/metadata.rs b/src/metadata.rs
index 9f25621..d569b40 100644
--- a/src/metadata.rs
+++ b/src/metadata.rs
@@ -176,6 +176,7 @@ pub struct Metadata {
basedir: PathBuf,
title: String,
date: Option<String>,
+ authors: Option<Vec<String>>,
markdown_filename: PathBuf,
bindings_filenames: Vec<PathBuf>,
bindings: Bindings,
@@ -234,6 +235,7 @@ impl Metadata {
basedir: basedir.as_ref().to_path_buf(),
title: yaml.title().into(),
date: yaml.date().map(|s| s.into()),
+ authors: yaml.authors().map(|a| a.into()),
markdown_filename: yaml.markdown().into(),
bindings_filenames,
bindings,
@@ -261,6 +263,11 @@ impl Metadata {
self.date = Some(date);
}
+ /// Authors.
+ pub fn authors(&self) -> Option<&[String]> {
+ self.authors.as_deref()
+ }
+
/// Return base dir for all relative filenames.
pub fn basedir(&self) -> &Path {
&self.basedir