diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-10-30 18:45:13 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-10-30 19:08:10 +0200 |
commit | 5a84e1bde773bdc782c1db5a633ef8765f220253 (patch) | |
tree | 93b283272bcc4854cbd96592dd381d09f77cac77 | |
parent | 7252cf5c1e0288b1bbb54908999fc7524d12bf1e (diff) | |
download | subplot-5a84e1bde773bdc782c1db5a633ef8765f220253.tar.gz |
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 <liw@liw.fi>
Sponsored-by: author
-rw-r--r-- | src/doc.rs | 3 | ||||
-rw-r--r-- | src/metadata.rs | 19 | ||||
-rw-r--r-- | subplot.md | 45 |
3 files changed, 67 insertions, 0 deletions
@@ -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<String>, #[serde(default)] impls: BTreeMap<String, Vec<PathBuf>>, + css_embed: Option<Vec<PathBuf>>, } impl YamlMetadata { @@ -167,6 +168,7 @@ pub struct Metadata { impls: HashMap<String, DocumentImpl>, /// Extra class names which should be considered 'correct' for this document classes: Vec<String>, + css_embed: Vec<String>, } #[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<Item = &str> { self.classes.iter().map(Deref::deref) } + + /// Contents of CSS files to embed into the HTML output. + pub fn css_embed(&self) -> impl Iterator<Item = &str> { + self.css_embed.iter().map(Deref::deref) + } } impl DocumentImpl { @@ -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 |