-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from yuma140902/template-engine
add `TemplateEngine`
- Loading branch information
Showing
4 changed files
with
93 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! This module defines a custom Helper for Tempura. | ||
//! Helper is a term used in Handlebars to refer to a kind of function that can be called from a template. | ||
|
||
use std::path::PathBuf; | ||
|
||
use handlebars::{ | ||
handlebars_helper, Context, Handlebars, Helper, HelperResult, JsonRender, Output, | ||
RenderContext, RenderError, | ||
}; | ||
|
||
use crate::directory; | ||
|
||
handlebars_helper!(md2html: |markdown: String| { | ||
let options = pulldown_cmark::Options::empty(); | ||
let parser = pulldown_cmark::Parser::new_ext(&markdown, options); | ||
let mut html_output = String::new(); | ||
pulldown_cmark::html::push_html(&mut html_output, parser); | ||
html_output | ||
}); | ||
|
||
// TODO: | ||
pub fn resolve( | ||
h: &Helper<'_, '_>, | ||
_: &Handlebars<'_>, | ||
ctx: &Context, | ||
_rc: &mut RenderContext<'_, '_>, | ||
out: &mut dyn Output, | ||
) -> HelperResult { | ||
let target_input_path = PathBuf::from( | ||
h.param(0) | ||
.and_then(|v| v.value().as_str()) | ||
.ok_or_else(|| RenderError::new("target_input_path not specified"))?, | ||
); | ||
let project_root = PathBuf::from( | ||
ctx.data() | ||
.get("project_root") | ||
.ok_or_else(|| RenderError::new("project_root not specified"))? | ||
.render() | ||
.as_str(), | ||
); | ||
let output_directory = directory::get_output_directory(project_root); | ||
let self_output_filepath = PathBuf::from( | ||
ctx.data() | ||
.get("output_file") | ||
.ok_or_else(|| RenderError::new("output_file not specified"))? | ||
.render() | ||
.as_str(), | ||
); | ||
let self_output_parent = self_output_filepath.parent().ok_or_else(|| { | ||
RenderError::new(format!( | ||
"could not get parent directory for output_file '{}'", | ||
self_output_filepath.display() | ||
)) | ||
})?; | ||
|
||
let target_output_path = output_directory.join(target_input_path); //TODO: | ||
//rule.export_extension | ||
//rule.export_base | ||
let relative_path = directory::get_relative_path(target_output_path, self_output_parent); | ||
write!(out, "{}", relative_path.display())?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use handlebars::Handlebars; | ||
|
||
use crate::handlebars_helpers; | ||
|
||
/// TemplateEngine is a wrapper for Handlebars and processes templates and [`Value`](crate::Value)s. | ||
pub struct TemplateEngine(Handlebars<'static>); | ||
|
||
impl TemplateEngine { | ||
pub fn new() -> Self { | ||
let mut handlebars = Handlebars::new(); | ||
|
||
handlebars.register_helper("md2html", Box::new(handlebars_helpers::md2html)); | ||
handlebars.register_helper("resolve", Box::new(handlebars_helpers::resolve)); | ||
|
||
Self(handlebars) | ||
} | ||
} | ||
|
||
impl Default for TemplateEngine { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |