better component templates
This commit is contained in:
6
Cargo.lock
generated
6
Cargo.lock
generated
@@ -227,8 +227,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "pulldown-cmark"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e8bbe1a966bd2f362681a44f6edce3c2310ac21e4d5067a6e7ec396297a6ea0"
|
||||
source = "git+https://git.gay/LailaTheElf/pulldown-cmark.git?branch=feature%2FeventTree#2300d0db6541279c41928b44d12eac510ecc3199"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"getopts",
|
||||
@@ -241,8 +240,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "pulldown-cmark-escape"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"
|
||||
source = "git+https://git.gay/LailaTheElf/pulldown-cmark.git?branch=feature%2FeventTree#2300d0db6541279c41928b44d12eac510ecc3199"
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
|
||||
@@ -7,6 +7,10 @@ edition = "2024"
|
||||
clap = { version = "4.5.55", features = ["derive"] }
|
||||
hashlink = "0.10.0"
|
||||
minijinja = { version = "2.10.2", features = ["loader", "builtins", "json"] }
|
||||
pulldown-cmark = { version = "0.13.0", features = ["serde"] }
|
||||
regex = "1.11.1"
|
||||
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 minijinja::Environment;
|
||||
|
||||
mod render;
|
||||
use render::{Renderer, IndexItem, build_jinja_env, Template};
|
||||
use render::{Renderer, IndexItem};
|
||||
|
||||
mod version;
|
||||
use version::VERSION;
|
||||
|
||||
fn render_index(
|
||||
fn render_page(
|
||||
page: &IndexItem,
|
||||
out_path: &Path,
|
||||
index: &IndexItem,
|
||||
index: &Renderer,
|
||||
cur_path: &Path,
|
||||
site_index: &Renderer,
|
||||
templates: Option<Vec<Box<Template>>>,
|
||||
jinja_env: Option<&Environment>
|
||||
) {
|
||||
let dest_path: &Path = &cur_path.join(&index.friendly);
|
||||
// println!("dest_path: {:?}", dest_path);
|
||||
|
||||
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
|
||||
let friendly = match page.friendly.len() {
|
||||
0 => "index",
|
||||
_ => &page.friendly
|
||||
};
|
||||
|
||||
if let Some(src) = &page.src {
|
||||
if !page.is_asset {
|
||||
// get output file extention
|
||||
let extention = match index.target_extention.len() {
|
||||
0 => match templ {
|
||||
Some(templ) => templ.extention,
|
||||
None => String::from("html")
|
||||
}
|
||||
_ => index.target_extention.clone()
|
||||
let extention = match page.target_extention.len() {
|
||||
0 => String::from("html"),
|
||||
_ => page.target_extention.clone()
|
||||
};
|
||||
let dest_path: &Path = &cur_path.join(format!("{}.{}", friendly, extention));
|
||||
// render the content
|
||||
let dest_path_str = dest_path.to_str();
|
||||
if dest_path_str == None {
|
||||
println!("ERROR: dest path is None for page: {}", friendly);
|
||||
return;
|
||||
}
|
||||
println!("INFO: render {}", dest_path_str.unwrap());
|
||||
let content = site_index.render_page(index, &site_index.site, jinja_env).unwrap();
|
||||
fs::write(dest_path, content).unwrap();
|
||||
println!("INFO: render {}", dest_path.to_str().unwrap());
|
||||
match index.render_page(page) {
|
||||
Ok(content) => {
|
||||
let _: Result<(), Infallible> = fs::write(dest_path, content).or_else(|e|{
|
||||
println!("ERROR during writing: {:?}", e);
|
||||
Ok(())
|
||||
});
|
||||
},
|
||||
Err(e) => {
|
||||
println!("ERROR during rendering: {:?}", e);
|
||||
},
|
||||
};
|
||||
}
|
||||
else {
|
||||
// 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() {
|
||||
Some(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 => {
|
||||
println!("ERROR: dest path is None for asset: {}", friendly);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
None => {
|
||||
let _ = fs::create_dir(dest_path);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
for page in &index.sub_pages {
|
||||
render_index(out_path, page, dest_path, &site_index, Some(templates.clone()), Some(&jinja_env));
|
||||
for page in page.sub_pages.iter() {
|
||||
render_page(page, out_path, &index, cur_path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,16 +90,16 @@ fn main() {
|
||||
let src_path = Path::new(&args.source);
|
||||
let out_path = Path::new(&args.output);
|
||||
|
||||
// scan all pages, template, components, etc.
|
||||
let index = match Renderer::index(src_path).unwrap() {
|
||||
// scan all pages adn templates
|
||||
let index = match Renderer::index(src_path).or_else::<Infallible, _>(|e| {
|
||||
panic!("PANIC: faild to index: {:?}", e);
|
||||
}).unwrap() {
|
||||
Some(index) => index,
|
||||
None => {
|
||||
println!("ERROR: src directory not found ({})", src_path.to_str().or(Some("Utf parse error")).unwrap());
|
||||
return;
|
||||
panic!("ERROR: src directory not found ({})", src_path.to_str().or(Some("Utf parse error")).unwrap());
|
||||
},
|
||||
};
|
||||
|
||||
// render website
|
||||
let _ = fs::create_dir_all(out_path);
|
||||
render_index(out_path, &index.site, out_path, &index, None, None);
|
||||
// render pages
|
||||
render_page(&index.site, out_path, &index, out_path);
|
||||
}
|
||||
|
||||
@@ -1,63 +1,42 @@
|
||||
mod index;
|
||||
mod parse_md;
|
||||
|
||||
use std::{collections::BTreeMap, fs, path::Path, io::Error as IoError};
|
||||
use minijinja::{context, Environment, Value, Error as JinjaError};
|
||||
use index::indexer;
|
||||
use parse_md::parse_md;
|
||||
use std::{collections::BTreeMap, fs, io::Error as IoError, path::Path};
|
||||
use minijinja::{Environment, Error as JinjaError, Value, context};
|
||||
use index::indexer::Error as IndexError;
|
||||
use yaml_rust2::Yaml;
|
||||
use crate::render::{index::indexer::split_params, parse_md::{Parser, Error as ParserError}};
|
||||
|
||||
pub use index::indexer::{IndexItem, Template};
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
NoSrc,
|
||||
Io(IoError),
|
||||
Jinja(JinjaError)
|
||||
}
|
||||
|
||||
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
|
||||
Jinja(JinjaError),
|
||||
Indexer(IndexError),
|
||||
Parser(ParserError),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Renderer {
|
||||
// jinja_env: Environment<'a>,
|
||||
pub struct Renderer<'a> {
|
||||
pub site: IndexItem,
|
||||
pub path: Box<Path>
|
||||
templates: Vec<Template>,
|
||||
jinja_env: Environment<'a>,
|
||||
}
|
||||
impl Renderer {
|
||||
pub fn index(path: &Path) -> Result<Option<Renderer>, indexer::Error> {
|
||||
if let Some(site_index) = IndexItem::index(&path)? {
|
||||
impl<'a> Renderer<'a> {
|
||||
pub fn index(path: &Path) -> Result<Option<Renderer>, Error> {
|
||||
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 {
|
||||
site: site_index,
|
||||
path: Box::from(path)
|
||||
templates,
|
||||
jinja_env,
|
||||
}))
|
||||
}
|
||||
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 {
|
||||
return Err(Error::NoSrc);
|
||||
}
|
||||
};
|
||||
let content = fs::read_to_string(page.src.as_ref().unwrap()).or_else(|e| Err(Error::Io(e)))?;
|
||||
let split = indexer::split_params(content);
|
||||
match parse_md(split.md, jinja_env) {
|
||||
Some(md) => {
|
||||
let template = jinja_env.get_template(&page.template).or_else(|e| Err(Error::Jinja(e)))?;
|
||||
let split = split_params(content);
|
||||
let mut parser = Parser::new(&split.md, &self.templates);
|
||||
match parser.render() {
|
||||
Ok(body) => {
|
||||
let template = self.jinja_env.get_template(&page.template).or_else(|e| Err(Error::Jinja(e)))?;
|
||||
let html = template.render(context! {
|
||||
index => index.to_minijinja(),
|
||||
index => self.site.to_minijinja(),
|
||||
is_error_not_found => false,
|
||||
url => page.get_url(),
|
||||
body => md,
|
||||
body,
|
||||
params => Renderer::convert_yaml(split.yaml)
|
||||
}).or_else(|e| Err(Error::Jinja(e)))?;
|
||||
|
||||
Ok(html)
|
||||
},
|
||||
None => todo!()
|
||||
Err(e) => Err(Error::Parser(e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ pub mod indexer {
|
||||
use core::fmt;
|
||||
use std::{collections::BTreeMap, ffi::OsString, fs, path::Path, io::Error as IoError};
|
||||
use hashlink::LinkedHashMap;
|
||||
use minijinja::{Environment, Error as JinjaError};
|
||||
use regex::Regex;
|
||||
use yaml_rust2::{Yaml, YamlLoader};
|
||||
|
||||
@@ -218,7 +219,7 @@ pub mod indexer {
|
||||
pub extention: String,
|
||||
pub path: Option<String>,
|
||||
pub src: Option<String>,
|
||||
pub sub_templates: Vec<Box<Template>>
|
||||
pub sub_templates: Vec<Template>
|
||||
}
|
||||
impl Template {
|
||||
const fn new() -> Template {
|
||||
@@ -258,7 +259,7 @@ pub mod indexer {
|
||||
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)? {
|
||||
Some(templates) => Ok(templates.sub_templates),
|
||||
None => Ok(Vec::new()),
|
||||
@@ -307,15 +308,15 @@ pub mod indexer {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn scan_dir(path: &Path) -> Result<Vec<Box<Template>>, Error> {
|
||||
let mut items: Vec<Box<Template>> = Vec::new();
|
||||
fn scan_dir(path: &Path) -> Result<Vec<Template>, Error> {
|
||||
let mut items: Vec<Template> = Vec::new();
|
||||
|
||||
let list = fs::read_dir(path).or(Err(Error::InvalidPathComponent))?;
|
||||
for entry in list {
|
||||
let entry = entry.or_else(|e| Err(Error::ReadDir(e)))?;
|
||||
let item: Option<Template> = Template::scan_template(&entry.path())?;
|
||||
if let Some(i) = item {
|
||||
items.push(Box::from(i));
|
||||
items.push(i);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,6 +324,24 @@ pub mod indexer {
|
||||
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 yaml: Yaml,
|
||||
|
||||
@@ -1,10 +1,50 @@
|
||||
use std::{collections::BTreeMap};
|
||||
|
||||
use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};
|
||||
use minijinja::{context, Environment, Value};
|
||||
use pulldown_cmark::{Options, Parser as PulldownParser, Tree, TreeEvent};
|
||||
use minijinja::{Environment, Value, context, Error as JinjaError};
|
||||
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();
|
||||
options.insert(Options::ENABLE_TABLES);
|
||||
options.insert(Options::ENABLE_FOOTNOTES);
|
||||
@@ -22,340 +62,518 @@ pub fn parse_md(md: String, jinja_env: &Environment) -> Option<String> {
|
||||
options.insert(Options::ENABLE_SUBSCRIPT);
|
||||
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);
|
||||
// let mut in_heading_two: Vec<Event> = Vec::new();
|
||||
let parser = Parser::new_ext(&md, options).filter_map(|event| {
|
||||
match &event {
|
||||
Event::Start(tag) => {
|
||||
match tag {
|
||||
Tag::Paragraph => {
|
||||
render_component("paragraph_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
self.md_parser_children(&mut branch)
|
||||
}
|
||||
|
||||
fn md_preprocessor(&self) -> Result<String, Error> {
|
||||
let mut new = String::with_capacity(self.md.len());
|
||||
let mut last_match = 0;
|
||||
let re = Regex::new(r"\{\% *(.*)\((.*)\).*\%\}").unwrap();
|
||||
for caps in re.captures_iter(self.md) {
|
||||
let m = caps.get(0).unwrap();
|
||||
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 } => {
|
||||
render_component("heading_start", context! {
|
||||
level => *level as u8,
|
||||
id,
|
||||
classes,
|
||||
attrs
|
||||
}, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
None => String::new(),
|
||||
};
|
||||
content.push_str(&self.md_parser_event(branch, child_content)?);
|
||||
|
||||
if self.tree.as_mut().unwrap().goto_next() == None {
|
||||
break;
|
||||
}
|
||||
}
|
||||
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) => {
|
||||
render_component("blockquote_start", context! {
|
||||
kind => block_quote_kind
|
||||
}, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::Paragraph => {
|
||||
self.render_component("Paragraph", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::CodeBlock(code_block_kind) => {
|
||||
render_component("codeblock_start", context! {
|
||||
kind => code_block_kind
|
||||
}, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::Heading { level, id, classes, attrs } => {
|
||||
self.render_component("Heading", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
level => level as u8,
|
||||
id, classes, attrs
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::HtmlBlock => {
|
||||
render_component("htmlblock_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::BlockQuote(block_quote_kind) => {
|
||||
self.render_component("BlockQuote", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
block_quote_kind
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::List(start) => {
|
||||
render_component("list_start", context! {
|
||||
start
|
||||
}, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::CodeBlock => {
|
||||
self.render_component("CodeBlock", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::Item => {
|
||||
render_component("item_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::ContainerBlock(container_kind, _) => {
|
||||
self.render_component("ContainerBlock", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
container_kind
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::FootnoteDefinition(cow_str) => {
|
||||
render_component("footnote_definition_start", context! {
|
||||
label => cow_str
|
||||
}, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::HtmlBlock => {
|
||||
self.render_component("HtmlBlock", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::DefinitionList => {
|
||||
render_component("definitionlist_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::List(_) => {
|
||||
self.render_component("List", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::DefinitionListTitle => {
|
||||
render_component("definitionlist_title_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::Item => {
|
||||
self.render_component("Item", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::DefinitionListDefinition => {
|
||||
render_component("definitionlist_definition_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::FootnoteDefinition(_) => {
|
||||
self.render_component("FootnoteDefinition", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::Table(alignments) => {
|
||||
render_component("table_start", context! {
|
||||
TreeEvent::DefinitionList => {
|
||||
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
|
||||
}, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::TableHead => {
|
||||
render_component("tablehead_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::TableHead => {
|
||||
self.render_component("TableHead", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::TableRow => {
|
||||
render_component("tablerow_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::TableRow => {
|
||||
self.render_component("TableRow", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::TableCell => {
|
||||
render_component("tablecell_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::TableCell => {
|
||||
self.render_component("TableCell", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::Emphasis => {
|
||||
render_component("emphasis_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::Emphasis => {
|
||||
self.render_component("Emphasis", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::Strong => {
|
||||
render_component("strong_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::Strong => {
|
||||
self.render_component("Strong", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::Strikethrough => {
|
||||
render_component("strikethrough_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::Strikethrough => {
|
||||
self.render_component("Strikethrough", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::Superscript => {
|
||||
render_component("superscript_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::Superscript => {
|
||||
self.render_component("Superscript", context! {
|
||||
content,
|
||||
parents => branch
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::Subscript => {
|
||||
render_component("subscript_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::Subscript => {
|
||||
self.render_component("Subscript", context! {
|
||||
content,
|
||||
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 (url, args) = match re.captures(&dest_url) {
|
||||
let (_, args) = match re.captures(&dest_url) {
|
||||
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)
|
||||
};
|
||||
render_component("link_start", context! {
|
||||
link_type => link_type,
|
||||
dest_url => url,
|
||||
title => title,
|
||||
id => id,
|
||||
args => args
|
||||
}, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
self.render_component("Link", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
link_type, dest_url, title, id, args
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::Image { link_type, dest_url, title, id } => {
|
||||
render_component("image_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::Image { link_type, dest_url, title, id } => {
|
||||
self.render_component("Image", context! {
|
||||
content,
|
||||
parents => branch,
|
||||
link_type, dest_url, title, id
|
||||
}).or_else(|e| {
|
||||
if e == Error::TemplateNotFound {
|
||||
todo!()
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
})
|
||||
},
|
||||
Tag::MetadataBlock(_metadata_block_kind) => {
|
||||
render_component("metadata_block_start", Value::UNDEFINED, jinja_env)
|
||||
.and_then(|s| Some(Event::Html(s.into())))
|
||||
.or(Some(event))
|
||||
TreeEvent::MetadataBlock(metadata_block_kind) => {
|
||||
self.render_component("MetadataBlock", context! {
|
||||
content,
|
||||
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 {
|
||||
let re = Regex::new(r"\{\% *(.*)\((.*)\).*\%\}").unwrap();
|
||||
let out = re.replace_all(md, |capture: ®ex::Captures<'_>| {
|
||||
let template = &capture[1];
|
||||
let args = &capture[2];
|
||||
match render_component(template, context! { args => parse_args(args)}, jinja_env){
|
||||
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
|
||||
},
|
||||
fn render_component(&self, component: &str, args: Value) -> Result<String, Error> {
|
||||
match self.jinja_env.get_template(&format!("components/{component}")) {
|
||||
Ok(tmpl) => {
|
||||
match tmpl.render(args) {
|
||||
Ok(html) => Ok(html),
|
||||
Err(err) => Err(Error::JinjaError(err)),
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
match e.kind() {
|
||||
minijinja::ErrorKind::TemplateNotFound => None,
|
||||
_ => panic!("Failt to render component {}: {:?}", component, e),
|
||||
minijinja::ErrorKind::TemplateNotFound => Err(Error::TemplateNotFound),
|
||||
_ => Err(Error::JinjaError(e)),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const NUMBER: i8 = 0;
|
||||
|
||||
Reference in New Issue
Block a user