srctrait_note/
template.rs1use std::borrow::Cow;
2
3use srctrait_common_chronox::DateTimeFormat;
4
5use crate::*;
6
7pub(crate) const TMPL_TODAY: &'static str = include_str!("../assets/note-templates/today.md.tmpl");
8pub(crate) const TMPL_IDEA: &'static str = include_str!("../assets/note-templates/idea.md.tmpl");
9pub(crate) const TMPL_TODO: &'static str = include_str!("../assets/note-templates/todo.md.tmpl");
10pub(crate) const TMPL_PLAN: &'static str = include_str!("../assets/note-templates/plan.md.tmpl");
11pub(crate) const TMPL_PLAN_TOPIC: &'static str = include_str!("../assets/note-templates/plan-topic.md.tmpl");
12
13impl NoteType {
14 pub fn default_template_str(&self) -> &'static str {
15 match self {
16 NoteType::Today(_) => TMPL_TODAY,
17 NoteType::Idea(_) => TMPL_IDEA,
18 NoteType::Todo(_) => TMPL_TODO,
19 NoteType::Plan(topic) => if topic.is_some() {
20 TMPL_PLAN_TOPIC
21 } else {
22 TMPL_PLAN
23 },
24 }
25 }
26}
27
28pub fn render_template_str(tmpl: &str, vars: Vec<(&'static str, Cow<'_, str>)>) -> String {
29 let mut output = tmpl.to_string();
30 for (var, value) in vars {
31 output = output.replace(&format!("{{{{{var}}}}}"), &value);
32 }
33
34 output
35}
36
37pub fn build_template_vars<'c>(date: &Date, topic: Option<&'c str>) -> Vec<(&'static str, Cow<'c, str>)> {
38 Vec::from([
39 ("long-date", Cow::Owned(date.display(DateTimeFormat::Long).to_string())),
40 ("date", Cow::Owned(date.display(DateTimeFormat::YmdDash).to_string())),
41 ("topic", Cow::Borrowed(topic.unwrap_or_default()))
42 ])
43}