better component templates
This commit is contained in:
6
Cargo.lock
generated
6
Cargo.lock
generated
@@ -227,8 +227,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "pulldown-cmark"
|
name = "pulldown-cmark"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "git+https://git.gay/LailaTheElf/pulldown-cmark.git?branch=feature%2FeventTree#2300d0db6541279c41928b44d12eac510ecc3199"
|
||||||
checksum = "1e8bbe1a966bd2f362681a44f6edce3c2310ac21e4d5067a6e7ec396297a6ea0"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"getopts",
|
"getopts",
|
||||||
@@ -241,8 +240,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "pulldown-cmark-escape"
|
name = "pulldown-cmark-escape"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "git+https://git.gay/LailaTheElf/pulldown-cmark.git?branch=feature%2FeventTree#2300d0db6541279c41928b44d12eac510ecc3199"
|
||||||
checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ edition = "2024"
|
|||||||
clap = { version = "4.5.55", features = ["derive"] }
|
clap = { version = "4.5.55", features = ["derive"] }
|
||||||
hashlink = "0.10.0"
|
hashlink = "0.10.0"
|
||||||
minijinja = { version = "2.10.2", features = ["loader", "builtins", "json"] }
|
minijinja = { version = "2.10.2", features = ["loader", "builtins", "json"] }
|
||||||
pulldown-cmark = { version = "0.13.0", features = ["serde"] }
|
|
||||||
regex = "1.11.1"
|
regex = "1.11.1"
|
||||||
yaml-rust2 = "0.10.2"
|
yaml-rust2 = "0.10.2"
|
||||||
|
|
||||||
|
[dependencies.pulldown-cmark]
|
||||||
|
git = "https://git.gay/LailaTheElf/pulldown-cmark.git"
|
||||||
|
branch = "feature/eventTree"
|
||||||
|
features = ["serde"]
|
||||||
|
|||||||
111
src/main.rs
111
src/main.rs
@@ -1,96 +1,65 @@
|
|||||||
use std::{path::Path, fs};
|
use std::{convert::Infallible, fs, path::Path};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use minijinja::Environment;
|
|
||||||
|
|
||||||
mod render;
|
mod render;
|
||||||
use render::{Renderer, IndexItem, build_jinja_env, Template};
|
use render::{Renderer, IndexItem};
|
||||||
|
|
||||||
mod version;
|
mod version;
|
||||||
use version::VERSION;
|
use version::VERSION;
|
||||||
|
|
||||||
fn render_index(
|
fn render_page(
|
||||||
|
page: &IndexItem,
|
||||||
out_path: &Path,
|
out_path: &Path,
|
||||||
index: &IndexItem,
|
index: &Renderer,
|
||||||
cur_path: &Path,
|
cur_path: &Path,
|
||||||
site_index: &Renderer,
|
|
||||||
templates: Option<Vec<Box<Template>>>,
|
|
||||||
jinja_env: Option<&Environment>
|
|
||||||
) {
|
) {
|
||||||
let dest_path: &Path = &cur_path.join(&index.friendly);
|
let friendly = match page.friendly.len() {
|
||||||
// println!("dest_path: {:?}", dest_path);
|
0 => "index",
|
||||||
|
_ => &page.friendly
|
||||||
let templates = match templates {
|
|
||||||
Some(template) => template,
|
|
||||||
None => Template::index(&site_index.path.join("templates")).unwrap(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let jinja_env = match jinja_env {
|
|
||||||
Some(env) => env,
|
|
||||||
None => &build_jinja_env(templates.clone()),
|
|
||||||
};
|
|
||||||
|
|
||||||
match &index.src {
|
|
||||||
Some(src) => {
|
|
||||||
// a source file is available. try to render it
|
|
||||||
|
|
||||||
let friendly = match index.friendly.len() {
|
|
||||||
0 => String::from("index"),
|
|
||||||
_ => index.friendly.clone()
|
|
||||||
};
|
|
||||||
|
|
||||||
if !index.is_asset {
|
|
||||||
// find template
|
|
||||||
let templ: Option<Template> = {
|
|
||||||
let mut out = None;
|
|
||||||
for templ in templates.clone() {
|
|
||||||
if let Some(templ) = templ.search(index.template.clone()) {
|
|
||||||
out = Some(templ);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if let Some(src) = &page.src {
|
||||||
|
if !page.is_asset {
|
||||||
// get output file extention
|
// get output file extention
|
||||||
let extention = match index.target_extention.len() {
|
let extention = match page.target_extention.len() {
|
||||||
0 => match templ {
|
0 => String::from("html"),
|
||||||
Some(templ) => templ.extention,
|
_ => page.target_extention.clone()
|
||||||
None => String::from("html")
|
|
||||||
}
|
|
||||||
_ => index.target_extention.clone()
|
|
||||||
};
|
};
|
||||||
let dest_path: &Path = &cur_path.join(format!("{}.{}", friendly, extention));
|
let dest_path: &Path = &cur_path.join(format!("{}.{}", friendly, extention));
|
||||||
// render the content
|
// render the content
|
||||||
let dest_path_str = dest_path.to_str();
|
println!("INFO: render {}", dest_path.to_str().unwrap());
|
||||||
if dest_path_str == None {
|
match index.render_page(page) {
|
||||||
println!("ERROR: dest path is None for page: {}", friendly);
|
Ok(content) => {
|
||||||
return;
|
let _: Result<(), Infallible> = fs::write(dest_path, content).or_else(|e|{
|
||||||
}
|
println!("ERROR during writing: {:?}", e);
|
||||||
println!("INFO: render {}", dest_path_str.unwrap());
|
Ok(())
|
||||||
let content = site_index.render_page(index, &site_index.site, jinja_env).unwrap();
|
});
|
||||||
fs::write(dest_path, content).unwrap();
|
},
|
||||||
|
Err(e) => {
|
||||||
|
println!("ERROR during rendering: {:?}", e);
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// file is an asset, no rendering done for assets
|
// file is an asset, no rendering done for assets
|
||||||
let dest_path: &Path = &cur_path.join(friendly.clone());
|
let dest_path: &Path = &cur_path.join(friendly);
|
||||||
match dest_path.to_str() {
|
match dest_path.to_str() {
|
||||||
Some(dest_path_str) => {
|
Some(dest_path_str) => {
|
||||||
println!("INFO: copy {}", dest_path_str);
|
println!("INFO: copy {}", dest_path_str);
|
||||||
let _ = fs::copy(src, dest_path);
|
let _: Result<u64, Infallible> = fs::copy(src, dest_path).or_else(|e|{
|
||||||
|
println!("ERROR during copying: {:?}", e);
|
||||||
|
Ok(0)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
println!("ERROR: dest path is None for asset: {}", friendly);
|
println!("ERROR: dest path is None for asset: {}", friendly);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
None => {
|
|
||||||
let _ = fs::create_dir(dest_path);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
for page in &index.sub_pages {
|
for page in page.sub_pages.iter() {
|
||||||
render_index(out_path, page, dest_path, &site_index, Some(templates.clone()), Some(&jinja_env));
|
render_page(page, out_path, &index, cur_path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,16 +90,16 @@ fn main() {
|
|||||||
let src_path = Path::new(&args.source);
|
let src_path = Path::new(&args.source);
|
||||||
let out_path = Path::new(&args.output);
|
let out_path = Path::new(&args.output);
|
||||||
|
|
||||||
// scan all pages, template, components, etc.
|
// scan all pages adn templates
|
||||||
let index = match Renderer::index(src_path).unwrap() {
|
let index = match Renderer::index(src_path).or_else::<Infallible, _>(|e| {
|
||||||
|
panic!("PANIC: faild to index: {:?}", e);
|
||||||
|
}).unwrap() {
|
||||||
Some(index) => index,
|
Some(index) => index,
|
||||||
None => {
|
None => {
|
||||||
println!("ERROR: src directory not found ({})", src_path.to_str().or(Some("Utf parse error")).unwrap());
|
panic!("ERROR: src directory not found ({})", src_path.to_str().or(Some("Utf parse error")).unwrap());
|
||||||
return;
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// render website
|
// render pages
|
||||||
let _ = fs::create_dir_all(out_path);
|
render_page(&index.site, out_path, &index, out_path);
|
||||||
render_index(out_path, &index.site, out_path, &index, None, None);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,63 +1,42 @@
|
|||||||
mod index;
|
mod index;
|
||||||
mod parse_md;
|
mod parse_md;
|
||||||
|
|
||||||
use std::{collections::BTreeMap, fs, path::Path, io::Error as IoError};
|
use std::{collections::BTreeMap, fs, io::Error as IoError, path::Path};
|
||||||
use minijinja::{context, Environment, Value, Error as JinjaError};
|
use minijinja::{Environment, Error as JinjaError, Value, context};
|
||||||
use index::indexer;
|
use index::indexer::Error as IndexError;
|
||||||
use parse_md::parse_md;
|
|
||||||
use yaml_rust2::Yaml;
|
use yaml_rust2::Yaml;
|
||||||
|
use crate::render::{index::indexer::split_params, parse_md::{Parser, Error as ParserError}};
|
||||||
|
|
||||||
pub use index::indexer::{IndexItem, Template};
|
pub use index::indexer::{IndexItem, Template};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
NoSrc,
|
NoSrc,
|
||||||
Io(IoError),
|
Io(IoError),
|
||||||
Jinja(JinjaError)
|
Jinja(JinjaError),
|
||||||
}
|
Indexer(IndexError),
|
||||||
|
Parser(ParserError),
|
||||||
pub fn build_jinja_env<'a>(templates: Vec<Box<indexer::Template>>) -> Environment<'a> {
|
|
||||||
build_jinja_env_dir(templates)
|
|
||||||
}
|
|
||||||
fn build_jinja_env_dir<'a>(templates: Vec<Box<indexer::Template>>) -> Environment<'a> {
|
|
||||||
let mut env: Environment = Environment::new();
|
|
||||||
for template in templates {
|
|
||||||
match template.src {
|
|
||||||
Some(src) => {
|
|
||||||
match env.add_template_owned(template.name.clone(), src) {
|
|
||||||
Ok(_) => {},
|
|
||||||
Err(err) => println!("ERROR: failt to add template \"{}\" ({err:?})", template.name),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
for plate in template.sub_templates {
|
|
||||||
if let Some(src) = plate.src {
|
|
||||||
let name = format!("{}/{}", template.name.clone(), plate.name.clone());
|
|
||||||
match env.add_template_owned(name.clone(), src) {
|
|
||||||
Ok(_) => {}, //println!("template name: {name}"),
|
|
||||||
Err(err) => println!("ERROR: failt to add template \"{name}\" ({err:?})"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
env
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Renderer {
|
pub struct Renderer<'a> {
|
||||||
// jinja_env: Environment<'a>,
|
|
||||||
pub site: IndexItem,
|
pub site: IndexItem,
|
||||||
pub path: Box<Path>
|
templates: Vec<Template>,
|
||||||
|
jinja_env: Environment<'a>,
|
||||||
}
|
}
|
||||||
impl Renderer {
|
impl<'a> Renderer<'a> {
|
||||||
pub fn index(path: &Path) -> Result<Option<Renderer>, indexer::Error> {
|
pub fn index(path: &Path) -> Result<Option<Renderer>, Error> {
|
||||||
if let Some(site_index) = IndexItem::index(&path)? {
|
if let Some(site_index) = IndexItem::index(&path).or_else(|e| Err(Error::Indexer(e)))? {
|
||||||
|
let templates = Template::index(&path.join("templates")).or_else(|e| Err(Error::Indexer(e)))?;
|
||||||
|
let mut jinja_env = Environment::new();
|
||||||
|
for tmpl in templates.iter() {
|
||||||
|
tmpl.add_to_jinja_env(&mut jinja_env).or_else(|e| Err(Error::Jinja(e)))?;
|
||||||
|
};
|
||||||
Ok(Some(Renderer {
|
Ok(Some(Renderer {
|
||||||
site: site_index,
|
site: site_index,
|
||||||
path: Box::from(path)
|
templates,
|
||||||
|
jinja_env,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -96,26 +75,27 @@ impl Renderer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_page(&self, page: &IndexItem, index: &IndexItem, jinja_env: &Environment) -> Result<String, Error> {
|
pub fn render_page(&self, page: &IndexItem) -> Result<String, Error> {
|
||||||
if page.src == None {
|
if page.src == None {
|
||||||
return Err(Error::NoSrc);
|
return Err(Error::NoSrc);
|
||||||
}
|
};
|
||||||
let content = fs::read_to_string(page.src.as_ref().unwrap()).or_else(|e| Err(Error::Io(e)))?;
|
let content = fs::read_to_string(page.src.as_ref().unwrap()).or_else(|e| Err(Error::Io(e)))?;
|
||||||
let split = indexer::split_params(content);
|
let split = split_params(content);
|
||||||
match parse_md(split.md, jinja_env) {
|
let mut parser = Parser::new(&split.md, &self.templates);
|
||||||
Some(md) => {
|
match parser.render() {
|
||||||
let template = jinja_env.get_template(&page.template).or_else(|e| Err(Error::Jinja(e)))?;
|
Ok(body) => {
|
||||||
|
let template = self.jinja_env.get_template(&page.template).or_else(|e| Err(Error::Jinja(e)))?;
|
||||||
let html = template.render(context! {
|
let html = template.render(context! {
|
||||||
index => index.to_minijinja(),
|
index => self.site.to_minijinja(),
|
||||||
is_error_not_found => false,
|
is_error_not_found => false,
|
||||||
url => page.get_url(),
|
url => page.get_url(),
|
||||||
body => md,
|
body,
|
||||||
params => Renderer::convert_yaml(split.yaml)
|
params => Renderer::convert_yaml(split.yaml)
|
||||||
}).or_else(|e| Err(Error::Jinja(e)))?;
|
}).or_else(|e| Err(Error::Jinja(e)))?;
|
||||||
|
|
||||||
Ok(html)
|
Ok(html)
|
||||||
},
|
},
|
||||||
None => todo!()
|
Err(e) => Err(Error::Parser(e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ pub mod indexer {
|
|||||||
use core::fmt;
|
use core::fmt;
|
||||||
use std::{collections::BTreeMap, ffi::OsString, fs, path::Path, io::Error as IoError};
|
use std::{collections::BTreeMap, ffi::OsString, fs, path::Path, io::Error as IoError};
|
||||||
use hashlink::LinkedHashMap;
|
use hashlink::LinkedHashMap;
|
||||||
|
use minijinja::{Environment, Error as JinjaError};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use yaml_rust2::{Yaml, YamlLoader};
|
use yaml_rust2::{Yaml, YamlLoader};
|
||||||
|
|
||||||
@@ -218,7 +219,7 @@ pub mod indexer {
|
|||||||
pub extention: String,
|
pub extention: String,
|
||||||
pub path: Option<String>,
|
pub path: Option<String>,
|
||||||
pub src: Option<String>,
|
pub src: Option<String>,
|
||||||
pub sub_templates: Vec<Box<Template>>
|
pub sub_templates: Vec<Template>
|
||||||
}
|
}
|
||||||
impl Template {
|
impl Template {
|
||||||
const fn new() -> Template {
|
const fn new() -> Template {
|
||||||
@@ -258,7 +259,7 @@ pub mod indexer {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn index(path: &Path) -> Result<Vec<Box<Self>>, Error> {
|
pub fn index(path: &Path) -> Result<Vec<Self>, Error> {
|
||||||
match Self::scan_template(path)? {
|
match Self::scan_template(path)? {
|
||||||
Some(templates) => Ok(templates.sub_templates),
|
Some(templates) => Ok(templates.sub_templates),
|
||||||
None => Ok(Vec::new()),
|
None => Ok(Vec::new()),
|
||||||
@@ -307,15 +308,15 @@ pub mod indexer {
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan_dir(path: &Path) -> Result<Vec<Box<Template>>, Error> {
|
fn scan_dir(path: &Path) -> Result<Vec<Template>, Error> {
|
||||||
let mut items: Vec<Box<Template>> = Vec::new();
|
let mut items: Vec<Template> = Vec::new();
|
||||||
|
|
||||||
let list = fs::read_dir(path).or(Err(Error::InvalidPathComponent))?;
|
let list = fs::read_dir(path).or(Err(Error::InvalidPathComponent))?;
|
||||||
for entry in list {
|
for entry in list {
|
||||||
let entry = entry.or_else(|e| Err(Error::ReadDir(e)))?;
|
let entry = entry.or_else(|e| Err(Error::ReadDir(e)))?;
|
||||||
let item: Option<Template> = Template::scan_template(&entry.path())?;
|
let item: Option<Template> = Template::scan_template(&entry.path())?;
|
||||||
if let Some(i) = item {
|
if let Some(i) = item {
|
||||||
items.push(Box::from(i));
|
items.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,6 +324,24 @@ pub mod indexer {
|
|||||||
Ok(items)
|
Ok(items)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl Template {
|
||||||
|
pub fn add_to_jinja_env(&self, env: &mut Environment) -> Result<(), JinjaError> {
|
||||||
|
self.add_to_jinja_env_with_path(env, String::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_to_jinja_env_with_path(&self, env: &mut Environment, mut path: String) -> Result<(), JinjaError> {
|
||||||
|
path.push_str(&self.name);
|
||||||
|
if let Some(source) = &self.src {
|
||||||
|
env.add_template_owned(path.clone(), source.clone())?;
|
||||||
|
}
|
||||||
|
path.push_str("/");
|
||||||
|
for tmpl in self.sub_templates.iter() {
|
||||||
|
tmpl.add_to_jinja_env_with_path(env, path.clone())?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct SplitMd {
|
pub struct SplitMd {
|
||||||
pub yaml: Yaml,
|
pub yaml: Yaml,
|
||||||
|
|||||||
@@ -1,10 +1,50 @@
|
|||||||
use std::{collections::BTreeMap};
|
use std::{collections::BTreeMap};
|
||||||
|
|
||||||
use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};
|
use pulldown_cmark::{Options, Parser as PulldownParser, Tree, TreeEvent};
|
||||||
use minijinja::{context, Environment, Value};
|
use minijinja::{Environment, Value, context, Error as JinjaError};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
pub fn parse_md(md: String, jinja_env: &Environment) -> Option<String> {
|
use crate::render::Template;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
NoCurrentEvent,
|
||||||
|
JinjaError(JinjaError),
|
||||||
|
TemplateNotFound
|
||||||
|
}
|
||||||
|
impl PartialEq for Error {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
match (self, other) {
|
||||||
|
(Self::JinjaError(l0), Self::JinjaError(r0)) => {
|
||||||
|
l0.kind() == r0.kind()
|
||||||
|
},
|
||||||
|
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Parser<'a> {
|
||||||
|
md: &'a str,
|
||||||
|
tree: Option<Tree<TreeEvent>>,
|
||||||
|
templates: &'a Vec<Template>,
|
||||||
|
jinja_env: Environment<'a>
|
||||||
|
}
|
||||||
|
impl<'a> Parser<'a> {
|
||||||
|
pub fn new(md: &'a str, templates: &'a Vec<Template>) -> Self {
|
||||||
|
let mut env = Environment::new();
|
||||||
|
for tmpl in templates.iter() {
|
||||||
|
tmpl.add_to_jinja_env(&mut env).unwrap();
|
||||||
|
};
|
||||||
|
|
||||||
|
Self {
|
||||||
|
md,
|
||||||
|
tree: None,
|
||||||
|
templates,
|
||||||
|
jinja_env: env
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(&mut self) -> Result<String, Error> {
|
||||||
let mut options = Options::empty();
|
let mut options = Options::empty();
|
||||||
options.insert(Options::ENABLE_TABLES);
|
options.insert(Options::ENABLE_TABLES);
|
||||||
options.insert(Options::ENABLE_FOOTNOTES);
|
options.insert(Options::ENABLE_FOOTNOTES);
|
||||||
@@ -22,341 +62,519 @@ pub fn parse_md(md: String, jinja_env: &Environment) -> Option<String> {
|
|||||||
options.insert(Options::ENABLE_SUBSCRIPT);
|
options.insert(Options::ENABLE_SUBSCRIPT);
|
||||||
options.insert(Options::ENABLE_WIKILINKS);
|
options.insert(Options::ENABLE_WIKILINKS);
|
||||||
|
|
||||||
let md = md_preprocessor(&md, jinja_env);
|
let md = self.md_preprocessor()?;
|
||||||
|
let mut parser = PulldownParser::new_ext(&md, options);
|
||||||
|
self.tree = Some(parser.to_event_tree());
|
||||||
|
let mut branch = Vec::new();
|
||||||
|
|
||||||
// let mut parser = Parser::new_ext("", options);
|
self.md_parser_children(&mut branch)
|
||||||
// let mut in_heading_two: Vec<Event> = Vec::new();
|
}
|
||||||
let parser = Parser::new_ext(&md, options).filter_map(|event| {
|
|
||||||
match &event {
|
fn md_preprocessor(&self) -> Result<String, Error> {
|
||||||
Event::Start(tag) => {
|
let mut new = String::with_capacity(self.md.len());
|
||||||
match tag {
|
let mut last_match = 0;
|
||||||
Tag::Paragraph => {
|
let re = Regex::new(r"\{\% *(.*)\((.*)\).*\%\}").unwrap();
|
||||||
render_component("paragraph_start", Value::UNDEFINED, jinja_env)
|
for caps in re.captures_iter(self.md) {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
let m = caps.get(0).unwrap();
|
||||||
.or(Some(event))
|
new.push_str(&self.md[last_match..m.start()]);
|
||||||
|
let template = &caps[1];
|
||||||
|
let args = &caps[2];
|
||||||
|
new.push_str(&self.render_component(template, context! { args => parse_args(args)})?);
|
||||||
|
last_match = m.end();
|
||||||
|
}
|
||||||
|
new.push_str(&self.md[last_match..]);
|
||||||
|
Ok(new)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn md_parser_children(&mut self, branch: &mut Vec<TreeEvent>) -> Result<String, Error> {
|
||||||
|
let mut content = String::new();
|
||||||
|
loop {
|
||||||
|
let event = self.tree.as_ref().unwrap().get_cur().unwrap();
|
||||||
|
let child_content = match self.tree.as_mut().unwrap().goto_first_child() {
|
||||||
|
Some(_) => {
|
||||||
|
branch.push(event.clone());
|
||||||
|
let content = self.md_parser_children(branch)?;
|
||||||
|
branch.pop();
|
||||||
|
self.tree.as_mut().unwrap().goto_parent();
|
||||||
|
|
||||||
|
content
|
||||||
},
|
},
|
||||||
Tag::Heading { level, id, classes, attrs } => {
|
None => String::new(),
|
||||||
render_component("heading_start", context! {
|
};
|
||||||
level => *level as u8,
|
content.push_str(&self.md_parser_event(branch, child_content)?);
|
||||||
id,
|
|
||||||
classes,
|
if self.tree.as_mut().unwrap().goto_next() == None {
|
||||||
attrs
|
break;
|
||||||
}, jinja_env)
|
}
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
}
|
||||||
.or(Some(event))
|
Ok(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn md_parser_event(&self, branch: &Vec<TreeEvent>, content: String) -> Result<String, Error> {
|
||||||
|
match self.tree.as_ref().unwrap().get_cur().ok_or(Error::NoCurrentEvent)? {
|
||||||
|
TreeEvent::Root => {
|
||||||
|
Ok(content)
|
||||||
},
|
},
|
||||||
Tag::BlockQuote(block_quote_kind) => {
|
TreeEvent::Paragraph => {
|
||||||
render_component("blockquote_start", context! {
|
self.render_component("Paragraph", context! {
|
||||||
kind => block_quote_kind
|
content,
|
||||||
}, jinja_env)
|
parents => branch
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
}).or_else(|e| {
|
||||||
.or(Some(event))
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::CodeBlock(code_block_kind) => {
|
TreeEvent::Heading { level, id, classes, attrs } => {
|
||||||
render_component("codeblock_start", context! {
|
self.render_component("Heading", context! {
|
||||||
kind => code_block_kind
|
content,
|
||||||
}, jinja_env)
|
parents => branch,
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
level => level as u8,
|
||||||
.or(Some(event))
|
id, classes, attrs
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::HtmlBlock => {
|
TreeEvent::BlockQuote(block_quote_kind) => {
|
||||||
render_component("htmlblock_start", Value::UNDEFINED, jinja_env)
|
self.render_component("BlockQuote", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch,
|
||||||
|
block_quote_kind
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::List(start) => {
|
TreeEvent::CodeBlock => {
|
||||||
render_component("list_start", context! {
|
self.render_component("CodeBlock", context! {
|
||||||
start
|
content,
|
||||||
}, jinja_env)
|
parents => branch
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
}).or_else(|e| {
|
||||||
.or(Some(event))
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::Item => {
|
TreeEvent::ContainerBlock(container_kind, _) => {
|
||||||
render_component("item_start", Value::UNDEFINED, jinja_env)
|
self.render_component("ContainerBlock", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch,
|
||||||
|
container_kind
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::FootnoteDefinition(cow_str) => {
|
TreeEvent::HtmlBlock => {
|
||||||
render_component("footnote_definition_start", context! {
|
self.render_component("HtmlBlock", context! {
|
||||||
label => cow_str
|
content,
|
||||||
}, jinja_env)
|
parents => branch
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
}).or_else(|e| {
|
||||||
.or(Some(event))
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::DefinitionList => {
|
TreeEvent::List(_) => {
|
||||||
render_component("definitionlist_start", Value::UNDEFINED, jinja_env)
|
self.render_component("List", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::DefinitionListTitle => {
|
TreeEvent::Item => {
|
||||||
render_component("definitionlist_title_start", Value::UNDEFINED, jinja_env)
|
self.render_component("Item", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::DefinitionListDefinition => {
|
TreeEvent::FootnoteDefinition(_) => {
|
||||||
render_component("definitionlist_definition_start", Value::UNDEFINED, jinja_env)
|
self.render_component("FootnoteDefinition", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::Table(alignments) => {
|
TreeEvent::DefinitionList => {
|
||||||
render_component("table_start", context! {
|
self.render_component("DefinitionList", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::DefinitionListTitle => {
|
||||||
|
self.render_component("DefinitionListTitle", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::DefinitionListDefinition => {
|
||||||
|
self.render_component("DefinitionListDefinition", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::Table(alignments) => {
|
||||||
|
self.render_component("Table", context! {
|
||||||
|
content,
|
||||||
|
parents => branch,
|
||||||
alignments
|
alignments
|
||||||
}, jinja_env)
|
}).or_else(|e| {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
if e == Error::TemplateNotFound {
|
||||||
.or(Some(event))
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::TableHead => {
|
TreeEvent::TableHead => {
|
||||||
render_component("tablehead_start", Value::UNDEFINED, jinja_env)
|
self.render_component("TableHead", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::TableRow => {
|
TreeEvent::TableRow => {
|
||||||
render_component("tablerow_start", Value::UNDEFINED, jinja_env)
|
self.render_component("TableRow", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::TableCell => {
|
TreeEvent::TableCell => {
|
||||||
render_component("tablecell_start", Value::UNDEFINED, jinja_env)
|
self.render_component("TableCell", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::Emphasis => {
|
TreeEvent::Emphasis => {
|
||||||
render_component("emphasis_start", Value::UNDEFINED, jinja_env)
|
self.render_component("Emphasis", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::Strong => {
|
TreeEvent::Strong => {
|
||||||
render_component("strong_start", Value::UNDEFINED, jinja_env)
|
self.render_component("Strong", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::Strikethrough => {
|
TreeEvent::Strikethrough => {
|
||||||
render_component("strikethrough_start", Value::UNDEFINED, jinja_env)
|
self.render_component("Strikethrough", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::Superscript => {
|
TreeEvent::Superscript => {
|
||||||
render_component("superscript_start", Value::UNDEFINED, jinja_env)
|
self.render_component("Superscript", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::Subscript => {
|
TreeEvent::Subscript => {
|
||||||
render_component("subscript_start", Value::UNDEFINED, jinja_env)
|
self.render_component("Subscript", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::Link { link_type, dest_url, title, id } => {
|
TreeEvent::Link { link_type, dest_url, title, id } => {
|
||||||
let re = Regex::new(r"^%(?<tags>[^%]+)%").unwrap();
|
let re = Regex::new(r"^%(?<tags>[^%]+)%").unwrap();
|
||||||
let (url, args) = match re.captures(&dest_url) {
|
let (_, args) = match re.captures(&dest_url) {
|
||||||
Some(args) => {
|
Some(args) => {
|
||||||
(dest_url.strip_prefix(args.get(0).unwrap().as_str()).unwrap().to_string(), parse_args(args.name("tags").unwrap().as_str()))
|
(
|
||||||
|
dest_url.strip_prefix(args.get(0).unwrap().as_str()).unwrap().to_string(),
|
||||||
|
parse_args(args.name("tags").unwrap().as_str())
|
||||||
|
)
|
||||||
},
|
},
|
||||||
None => (dest_url.to_string(), Value::UNDEFINED)
|
None => (dest_url.to_string(), Value::UNDEFINED)
|
||||||
};
|
};
|
||||||
render_component("link_start", context! {
|
self.render_component("Link", context! {
|
||||||
link_type => link_type,
|
content,
|
||||||
dest_url => url,
|
parents => branch,
|
||||||
title => title,
|
link_type, dest_url, title, id, args
|
||||||
id => id,
|
}).or_else(|e| {
|
||||||
args => args
|
if e == Error::TemplateNotFound {
|
||||||
}, jinja_env)
|
todo!()
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
} else {
|
||||||
.or(Some(event))
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::Image { link_type, dest_url, title, id } => {
|
TreeEvent::Image { link_type, dest_url, title, id } => {
|
||||||
render_component("image_start", Value::UNDEFINED, jinja_env)
|
self.render_component("Image", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch,
|
||||||
|
link_type, dest_url, title, id
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
Tag::MetadataBlock(_metadata_block_kind) => {
|
TreeEvent::MetadataBlock(metadata_block_kind) => {
|
||||||
render_component("metadata_block_start", Value::UNDEFINED, jinja_env)
|
self.render_component("MetadataBlock", context! {
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
content,
|
||||||
.or(Some(event))
|
parents => branch,
|
||||||
|
metadata_block_kind
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::Text(text) => {
|
||||||
|
self.render_component("Text", context! {
|
||||||
|
content,
|
||||||
|
parents => branch,
|
||||||
|
text
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::Code(_) => {
|
||||||
|
self.render_component("Code", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::InlineMath(_) => {
|
||||||
|
self.render_component("InlineMath", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::DisplayMath(_) => {
|
||||||
|
self.render_component("DisplayMath", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::Html(_) => {
|
||||||
|
self.render_component("Html", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::InlineHtml(_) => {
|
||||||
|
self.render_component("InlineHtml", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::FootnoteReference(_) => {
|
||||||
|
self.render_component("FootnoteReference", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::SoftBreak => {
|
||||||
|
self.render_component("SoftBreak", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::HardBreak => {
|
||||||
|
self.render_component("HardBreak", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::Rule => {
|
||||||
|
self.render_component("Rule", context! {
|
||||||
|
content,
|
||||||
|
parents => branch
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
TreeEvent::TaskListMarker(checked) => {
|
||||||
|
self.render_component("TaskListMarker", context! {
|
||||||
|
content,
|
||||||
|
parents => branch,
|
||||||
|
checked
|
||||||
|
}).or_else(|e| {
|
||||||
|
if e == Error::TemplateNotFound {
|
||||||
|
todo!()
|
||||||
|
} else {
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
|
||||||
Event::End(tag) => {
|
|
||||||
match tag {
|
|
||||||
TagEnd::Paragraph => {
|
|
||||||
render_component("paragraph_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Heading(level) => {
|
|
||||||
render_component("heading_end", context! {
|
|
||||||
level => *level as u8
|
|
||||||
}, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::BlockQuote(block_quote_kind) => {
|
|
||||||
render_component("blockquote_end", context! {
|
|
||||||
kind => block_quote_kind
|
|
||||||
}, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::CodeBlock => {
|
|
||||||
render_component("codeblock_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::HtmlBlock => {
|
|
||||||
render_component("htmlblock_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::List(start) => {
|
|
||||||
render_component("list_end", context! {
|
|
||||||
start
|
|
||||||
}, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Item => {
|
|
||||||
render_component("item_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::FootnoteDefinition => {
|
|
||||||
render_component("footnote_definition_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::DefinitionList => {
|
|
||||||
render_component("definitionlist_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::DefinitionListTitle => {
|
|
||||||
render_component("definitionlist_title_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::DefinitionListDefinition => {
|
|
||||||
render_component("definitionlist_definition_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Table => {
|
|
||||||
render_component("table_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::TableHead => {
|
|
||||||
render_component("tablehead_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::TableRow => {
|
|
||||||
render_component("tablerow_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::TableCell => {
|
|
||||||
render_component("tablecell_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Emphasis => {
|
|
||||||
render_component("emphasis_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Strong => {
|
|
||||||
render_component("strong_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Strikethrough => {
|
|
||||||
render_component("strikethrough_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Superscript => {
|
|
||||||
render_component("superscript_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Subscript => {
|
|
||||||
render_component("subscript_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Link => {
|
|
||||||
render_component("link_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::Image => {
|
|
||||||
render_component("image_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
TagEnd::MetadataBlock(_metadata_block_kind) => {
|
|
||||||
render_component("metadata_block_end", Value::UNDEFINED, jinja_env)
|
|
||||||
.and_then(|s| Some(Event::Html(s.into())))
|
|
||||||
.or(Some(event))
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Event::Text(_cow_str) => Some(event),
|
|
||||||
Event::Code(_cow_str) => Some(event),
|
|
||||||
Event::InlineMath(_cow_str) => Some(event),
|
|
||||||
Event::DisplayMath(_cow_str) => Some(event),
|
|
||||||
Event::Html(_cow_str) => Some(event),
|
|
||||||
Event::InlineHtml(_cow_str) => Some(event),
|
|
||||||
Event::FootnoteReference(_cow_str) => Some(event),
|
|
||||||
Event::SoftBreak => Some(event),
|
|
||||||
Event::HardBreak => Some(event),
|
|
||||||
Event::Rule => Some(event),
|
|
||||||
Event::TaskListMarker(_) => Some(event),
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let mut html = String::new();
|
|
||||||
pulldown_cmark::html::push_html(&mut html, parser);
|
|
||||||
|
|
||||||
Some(html)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn md_preprocessor(md: &String, jinja_env: &Environment) -> String {
|
fn render_component(&self, component: &str, args: Value) -> Result<String, Error> {
|
||||||
let re = Regex::new(r"\{\% *(.*)\((.*)\).*\%\}").unwrap();
|
match self.jinja_env.get_template(&format!("components/{component}")) {
|
||||||
let out = re.replace_all(md, |capture: ®ex::Captures<'_>| {
|
Ok(tmpl) => {
|
||||||
let template = &capture[1];
|
match tmpl.render(args) {
|
||||||
let args = &capture[2];
|
Ok(html) => Ok(html),
|
||||||
match render_component(template, context! { args => parse_args(args)}, jinja_env){
|
Err(err) => Err(Error::JinjaError(err)),
|
||||||
Some(html) => html,
|
|
||||||
None => {
|
|
||||||
println!("ERROR: could not find template {}", template);
|
|
||||||
String::from("")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
String::from(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_component(component: &str, args: Value, jinja_env: &Environment) -> Option<String> {
|
|
||||||
// println!(" tag found: {component} <- {args}");
|
|
||||||
match jinja_env.get_template(&format!("components/{component}")) {
|
|
||||||
Ok(ding) => {
|
|
||||||
// match ding.render(context! { args => args }) {
|
|
||||||
match ding.render(args) {
|
|
||||||
Ok(html) => Some(html),
|
|
||||||
Err(err) => {
|
|
||||||
println!("ERROR: faild to render template: {component} ({err:?})");
|
|
||||||
None
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
match e.kind() {
|
match e.kind() {
|
||||||
minijinja::ErrorKind::TemplateNotFound => None,
|
minijinja::ErrorKind::TemplateNotFound => Err(Error::TemplateNotFound),
|
||||||
_ => panic!("Failt to render component {}: {:?}", component, e),
|
_ => Err(Error::JinjaError(e)),
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const NUMBER: i8 = 0;
|
const NUMBER: i8 = 0;
|
||||||
const STRING: i8 = 1;
|
const STRING: i8 = 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user