From 5a84e1bde773bdc782c1db5a633ef8765f220253 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 30 Oct 2023 18:45:13 +0200 Subject: feat: allow .subplot to list CSS files to embed in HTML output This is in the meta data field "css_embed". Signed-off-by: Lars Wirzenius Sponsored-by: author --- src/doc.rs | 3 +++ src/metadata.rs | 19 +++++++++++++++++++ subplot.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/src/doc.rs b/src/doc.rs index f3f9641..83b5ec2 100644 --- a/src/doc.rs +++ b/src/doc.rs @@ -155,6 +155,9 @@ impl Document { let mut css = Element::new(ElementTag::Style); css.push_child(Content::Text(CSS.into())); + for css_file in self.meta.css_embed() { + css.push_child(Content::Text(css_file.into())); + } head.push_child(Content::Elt(css)); self.meta.set_date(date.into()); diff --git a/src/metadata.rs b/src/metadata.rs index e3463b3..dc28ac4 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -50,6 +50,7 @@ pub struct YamlMetadata { documentclass: Option, #[serde(default)] impls: BTreeMap>, + css_embed: Option>, } impl YamlMetadata { @@ -167,6 +168,7 @@ pub struct Metadata { impls: HashMap, /// Extra class names which should be considered 'correct' for this document classes: Vec, + css_embed: Vec, } #[derive(Debug)] @@ -208,6 +210,17 @@ impl Metadata { vec![] }; + let mut css_embed = vec![]; + if let Some(filenames) = &yaml.css_embed { + for filename in filenames.iter() { + let css = std::fs::read(filename) + .map_err(|e| SubplotError::ReadFile(filename.into(), e))?; + let css = String::from_utf8(css) + .map_err(|e| SubplotError::FileUtf8(filename.into(), e))?; + css_embed.push(css); + } + } + let meta = Self { basedir: basedir.as_ref().to_path_buf(), title: yaml.title().into(), @@ -218,6 +231,7 @@ impl Metadata { bindings, impls, classes, + css_embed, }; trace!("metadata: {:#?}", meta); @@ -278,6 +292,11 @@ impl Metadata { pub fn classes(&self) -> impl Iterator { self.classes.iter().map(Deref::deref) } + + /// Contents of CSS files to embed into the HTML output. + pub fn css_embed(&self) -> impl Iterator { + self.css_embed.iter().map(Deref::deref) + } } impl DocumentImpl { diff --git a/subplot.md b/subplot.md index 06e9e38..6532614 100644 --- a/subplot.md +++ b/subplot.md @@ -3639,6 +3639,51 @@ markdowns: ~~~ ~~~~~~ +## HTML output + +### Embedded CSS + +_Requirement:_ The user can specify CSS files to embed in the HTML +output. + +Justification: We want to allow production of self-standing output +with user-defined styling. + +~~~scenario +given file embedded-css.subplot +given file embedded-css.md +given file embedded-css.css +given file b.yaml +given an installed subplot +when I run subplot docgen embedded-css.subplot -o foo.html +then file foo.html contains "silly: property;" +~~~ + +~~~{#embedded-css.subplot .file .yaml .numberLines} +title: Embedded CSS +markdowns: + - embedded-css.md +bindings: + - b.yaml +css_embed: + - embedded-css.css +~~~ + +~~~~~~{#embedded-css.md .file .markdown .numberLines} +# This is a title + +~~~scenario +given precondition +~~~ +~~~~~~ + +~~~{#embedded-css.css .file .css .numberLines} +html { + silly: property; +} +~~~ + + ## Running Subplot The scenarios in this section verify that the Subplot tool can be run -- cgit v1.2.1