waawrwrw fuck it
This commit is contained in:
parent
2e1f4b4d1d
commit
7bfc5a7872
|
@ -4,7 +4,6 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
lol_html = "1.2.0"
|
|
||||||
regex = "^1.7"
|
regex = "^1.7"
|
||||||
roxy_core = { git = "https://github.com/kitsunecafe/roxy-core.git" }
|
roxy_core = { git = "https://github.com/kitsunecafe/roxy-core.git" }
|
||||||
|
once_cell = { version = "1.19.0" }
|
||||||
|
|
65
src/lib.rs
65
src/lib.rs
|
@ -1,33 +1,54 @@
|
||||||
use regex::Regex;
|
use std::io::Write;
|
||||||
use lol_html::{element, HtmlRewriter};
|
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use regex::{Captures, Regex};
|
||||||
use roxy_core::roxy::Parse;
|
use roxy_core::roxy::Parse;
|
||||||
|
|
||||||
const EXTENSION_RE: &'static Regex = Regex::new("{% extends \"?.+\"? %}");
|
pub struct MarkdownTeraRewriter;
|
||||||
|
|
||||||
pub struct MarkdownTeraParser<'a, F: FnMut(&'a [u8]) -> ()> {
|
const EXTENSION_RE: Lazy<Regex> = Lazy::new(|| Regex::new("<p>(\\{% extends.+%\\})</p>").unwrap());
|
||||||
rewriter: HtmlRewriter<'a, F>,
|
|
||||||
output: Vec<u8>
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, O: FnMut(&'a [u8]) -> ()> MarkdownTeraParser<'a, O> {
|
impl MarkdownTeraRewriter {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut output = vec![];
|
Self
|
||||||
Self {
|
|
||||||
rewriter: HtmlRewriter::new(lol_html::Settings {
|
|
||||||
element_content_handlers: vec![element!("p", |el| {
|
|
||||||
|
|
||||||
if
|
|
||||||
})],
|
|
||||||
..Default::default()
|
|
||||||
}, |c: &[u8]| output.extend_from_slice(c)),
|
|
||||||
output
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: use an html->dom parser to strip <p> tags around tera placeholders
|
impl Parse for MarkdownTeraRewriter {
|
||||||
impl<'a, O: FnMut(&'a [u8]) -> ()> Parse for MarkdownTeraParser<'a, O> {
|
fn parse(&mut self, _path: &str, src: &[u8], dst: &mut Vec<u8>) -> std::io::Result<()> {
|
||||||
fn parse(&mut self, path: &str, src: &[u8], dst: &mut Vec<u8>) -> std::io::Result<()> {
|
let utf8 = String::from_utf8_lossy(src).to_string();
|
||||||
|
let result = EXTENSION_RE
|
||||||
|
.replace(utf8.as_str(), |captures: &Captures| {
|
||||||
|
format!("{}", &captures[1])
|
||||||
|
})
|
||||||
|
.to_string();
|
||||||
|
dst.write(result.as_bytes())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use roxy_core::roxy::Parse;
|
||||||
|
|
||||||
|
use crate::MarkdownTeraRewriter;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn basic_parse() {
|
||||||
|
let mut parser = MarkdownTeraRewriter::new();
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
|
||||||
|
parser
|
||||||
|
.parse(
|
||||||
|
"test.html",
|
||||||
|
b"<p>{% extends \"base.html\" %}</p><div><p>hi :3</p></div>",
|
||||||
|
&mut buf,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
"{% extends \"base.html\" %}<div><p>hi :3</p></div>",
|
||||||
|
String::from_utf8_lossy(&buf)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue