summaryrefslogtreecommitdiff
path: root/src/doc.rs
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/doc.rs
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/doc.rs')
-rw-r--r--src/doc.rs49
1 files changed, 44 insertions, 5 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