From 700c0ddd79fc6ca01d52250b69b02c1a13d4ddef Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 22 Feb 2026 10:15:06 +1300 Subject: Big rewrite A quick list of everything that's changed: - links to a duplicate heading beneath the same level 1 heading now work - rss feed generation using a .feed file - customisation of the html template using the html.template key - option to use symlinks instead of copying static files - fixed incorrect resolution of internal links - simplified different name forms with the Name type - allow linking to a redirect --- src/config.rs | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/config.rs (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..1933a70 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,92 @@ +use crate::*; + +use highlight::*; + +use std::collections::HashMap; + + +pub struct Config { + pub config: HashMap, + pub last_modified: Option, + pub languages: HashMap, + pub highlighters: Vec, + pub root_redirects: Vec, +} + +impl Config { + pub fn new() -> Self { + Self { + config: HashMap::new(), + last_modified: None, + languages: HashMap::new(), + highlighters: Vec::new(), + root_redirects: Vec::new(), + } + } + + pub fn get(&self, key: &str) -> String { + match self.config.get(key) { + Some(value) => value.to_owned(), + None => String::new(), + } + } + + pub fn parse_file(&mut self, content: &str, last_modified: Option) { + if self.last_modified.is_none() || self.last_modified > last_modified { + self.last_modified = last_modified; + } + + let mut key = None; + let mut value = String::new(); + macro_rules! bank_value { + () => { + value = value.trim().to_string(); + if let Some(key) = key { + if key == "highlighters" { + self.parse_highlighters(&value); + } + self.config.insert(key, std::mem::take(&mut value)); + } + }; + } + for line in content.lines() { + if line.starts_with(" ") || line.trim().is_empty() { + value.push_str(line.trim()); + value.push('\n'); + } else { + bank_value!(); + key = Some(line.trim().to_lowercase().to_string()); + } + } + bank_value!(); + } + + fn parse_highlighters(&mut self, value: &str) { + let mut languages = Vec::new(); + let mut source = String::new(); + macro_rules! bank_sources { + () => { + if !languages.is_empty() { + let i = self.highlighters.len(); + for language in std::mem::take(&mut languages) { + self.languages.insert(language, i); + } + let highlighter = Highlighter::from_str(&std::mem::take(&mut source)); + self.highlighters.push(highlighter); + } + }; + } + for line in value.lines() { + if let Some(line) = line.trim().strip_prefix('[') { + if let Some(line) = line.strip_suffix(']') { + bank_sources!(); + languages = line.split('/').map(|s| s.trim().to_string()).collect(); + continue; + } + } + source.push_str(line); + source.push('\n'); + } + bank_sources!(); + } +} -- cgit v1.2.3-70-g09d2