Basic toml config support.

This commit is contained in:
Quin 2025-07-11 16:55:49 -06:00
parent c8ffbb2af0
commit 38be7b7365
7 changed files with 274 additions and 31 deletions

View file

@ -1,4 +1,4 @@
use std::{cell::RefCell, fmt::Write, fs::read_to_string, path::PathBuf, rc::Rc};
use std::{cell::RefCell, fmt::Write, path::PathBuf, rc::Rc};
use anyhow::{Result, anyhow};
use kuchikiki::{NodeRef, traits::TendrilSink};
@ -12,7 +12,7 @@ use syntect::{
util::LinesWithEndings,
};
use crate::helpers::read_doc_from_file;
use crate::{helpers::read_doc_from_file, serve::get_config};
fn create_htmlua_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<Table> {
let t = l.create_table()?;
@ -108,7 +108,11 @@ pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from
let exported = from_node
.select_first(format!("exportelement.{name}").as_str())
.map_err(|()| anyhow!("Error finding exportelement"))?;
exported.as_node().children().rev().for_each(|c| i.as_node().insert_after(c));
exported
.as_node()
.children()
.rev()
.for_each(|c| i.as_node().insert_after(c));
}
}
while let Ok(i) = document.select_first("includeelement") {
@ -130,8 +134,13 @@ pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from
item_path.push(include_path);
let new_node = read_doc_from_file(item_path)?;
let replaced_node = expand_template(new_node, component_path, Some(i.as_node()))?;
replaced_node.select_first("html").map_err(|()| anyhow!("Error finding html"))?.as_node().children().rev().for_each(|c| i.as_node().insert_after(c));
replaced_node
.select_first("html")
.map_err(|()| anyhow!("Error finding html"))?
.as_node()
.children()
.rev()
.for_each(|c| i.as_node().insert_after(c));
}
}
while let Ok(i) = document.select_first("include") {
@ -141,16 +150,15 @@ pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from
}
pub fn process_syntax_highlighting(document: NodeRef) -> Result<NodeRef> {
let config = get_config();
let ps = SyntaxSet::load_defaults_newlines();
let mut ts = ThemeSet::load_defaults();
let syntax_elements: Vec<_> = match document.select("syntaxhighlight") {
Ok(e) => e.collect(),
Err(()) => return Err(anyhow!("Unable to find syntaxhighlight elements")),
};
if !syntax_elements.is_empty() {
// TODO: read from config
let themes = PathBuf::from("/var/www/htmlua/themes");
let _ = ts.add_from_folder(themes);
if !syntax_elements.is_empty() && config.syntax_highlighting.load_custom_themes {
let _ = ts.add_from_folder(&config.paths.themes);
}
for node in syntax_elements {
let attrs = match node.as_node().as_element() {
@ -158,7 +166,7 @@ pub fn process_syntax_highlighting(document: NodeRef) -> Result<NodeRef> {
None => continue,
};
let language = attrs.get("lang").unwrap_or("text");
let theme_name = attrs.get("theme").unwrap_or("base16-ocean.dark");
let theme_name = attrs.get("theme").unwrap_or(&config.syntax_highlighting.default_theme);
if let Some(text_node) = node.as_node().first_child() {
if let Some(code_text) = text_node.as_text() {
let syntax = ps