Skip to content

Commit

Permalink
Merge extra config of site and theme.
Browse files Browse the repository at this point in the history
  • Loading branch information
ibz committed Aug 22, 2024
1 parent 7e44f3d commit dc829fc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod sass;
mod site;
mod template;
mod theme;
mod utils;

use site::Site;
use theme::Theme;
Expand Down
22 changes: 19 additions & 3 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
content, nostr,
resource::{ContentSource, Resource, ResourceKind},
template,
utils::merge,
};

#[derive(Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -71,6 +72,16 @@ impl SiteConfig {
format!("{}/{}{}", self.base_url, path, trailing_bit)
}
}

pub fn merge(&mut self, other: &SiteConfig) {
for (key, value) in &other.extra {
if !self.extra.contains_key(key) {
self.extra.insert(key.to_owned(), value.clone());
continue;
}
merge(self.extra.get_mut(key).unwrap(), value).unwrap();
}
}
}

fn load_templates(site_config: &SiteConfig) -> tera::Tera {
Expand Down Expand Up @@ -448,9 +459,9 @@ pub fn load_sites() -> HashMap<String, Site> {
let mut config = config.unwrap();

let theme_path = format!("./themes/{}", config.theme.as_ref().unwrap());
let theme_config = load_config(&&format!("{}/config.toml", theme_path));
let theme_config = load_config(&format!("{}/config.toml", theme_path)).unwrap();

config.extra = theme_config.unwrap().extra; // TODO: merge rather than overwrite!
config.merge(&theme_config);

let tera = load_templates(&config);

Expand Down Expand Up @@ -488,7 +499,12 @@ pub fn create_site(domain: &str, admin_pubkey: Option<String>) -> Site {
);
fs::write(format!("./sites/{}/_config.toml", domain), &config_content).unwrap();

let config = load_config(&format!("{}/_config.toml", path)).unwrap();
let mut config = load_config(&format!("{}/_config.toml", path)).unwrap();

let theme_path = format!("./themes/{}", config.theme.as_ref().unwrap());
let theme_config = load_config(&format!("{}/config.toml", theme_path)).unwrap();

config.merge(&theme_config);

let tera = load_templates(&config);

Expand Down
33 changes: 33 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// * Code taken from [Zola](https://www.getzola.org/) and adapted.
// * Zola's MIT license applies. See: https://github.com/getzola/zola/blob/master/LICENSE

use toml::Value as TomlValue;

#[derive(Debug)]
pub struct MergeError;

// https://github.com/getzola/zola/blob/master/components/config/src/config/mod.rs

pub fn merge(into: &mut TomlValue, from: &TomlValue) -> Result<(), MergeError> {
match (from.is_table(), into.is_table()) {
(false, false) => {
// These are not tables so we have nothing to merge
Ok(())
}
(true, true) => {
// Recursively merge these tables
let into_table = into.as_table_mut().unwrap();
for (key, val) in from.as_table().unwrap() {
if !into_table.contains_key(key) {
// An entry was missing in the first table, insert it
into_table.insert(key.to_string(), val.clone());
continue;
}
// Two entries to compare, recurse
merge(into_table.get_mut(key).unwrap(), val)?;
}
Ok(())
}
_ => Err(MergeError),
}
}

0 comments on commit dc829fc

Please sign in to comment.