i dont remember, look at the git history
This commit is contained in:
parent
2940cf162f
commit
5c33c0c262
68
src/lib.rs
68
src/lib.rs
|
@ -1,12 +1,46 @@
|
|||
use std::io::Write;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::{Captures, Regex};
|
||||
use regex::{Captures, Regex, RegexBuilder};
|
||||
use roxy_core::roxy::Parse;
|
||||
|
||||
pub struct MarkdownTeraRewriter;
|
||||
const PLACEHOLDER_RE: Lazy<Regex> = Lazy::new(|| {
|
||||
RegexBuilder::new(r"^\n{0,2}(\s+)?(\{[\{|%].+?[\}|%]\})\r?\n{0,2}")
|
||||
.multi_line(true)
|
||||
.build()
|
||||
.unwrap()
|
||||
});
|
||||
pub struct MarkdownTeraPreformatter;
|
||||
|
||||
const EXTENSION_RE: Lazy<Regex> = Lazy::new(|| Regex::new("<p>(\\{% extends.+%\\})</p>").unwrap());
|
||||
impl MarkdownTeraPreformatter {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for MarkdownTeraPreformatter {
|
||||
fn parse(&mut self, path: &str, src: &[u8], dst: &mut Vec<u8>) -> std::io::Result<()> {
|
||||
let str = String::from_utf8_lossy(src).to_string();
|
||||
let result = PLACEHOLDER_RE
|
||||
.replace_all(str.as_str(), |captures: &Captures| {
|
||||
format!("\n{}\n", &captures[2])
|
||||
})
|
||||
.trim()
|
||||
.to_string();
|
||||
dst.write_all(result.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
const EXTENSION_RE: Lazy<Regex> = Lazy::new(|| {
|
||||
RegexBuilder::new(r"^<p>(\{[%|\{].+?[%|\}]\}.*?)</p>")
|
||||
.dot_matches_new_line(true)
|
||||
.multi_line(true)
|
||||
.build()
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
pub struct MarkdownTeraRewriter;
|
||||
|
||||
impl MarkdownTeraRewriter {
|
||||
pub fn new() -> Self {
|
||||
|
@ -18,11 +52,12 @@ impl Parse for MarkdownTeraRewriter {
|
|||
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| {
|
||||
.replace_all(utf8.as_str(), |captures: &Captures| {
|
||||
format!("{}", &captures[1])
|
||||
})
|
||||
.replace(""", "\"")
|
||||
.to_string();
|
||||
|
||||
dst.write(result.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -32,7 +67,7 @@ impl Parse for MarkdownTeraRewriter {
|
|||
mod tests {
|
||||
use roxy_core::roxy::Parse;
|
||||
|
||||
use crate::MarkdownTeraRewriter;
|
||||
use crate::{MarkdownTeraPreformatter, MarkdownTeraRewriter};
|
||||
|
||||
#[test]
|
||||
fn basic_parse() {
|
||||
|
@ -42,13 +77,32 @@ mod tests {
|
|||
parser
|
||||
.parse(
|
||||
"test.html",
|
||||
b"<p>{% extends \"base.html\" %}</p><div><p>hi :3</p></div>",
|
||||
b"<p>{% extends \"base.html\" %}\n{% block content %}</p>\n<div><p>hi :3</p></div>{% endblock content %}",
|
||||
&mut buf,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
"{% extends \"base.html\" %}<div><p>hi :3</p></div>",
|
||||
"{% extends \"base.html\" %}\n{% block content %}\n<div><p>hi :3</p></div>{% endblock content %}",
|
||||
String::from_utf8_lossy(&buf)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preformatter() {
|
||||
let mut parser = MarkdownTeraPreformatter::new();
|
||||
let mut buf = Vec::new();
|
||||
|
||||
parser
|
||||
.parse(
|
||||
"test.html",
|
||||
b"{% extends \"base.html\" %}\n{% block content %}\n# hi :3\n{% endblock content %}",
|
||||
&mut buf,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
"{% extends \"base.html\" %}\n\n{% block content %}\n# hi :3\n\n{% endblock content %}",
|
||||
String::from_utf8_lossy(&buf)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue