mirror of
https://github.com/Xevion/time-banner.git
synced 2025-12-16 02:13:27 -06:00
Complete overhaul of render/rasterize/parsing/templates/routes subsystems
This commit is contained in:
47
src/template.rs
Normal file
47
src/template.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use chrono::{DateTime, FixedOffset, Utc};
|
||||
use timeago::Formatter;
|
||||
use tera::{Context, Tera};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
static ref TEMPLATES: Tera = {
|
||||
let mut _tera = match Tera::new("templates/**/*.svg") {
|
||||
Ok(t) => {
|
||||
let names: Vec<&str> = t.get_template_names().collect();
|
||||
println!("{} templates found ([{}]).", names.len(), names.join(", "));
|
||||
t
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Parsing error(s): {}", e);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
};
|
||||
_tera
|
||||
};
|
||||
}
|
||||
|
||||
pub enum OutputForm {
|
||||
Relative,
|
||||
Absolute,
|
||||
}
|
||||
|
||||
pub struct RenderContext<'a> {
|
||||
pub output_form: OutputForm,
|
||||
pub value: DateTime<Utc>,
|
||||
pub tz_offset: FixedOffset,
|
||||
pub tz_name: &'a str,
|
||||
pub view: &'a str,
|
||||
}
|
||||
|
||||
pub fn render_template(context: RenderContext) -> Result<String, tera::Error> {
|
||||
let mut template_context = Context::new();
|
||||
let formatter = Formatter::new();
|
||||
|
||||
template_context.insert("text", match context.output_form {
|
||||
OutputForm::Relative => formatter.convert_chrono(context.value, Utc::now()),
|
||||
OutputForm::Absolute => context.value.to_rfc3339()
|
||||
}.as_str());
|
||||
|
||||
TEMPLATES.render("basic.svg", &template_context)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user