fixed parser bug

This commit is contained in:
gyoder 2025-07-21 12:35:51 -06:00
parent a07d893e6e
commit bfa5d30ae4
4 changed files with 18 additions and 8 deletions

View file

@ -95,5 +95,6 @@ impl Config {
Ok(()) Ok(())
} }
#[must_use]
pub fn config_file_path() -> PathBuf { Self::get_config_path() } pub fn config_file_path() -> PathBuf { Self::get_config_path() }
} }

View file

@ -6,7 +6,7 @@ use std::{
use anyhow::Result; use anyhow::Result;
use kuchikiki::NodeRef; use kuchikiki::NodeRef;
use markup5ever::{ns, LocalName, Namespace, namespace_url, QualName}; use markup5ever::{LocalName, QualName, namespace_url, ns};
use tendril::TendrilSink; use tendril::TendrilSink;

View file

@ -8,7 +8,7 @@ use std::{
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use kuchikiki::{NodeRef, traits::TendrilSink}; use kuchikiki::{NodeRef, traits::TendrilSink};
use markup5ever::{LocalName, Namespace, QualName}; use markup5ever::{LocalName, Namespace, QualName};
use mlua::{Lua, Table}; use mlua::Lua;
use pulldown_cmark::{Options, Parser, html}; use pulldown_cmark::{Options, Parser, html};
use syntect::{ use syntect::{
easy::HighlightLines, easy::HighlightLines,
@ -215,7 +215,7 @@ pub fn generate_footnotes(document: NodeRef) -> Result<NodeRef> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use httptest::{Expectation, ServerPool, matchers::*, responders::*}; use httptest::{Expectation, ServerPool, matchers::*, responders::*};
use markup5ever::{ns, namespace_url}; use markup5ever::{namespace_url, ns};
use super::*; use super::*;
@ -417,7 +417,14 @@ mod tests {
let ctx_name = QualName::new(None, ns!(html), LocalName::from("div")); let ctx_name = QualName::new(None, ns!(html), LocalName::from("div"));
let document = kuchikiki::parse_fragment(ctx_name, Vec::new()).one(page); let document = kuchikiki::parse_fragment(ctx_name, Vec::new()).one(page);
let d = expand_template(document, &p, None).unwrap(); let d = expand_template(document, &p, None).unwrap();
let text = d.select_first("head").unwrap().as_node().select_first("title").unwrap().as_node().text_contents(); let text = d
.select_first("head")
.unwrap()
.as_node()
.select_first("title")
.unwrap()
.as_node()
.text_contents();
assert_eq!(text, "title_test"); assert_eq!(text, "title_test");
let text = d.select_first("#tb").unwrap().as_node().text_contents(); let text = d.select_first("#tb").unwrap().as_node().text_contents();
assert_eq!(text, "body_el"); assert_eq!(text, "body_el");

View file

@ -1,6 +1,6 @@
use std::{path::Path, sync::OnceLock}; use std::{path::Path, sync::OnceLock};
use anyhow::Result; use anyhow::{Result, anyhow};
use crate::{ use crate::{
config::Config, config::Config,
@ -13,7 +13,7 @@ static CONFIG: OnceLock<Config> = OnceLock::new();
pub fn get_config() -> &'static Config { pub fn get_config() -> &'static Config {
CONFIG.get_or_init(|| { CONFIG.get_or_init(|| {
Config::load().unwrap_or_else(|e| { Config::load().unwrap_or_else(|e| {
eprintln!("Warning: Failed to load config: {}", e); eprintln!("Warning: Failed to load config: {e}");
eprintln!("Using default configuration"); eprintln!("Using default configuration");
Config::default() Config::default()
}) })
@ -24,8 +24,10 @@ pub fn serve_content(request_uri: &str) -> Result<String> {
let config = get_config(); let config = get_config();
let safe_path = Path::new(request_uri).strip_prefix("/")?; let safe_path = Path::new(request_uri).strip_prefix("/")?;
let page_path = config.paths.pages.join(safe_path); let page_path = config.paths.pages.join(safe_path);
let doc = read_doc_from_file(page_path)?; let doc = read_doc_from_file(page_path)?
let full_doc = expand_template(doc, &config.paths.components, None)?; .select_first("html")
.map_err(|()| anyhow!("failed to read doc"))?;
let full_doc = expand_template(doc.as_node().to_owned(), &config.paths.components, None)?;
let markdown_doc = process_markdown(full_doc)?; let markdown_doc = process_markdown(full_doc)?;
let highlighted_doc = process_syntax_highlighting(markdown_doc)?; let highlighted_doc = process_syntax_highlighting(markdown_doc)?;
let executed_doc = execute_lua(highlighted_doc)?; let executed_doc = execute_lua(highlighted_doc)?;