Merge branch 'main' into syntect
This commit is contained in:
commit
262a2e23d8
6 changed files with 115 additions and 15 deletions
|
|
@ -5,7 +5,9 @@ edition = "2024"
|
|||
|
||||
[dependencies]
|
||||
anyhow = "1.0.98"
|
||||
html5ever = "0.35.0"
|
||||
kuchikiki = "0.8.2"
|
||||
markup5ever = "0.11.0"
|
||||
mlua = { version = "0.10.5", features = ["lua54", "vendored", "serialize"] }
|
||||
pulldown-cmark = "0.13.0"
|
||||
syntect = "5.2.0"
|
||||
|
|
|
|||
32
htmlua-parser/src/helpers.rs
Normal file
32
htmlua-parser/src/helpers.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
use std::{fs::File, io::{BufRead, BufReader, Read, Seek, SeekFrom}, path::PathBuf};
|
||||
|
||||
use anyhow::Result;
|
||||
use kuchikiki::NodeRef;
|
||||
use markup5ever::{LocalName, Namespace, QualName};
|
||||
use tendril::TendrilSink;
|
||||
|
||||
|
||||
|
||||
|
||||
pub fn read_doc_from_file(path: PathBuf) -> Result<NodeRef> {
|
||||
|
||||
let mut file = File::open(path)?;
|
||||
let mut reader = BufReader::new(&mut file);
|
||||
|
||||
let mut first_line = String::new();
|
||||
reader.read_line(&mut first_line)?;
|
||||
let first_line_trimmed = first_line.trim();
|
||||
|
||||
let is_whole_doc = first_line_trimmed.eq_ignore_ascii_case("<!DOCTYPE html>");
|
||||
|
||||
file.seek(SeekFrom::Start(0))?;
|
||||
let mut page_string = String::new();
|
||||
file.read_to_string(&mut page_string)?;
|
||||
|
||||
if is_whole_doc {
|
||||
Ok(kuchikiki::parse_html().one(page_string))
|
||||
} else {
|
||||
let ctx_name = QualName::new(None, Namespace::from("http://www.w3.org/1999/xhtml"), LocalName::from("div"));
|
||||
Ok(kuchikiki::parse_fragment(ctx_name, Vec::new()).one(page_string))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
pub mod render;
|
||||
pub mod serve;
|
||||
pub mod helpers;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ use syntect::{
|
|||
util::LinesWithEndings,
|
||||
};
|
||||
|
||||
use crate::helpers::read_doc_from_file;
|
||||
|
||||
fn create_htmlua_stdlib(l: &Lua, stdout: &Rc<RefCell<String>>) -> mlua::Result<Table> {
|
||||
let t = l.create_table()?;
|
||||
|
||||
|
|
@ -106,13 +108,14 @@ 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().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") {
|
||||
i.as_node().detach();
|
||||
}
|
||||
}
|
||||
|
||||
for i in document
|
||||
.select("include")
|
||||
.map_err(|()| anyhow!("Error finding include"))?
|
||||
|
|
@ -124,12 +127,8 @@ pub fn expand_template(document: NodeRef, component_path: &PathBuf, include_from
|
|||
if let Some(include_path) = attrs.get("path") {
|
||||
let mut item_path = component_path.clone();
|
||||
item_path.push(include_path);
|
||||
let component_text = read_to_string(item_path)?;
|
||||
let new_doc = kuchikiki::parse_html().one(component_text);
|
||||
let new_node = new_doc
|
||||
.select_first("body")
|
||||
.map_err(|()| anyhow!("Failed to get body"))?;
|
||||
let replaced_node = expand_template(new_node.as_node().clone(), component_path, Some(i.as_node()))?;
|
||||
let new_node = read_doc_from_file(item_path)?;
|
||||
let replaced_node = expand_template(new_node, component_path, Some(i.as_node()))?;
|
||||
i.as_node().insert_before(replaced_node);
|
||||
i.as_node().detach();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
use crate::helpers::read_doc_from_file;
|
||||
|
||||
use super::render::{execute_lua, expand_template, process_markdown, process_syntax_highlighting};
|
||||
use anyhow::Result;
|
||||
use kuchikiki::traits::TendrilSink;
|
||||
use std::{
|
||||
fs::read_to_string,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::path::{Path, PathBuf}
|
||||
;
|
||||
|
||||
pub fn serve_content(request_uri: &str) -> Result<String> {
|
||||
// TODO: read from config
|
||||
|
|
@ -12,8 +11,9 @@ pub fn serve_content(request_uri: &str) -> Result<String> {
|
|||
let components = PathBuf::from("/var/www/htmlua/components");
|
||||
let safe_path = Path::new(request_uri).strip_prefix("/")?;
|
||||
let page_path = pages.join(safe_path);
|
||||
let page_string = read_to_string(&page_path)?;
|
||||
let doc = kuchikiki::parse_html().one(page_string);
|
||||
|
||||
let doc = read_doc_from_file(page_path)?;
|
||||
|
||||
let full_doc = expand_template(doc, &components, None)?;
|
||||
let markdown_doc = process_markdown(full_doc)?;
|
||||
let highlighted_doc = process_syntax_highlighting(markdown_doc)?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue