2 use std::path::PathBuf;
5 const TXT_TEMPLATE: &'static str = include_str!("../templates/index.txt");
6 const HTML_TEMPLATE: &'static str = include_str!("../templates/index.html");
7 const GMI_TEMPLATE: &'static str = include_str!("../templates/index.gmi");
8 const RSS_TEMPLATE: &'static str = include_str!("../templates/feed.xml");
14 DisplayDirective { content: String },
15 ConditionalDirective { condition: String, children: Vec<Token>},
16 IteratorDirective { collection: String, member_label: String, children: Vec<Token> }
19 impl std::fmt::Display for Token {
20 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
22 Token::Text(label) => write!(f, "Text {}", label),
23 Token::DisplayDirective{content} => write!(f, "DisplayDirective {}", content),
24 Token::ConditionalDirective{condition, children} => {
25 write!(f, "ConditionalDirective {} [[[\n", condition)?;
26 for child in children {
27 write!(f, "\t{}\n", child)?;
31 Token::IteratorDirective{collection, member_label, children} => {
32 write!(f, "IteratorDirective {}: {} [[[\n", collection, member_label)?;
33 for child in children {
34 write!(f, "\t{}\n", child)?;
42 pub struct ParsedTemplate {
43 pub tokens: Vec<Token>
46 pub fn parse(template: &str) -> ParsedTemplate {
47 let mut tokens = Vec::new();
48 tokenize(template, &mut tokens);
54 fn tokenize(template: &str, tokens: &mut Vec<Token>) {
55 let mut remaining_template = template;
57 while !remaining_template.is_empty() && remaining_template.contains("{{") {
58 let directive_start_index = remaining_template.find("{{")
59 .expect("Was expecting at least one tag opener");
60 if directive_start_index > 0 {
61 let text = remaining_template[..directive_start_index].to_string();
62 tokens.push(Token::Text(text.to_string()));
64 remaining_template = &remaining_template[directive_start_index..];
66 let directive_end_index = remaining_template.find("}}")
67 .expect("Was expecting }} after {{") + 2;
68 let directive = &remaining_template[..directive_end_index];
69 remaining_template = &remaining_template[directive_end_index..];
71 let directive_type = directive.chars().nth(2).unwrap();
72 match directive_type {
75 let content = directive[3..directive.len() - 2].trim();
76 tokens.push(Token::DisplayDirective{
77 content: content.to_string()
82 let content = directive[3..directive.len() - 2].trim();
83 let mut children = Vec::new();
85 match directive_type {
87 let closing_block = remaining_template.find("{{?}}").unwrap();
88 let directive_block = &remaining_template[..closing_block];
89 remaining_template = &remaining_template[closing_block + 5..];
90 tokenize(directive_block, &mut children);
91 tokens.push(Token::ConditionalDirective{
92 condition: content.to_string(),
97 let parts: Vec<_> = content.splitn(2, ':').collect();
98 let closing_block = remaining_template.find("{{~}}").unwrap();
99 let directive_block = &remaining_template[..closing_block];
100 remaining_template = &remaining_template[closing_block + 5..];
101 tokenize(directive_block, &mut children);
102 if parts.len() == 2 {
103 tokens.push(Token::IteratorDirective {
104 collection: parts[0].trim().to_string(),
105 member_label: parts[1].trim().to_string(),
116 tokens.push(Token::Text(remaining_template.to_string()));
121 pub fn find(template_directory: &PathBuf, filename: &str) -> Option<String> {
122 let template_path = template_directory.join(filename);
123 if template_path.exists() {
124 let mut contents = String::new();
125 if File::open(template_path).ok()?.read_to_string(&mut contents).is_ok() {
126 return Some(contents);
129 find_default(filename)
132 fn find_default(filename: &str) -> Option<String> {
134 "index.txt" => Some(TXT_TEMPLATE.to_string()),
135 "index.html" => Some(HTML_TEMPLATE.to_string()),
136 "index.gmi" => Some(GMI_TEMPLATE.to_string()),
137 "index.rss" => Some(RSS_TEMPLATE.to_string()),