You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.3 KiB
Rust
107 lines
3.3 KiB
Rust
4 years ago
|
use crate::elements::*;
|
||
4 years ago
|
use chrono::prelude::*;
|
||
4 years ago
|
use regex::Regex;
|
||
4 years ago
|
|
||
|
macro_rules! block {
|
||
|
($inner:expr) => {
|
||
|
Element::Block(Box::new($inner))
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#[allow(unused)]
|
||
4 years ago
|
macro_rules! line {
|
||
4 years ago
|
($inner:expr) => {
|
||
4 years ago
|
Element::Line(Box::new($inner))
|
||
4 years ago
|
};
|
||
|
}
|
||
|
|
||
4 years ago
|
macro_rules! inline {
|
||
4 years ago
|
($inner:expr) => {
|
||
4 years ago
|
Element::Inline(Box::new($inner))
|
||
4 years ago
|
};
|
||
|
}
|
||
|
|
||
|
pub(crate) trait ProcessPlaceholders {
|
||
|
fn process_placeholders(&mut self);
|
||
4 years ago
|
fn process_definitions(&mut self);
|
||
|
}
|
||
|
|
||
|
const S_VALUE: &str = "value";
|
||
|
|
||
4 years ago
|
const P_TOC: &str = "toc";
|
||
|
const P_DATE: &str = "date";
|
||
|
const P_TIME: &str = "time";
|
||
|
const P_DATETIME: &str = "datetime";
|
||
|
|
||
|
impl ProcessPlaceholders for Document {
|
||
|
/// parses all placeholders and assigns values to them
|
||
|
fn process_placeholders(&mut self) {
|
||
|
self.placeholders.iter().for_each(|p| {
|
||
4 years ago
|
let mut pholder = p.write().unwrap();
|
||
4 years ago
|
match pholder.name.to_lowercase().as_str() {
|
||
4 years ago
|
P_TOC => {
|
||
|
let ordered = if let Some(meta) = &pholder.metadata {
|
||
|
meta.get_bool("ordered")
|
||
|
} else {
|
||
|
false
|
||
|
};
|
||
|
pholder.set_value(block!(Block::List(self.create_toc(ordered))))
|
||
|
}
|
||
4 years ago
|
P_DATE => pholder.set_value(inline!(Inline::Plain(PlainText {
|
||
4 years ago
|
value: get_date_string()
|
||
|
}))),
|
||
4 years ago
|
P_TIME => pholder.set_value(inline!(Inline::Plain(PlainText {
|
||
4 years ago
|
value: get_time_string()
|
||
|
}))),
|
||
4 years ago
|
P_DATETIME => pholder.set_value(inline!(Inline::Plain(PlainText {
|
||
4 years ago
|
value: format!("{} {}", get_date_string(), get_time_string())
|
||
|
}))),
|
||
4 years ago
|
_ => {
|
||
|
if let Some(entry) = self.config.get_entry(pholder.name.to_lowercase().as_str())
|
||
|
{
|
||
|
let value = entry.get().as_string();
|
||
|
pholder.set_value(inline!(Inline::Plain(PlainText { value })))
|
||
|
}
|
||
|
}
|
||
4 years ago
|
}
|
||
|
})
|
||
|
}
|
||
4 years ago
|
|
||
|
fn process_definitions(&mut self) {
|
||
|
lazy_static::lazy_static! {
|
||
|
static ref RE_SET: Regex = Regex::new(r"^set:(.*)$").unwrap();
|
||
|
}
|
||
|
|
||
|
let placeholders = self.placeholders.clone();
|
||
|
placeholders.iter().for_each(|p| {
|
||
4 years ago
|
let mut pholder = p.write().unwrap();
|
||
4 years ago
|
let name = pholder.name.clone();
|
||
|
|
||
|
if let Some(cap) = RE_SET.captures(&name) {
|
||
|
if let Some(key) = cap.get(1) {
|
||
|
let key: &str = key.as_str();
|
||
|
pholder.value = Some(inline!(Inline::Plain(PlainText {
|
||
|
value: "".to_string()
|
||
|
})));
|
||
|
if let Some(meta) = &pholder.metadata {
|
||
|
if let Some(value) = meta.data.get(S_VALUE) {
|
||
4 years ago
|
self.config.set_from_meta(key, value.clone())
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
fn get_time_string() -> String {
|
||
|
let now = Local::now();
|
||
4 years ago
|
format!("{:02}:{:02}:{:02}", now.hour(), now.minute(), now.second())
|
||
4 years ago
|
}
|
||
|
|
||
|
fn get_date_string() -> String {
|
||
|
let now = Local::now();
|
||
|
format!("{:02}.{:02}.{:04}", now.day(), now.month(), now.year())
|
||
|
}
|