1use crate::PreprocessorContext;
2use regex::Regex;
3use std::collections::HashMap;
4
5use super::Template;
6
7pub struct KeybindingTemplate;
8
9impl KeybindingTemplate {
10 pub fn new() -> Self {
11 KeybindingTemplate
12 }
13}
14
15impl Template for KeybindingTemplate {
16 fn key(&self) -> &'static str {
17 "kb"
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("action".to_string(), args.trim().to_string());
27 map
28 }
29
30 fn render(&self, context: &PreprocessorContext, args: &HashMap<String, String>) -> String {
31 let action = args.get("action").map(String::as_str).unwrap_or("");
32 let macos_binding = context.find_binding("macos", action).unwrap_or_default();
33 let linux_binding = context.find_binding("linux", action).unwrap_or_default();
34
35 if macos_binding.is_empty() && linux_binding.is_empty() {
36 return "<div>No default binding</div>".to_string();
37 }
38
39 format!("<kbd class=\"keybinding\">{macos_binding}|{linux_binding}</kbd>")
40 }
41}