diff options
author | Lars Wirzenius <liw@liw.fi> | 2019-11-30 16:09:14 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2019-11-30 19:22:49 +0200 |
commit | bdf25e5964865b0faef15e1e330e482f2510054b (patch) | |
tree | c184c0ae77bb53765967e06353c7c36844bbc209 /src/typeset.rs | |
parent | 7405a3f6faccfb856d430cfd08988c8dc35893c5 (diff) | |
download | subplot-bdf25e5964865b0faef15e1e330e482f2510054b.tar.gz |
Refactor: move typesetting logic into their own modules
Diffstat (limited to 'src/typeset.rs')
-rw-r--r-- | src/typeset.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/typeset.rs b/src/typeset.rs new file mode 100644 index 0000000..6b83d51 --- /dev/null +++ b/src/typeset.rs @@ -0,0 +1,53 @@ +use pandoc_ast::{Block, Inline}; +use crate::parser::parse_scenario_snippet; + + +/// Typeset an error from dot as a Pandoc AST Block element. +pub fn error(err: Box<dyn std::error::Error>) -> Block { + let msg = format!("ERROR: {}", err.to_string()); + let msg = Inline::Str(msg); + let msg = Inline::Strong(vec![msg]); + Block::Para(vec![msg]) +} + + +/// Typeset a scenario snippet as a Pandoc AST Block. +/// +/// The snippet is given as a text string, which is parsed. It need +/// not be a complete scenario, but it should consist of complete steps. +pub fn scenario_snippet(snippet: &str) -> Block { + let steps = parse_scenario_snippet(snippet) + .map(|s| step(s)) + .collect(); + Block::LineBlock(steps) +} + +// Typeset a single scenario step as a sequence of Pandoc AST Inlines. +fn step(text: &str) -> Vec<Inline> { + let mut words = text.split_whitespace(); + let mut inlines = Vec::new(); + + if let Some(keyword) = words.next() { + typeset_keyword(keyword, &mut inlines); + inlines.push(typeset_str(" ")); + } + + for word in words { + inlines.push(typeset_str(word)); + inlines.push(typeset_str(" ")); + } + inlines +} + +// Typeset first word, which is assumed to be a keyword, of a scenario +// step. +fn typeset_keyword(word: &str, inlines: &mut Vec<Inline>) { + let word = typeset_str(word); + let emph = Inline::Emph(vec![word]); + inlines.push(emph); +} + +// Typeset a single inline string as an Inline element. +fn typeset_str(s: &str) -> Inline { + Inline::Str(s.to_string()) +} |