action.rs

 1use crate::PreprocessorContext;
 2use regex::Regex;
 3use std::collections::HashMap;
 4
 5use super::Template;
 6
 7pub struct ActionTemplate;
 8
 9impl ActionTemplate {
10    pub fn new() -> Self {
11        ActionTemplate
12    }
13}
14
15impl Template for ActionTemplate {
16    fn key(&self) -> &'static str {
17        "action"
18    }
19
20    fn regex(&self) -> Regex {
21        Regex::new(&format!(r"\{{#{}(.*?)\}}", self.key())).unwrap()
22    }
23
24    fn parse_args(&self, args: &str) -> HashMap<String, String> {
25        let mut map = HashMap::new();
26        map.insert("name".to_string(), args.trim().to_string());
27        map
28    }
29
30    fn render(&self, _context: &PreprocessorContext, args: &HashMap<String, String>) -> String {
31        let name = args.get("name").map(String::as_str).unwrap_or_default();
32
33        let formatted_name = name
34            .chars()
35            .enumerate()
36            .map(|(i, c)| {
37                if i > 0 && c.is_uppercase() {
38                    format!(" {}", c.to_lowercase())
39                } else {
40                    c.to_string()
41                }
42            })
43            .collect::<String>()
44            .trim()
45            .to_string()
46            .replace("::", ":");
47
48        format!("<code class=\"hljs\">{}</code>", formatted_name)
49    }
50}