diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-05-21 16:17:02 +0100 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-05-24 19:03:40 +0300 |
commit | 5683a8e252f47b45db9cc5d94c5284ff0f4b6fd5 (patch) | |
tree | affa2f3231449d962c3ceeb523760f6292af968c | |
parent | b563adcffaa300bab00a24742d46af2d6f9032b0 (diff) | |
download | subplot-5683a8e252f47b45db9cc5d94c5284ff0f4b6fd5.tar.gz |
feat: support adding newlines to files
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
-rw-r--r-- | src/ast.rs | 32 |
1 files changed, 25 insertions, 7 deletions
@@ -970,11 +970,8 @@ pub struct DataFile { } impl DataFile { - fn new(filename: &str, contents: &str) -> DataFile { - DataFile { - filename: filename.to_string(), - contents: contents.to_string(), - } + fn new(filename: String, contents: String) -> DataFile { + DataFile { filename, contents } } pub fn filename(&self) -> &str { @@ -1010,8 +1007,18 @@ impl MutVisitor for DataFiles { match block { Block::CodeBlock(attr, contents) => { if is_class(attr, "file") { - self.files - .push(DataFile::new(&get_filename(attr), &contents)); + let add_newline = match find_attr_kv(&attr, "add-newline").next() { + None | Some("auto") => !contents.ends_with('\n'), + Some("yes") => true, + Some("no") => false, + _ => unreachable!(), + }; + let contents = if add_newline { + format!("{}\n", contents) + } else { + contents.clone() + }; + self.files.push(DataFile::new(get_filename(attr), contents)); } } _ => { @@ -1081,3 +1088,14 @@ pub fn get_basedir_from(filename: &Path) -> Result<PathBuf> { }; Ok(dirname) } + +/// Utility function to find key/value pairs from an attribute +fn find_attr_kv<'a>(attr: &'a Attr, key: &'static str) -> impl Iterator<Item = &'a str> { + attr.2.iter().flat_map(move |(key_, value)| { + if key == key_ { + Some(value.as_ref()) + } else { + None + } + }) +} |