diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-01-14 16:46:09 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-01-14 19:10:21 +1300 |
commit | b0500b6ca074289119d12bd2ec6dcc3e8f6d0c05 (patch) | |
tree | cc9cc1023d9fd6210bae77157ec6fee7d9f42515 /src/collect_files.rs | |
parent | b7983daa81e94de246ad8f209317f483e4206b89 (diff) | |
download | toaster-b0500b6ca074289119d12bd2ec6dcc3e8f6d0c05.zip |
Implement a config file format
If the file `toaster.conf` is found in the root of the source directory,
the contents are parsed as key-value pairs. A line with no indentation
is a key, and all following indented lines are the value.
Diffstat (limited to 'src/collect_files.rs')
-rw-r--r-- | src/collect_files.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/collect_files.rs b/src/collect_files.rs index 49f5cc0..9c7ac24 100644 --- a/src/collect_files.rs +++ b/src/collect_files.rs @@ -2,9 +2,12 @@ use crate::*; use vagabond::*; +use std::collections::HashMap; + pub struct Website { pub name: String, + pub config: HashMap<String, String>, pub pages: Vec<Page>, pub static_files: Vec<StaticItem>, pub static_dirs: Vec<StaticItem>, @@ -56,6 +59,7 @@ impl Website { Ok(entry) => entry.name, Err(err) => error!("Couldn't open {:?}: {:?}", &path, err), }, + config: HashMap::new(), }; new.collect_entry(path, path); return new; @@ -91,6 +95,25 @@ impl Website { self.collect_entry(&child.original_path, prefix); } } + } else if parents.is_empty() && entry.name.to_lowercase() == "toaster.conf" { + // Parse the config file. + let config = std::fs::read_to_string(&source_path).unwrap(); + let mut key = None; + let mut value = String::new(); + for line in config.lines() { + if line.starts_with(" ") { + value.push_str(line.trim()); + value.push('\n'); + } else { + if let Some(key) = key { + self.config.insert(key, std::mem::take(&mut value)); + } + key = Some(line.trim().to_lowercase().to_string()); + } + } + if let Some(key) = key { + self.config.insert(key, std::mem::take(&mut value)); + } } else { match extension.as_str() { "md" => { @@ -230,5 +253,9 @@ impl Website { } return None; } + + pub fn get_config(&self, key: &str) -> String { + self.config.get(key).map(String::to_owned).unwrap_or_else(String::new) + } } |