1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::RwLock;
use crate::Value as Obj;
use crate::{tokenize, Token};
use super::compile::compile;
/// Manages template loading, caching, and rendering
pub struct TemplateManager {
/// Base directory for template files
template_dir: PathBuf,
/// Cache of parsed templates
template_cache: Arc<RwLock<HashMap<String, Vec<Token>>>>,
/// Maximum recursion depth for template inheritance
max_recursion_depth: u32,
/// Cache enabled flag
cache_enabled: bool,
}
impl TemplateManager {
/// Creates a new TemplateManager instance
///
/// # Arguments
/// * `template_dir` - Path to the directory containing template files
pub fn new<P: AsRef<Path>>(template_dir: P) -> Self {
TemplateManager {
template_dir: template_dir.as_ref().to_path_buf(),
template_cache: Arc::new(RwLock::new(HashMap::new())),
max_recursion_depth: 10,
cache_enabled: true,
}
}
/// Get a full dir of a path
pub fn get_template_path(&self, path: &str) -> PathBuf {
self.template_dir.join(path)
}
/// Enables or disables template caching
pub fn with_caching(mut self, enabled: bool) -> Self {
self.cache_enabled = enabled;
self
}
/// Set the maximum recursion depth for template inheritance
pub fn with_max_recursion_depth(mut self, depth: u32) -> Self {
self.max_recursion_depth = depth;
self
}
/// Add the template's tokens to the cache. If it already exists, it will be replaced.
pub fn load_into_cache(&self, template_name: &str) {
if self.cache_enabled {
let mut cache = self.template_cache.write().unwrap();
if let Ok(tokens) = self.read_template_token(template_name) {
cache.insert(template_name.to_string(), tokens);
} else {
eprintln!("Failed to load template into cache: {}", template_name);
}
}
}
/// Get the tokenized template from the cache. If it doesn't exist, it will returns a none
pub fn get_from_cache(&self, template_name: &str) -> Option<Vec<Token>> {
if self.cache_enabled {
let cache = self.template_cache.read().unwrap();
return cache.get(template_name).cloned();
}
None
}
/// Load tokens from a template file, using cache if enabled
pub fn load_tokens(&self, template_name: &str) -> Result<Vec<Token>, String> {
if self.cache_enabled {
if let Some(tokens) = self.get_from_cache(template_name) {
return Ok(tokens);
}
}
let tokens = self.read_template_token(template_name)?;
self.load_into_cache(template_name);
Ok(tokens)
}
/// Tokenizes the template file
pub fn read_template_token(&self, template_name: &str) -> Result<Vec<Token>, String> {
match self.read_template_content(template_name) {
Ok(content) => {
return Ok(tokenize(&content))
},
Err(e) => Err(e),
}
}
/// Get the string content of a template file
fn read_template_content(&self, template_name: &str) -> Result<String, String> {
fs::read_to_string(
&self.get_template_path(template_name)
).map_err(
|e| format!("Failed to read template '{}': {}", template_name, e)
)
}
/// Loads and renders a template by name
pub fn render(&self, template_name: &str, data: &HashMap<String, Obj>) -> Result<String, String> {
// Get all template tokens needed (with inheritance resolution)
let tokens = self.expand_template(self.load_tokens(template_name)?, template_name, &mut 0);
// Use your existing compile function to process these tokens
compile(tokens, data.clone())
}
pub fn render_string(&self, template_str: String, data: &HashMap<String, Obj>) -> Result<String, String> {
// Tokenize the string content
let tokens = tokenize(&template_str);
// Insert template content into the token stream
let tokens = self.expand_template(tokens, "", &mut 0);
// Use your existing compile function to process these tokens
compile(tokens, data.clone())
}
pub fn expand_template(&self, tokens: Vec<Token>, self_dir: &str, recursion_count: &mut u32) -> Vec<Token> {
if *recursion_count > self.max_recursion_depth {
return vec![Token::HtmlContent(format!("<!-- Template Error: Maximum recursion depth exceeded -->"))];
}
*recursion_count += 1; // Increment recursion count
// Insert template content into the token stream
let tokens = self.insert_template(tokens, self_dir, recursion_count);
// Extend with parent template if applicable
match self.extend_with_parent(tokens, self_dir, recursion_count) {
Ok(tokens) => tokens,
Err(e) => vec![Token::HtmlContent(format!("<!-- Template Error: {} -->", e))],
}
}
pub fn insert_template(&self, mut tokens: Vec<Token>, self_dir: &str, recursion_count: &mut u32) -> Vec<Token> {
let mut i = 0;
while i < tokens.len() {
if matches!(tokens.get(i), Some(Token::InsertKeyword)) &&
i + 1 < tokens.len() {
if let Some(Token::Object(Obj::Str(name))) = tokens.get(i + 1) {
// Get the full path of the template name
let full_path = get_full_dir(name, self_dir);
// Find the position to insert template content (after EndOfStatement)
let mut j = i + 2;
let mut found_end = false;
while j < tokens.len() {
if matches!(tokens[j], Token::EndOfStatement) {
found_end = true;
break;
}
j += 1;
}
// Only proceed if we found the end statement
if found_end {
// Load template tokens without unnecessary clone
let tokens_to_insert = match self.load_tokens(&full_path) {
Ok(template_tokens) => self.expand_template(template_tokens, &full_path, recursion_count),
Err(e) => {
// More informative error token
vec![Token::HtmlContent(format!("<!-- Template Error: {} - {} -->", full_path, e))]
}
};
// Calculate new position before modifying vector
let new_position = j + 1 + tokens_to_insert.len();
// Insert the tokens after EndOfStatement
tokens.splice(j + 1..j + 1, tokens_to_insert);
// Adjust index to account for inserted tokens
i = new_position - 1;
continue; // Skip the increment at the end
}
}
}
i += 1; // Move to the next token
}
tokens // Return the modified tokens vector
}
pub fn extend_with_parent(&self, tokens: Vec<Token>, self_dir: &str, recursion_count: &mut u32) -> Result<Vec<Token>, String> {
// Check if this template extends another one
if let Some(parent_name) = self.extract_parent_template_name(&tokens) {
// Deal with the dir
let parent_name = get_full_dir(&parent_name, self_dir);
// Load the parent template
let parent_tokens = self.expand_template(self.load_tokens(&parent_name)?, &parent_name, recursion_count);
// Extract blocks from both parent and child
let parent_blocks = self.extract_blocks(&parent_tokens)?;
let child_blocks = self.extract_blocks(&tokens)?;
// Create merged blocks where child overrides parent
let mut merged_blocks = parent_blocks;
for (name, tokens) in child_blocks {
merged_blocks.insert(name, tokens);
}
// Create the final template by replacing blocks in parent with merged blocks
self.create_template_with_blocks(&parent_tokens, merged_blocks)
} else {
// No inheritance, just process blocks within this template
let blocks = self.extract_blocks(&tokens)?;
self.create_template_with_blocks(&tokens, blocks)
}
}
/// Extracts the parent template name if this template extends another
fn extract_parent_template_name(&self, tokens: &[Token]) -> Option<String> {
for i in 0..tokens.len() {
if let Token::TemplateKeyword = &tokens[i] {
if i + 1 < tokens.len() {
if let Token::Object(Obj::Str(name)) = &tokens[i + 1] {
return Some(name.clone());
}
}
break;
}
}
None
}
/// Extracts all blocks from a token stream
fn extract_blocks(&self, tokens: &[Token]) -> Result<HashMap<String, Vec<Token>>, String> {
let mut blocks = HashMap::new();
let mut i = 0;
while i < tokens.len() {
if let Token::BlockKeyword = tokens[i] {
if i + 1 < tokens.len() {
if let Token::Identifier(name) = &tokens[i + 1] {
let block_name = name.clone();
i += 2; // Skip block keyword and name
// Skip EndOfStatement if present
if i < tokens.len() && matches!(tokens[i], Token::EndOfStatement) {
i += 1;
}
let start = i;
let mut depth = 1;
let mut end_pos = i;
// Find matching endblock
while end_pos < tokens.len() && depth > 0 {
match &tokens[end_pos] {
Token::BlockKeyword => depth += 1,
Token::EndBlockKeyword => {
depth -= 1;
if depth == 0 {
break;
}
},
_ => {}
}
end_pos += 1;
}
if depth > 0 {
return Err(format!("Unterminated block: {}", block_name));
}
// Extract block content (excluding endblock token)
let content = tokens[start..end_pos].to_vec();
blocks.insert(block_name, content);
i = end_pos + 1; // Skip past EndBlockKeyword
continue;
}
}
}
i += 1;
}
Ok(blocks)
}
/// Creates a processed template with all blocks properly defined
fn create_template_with_blocks(
&self,
template_tokens: &[Token],
blocks: HashMap<String, Vec<Token>>
) -> Result<Vec<Token>, String> {
let mut result = Vec::new();
let mut i = 0;
while i < template_tokens.len() {
match &template_tokens[i] {
// Skip template directive since we've already handled inheritance
Token::TemplateKeyword => {
// Skip template keyword and the template name string
i += 2;
},
// Add block definition with its identifier
Token::BlockKeyword => {
if i + 1 < template_tokens.len() {
if let Token::Identifier(name) = &template_tokens[i + 1] {
// Add BlockKeyword and its name
result.push(template_tokens[i].clone());
result.push(template_tokens[i + 1].clone());
// Skip to after block name
i += 2;
// Skip EndOfStatement if present
if i < template_tokens.len() && matches!(template_tokens[i], Token::EndOfStatement) {
result.push(template_tokens[i].clone());
i += 1;
}
// Add the block content from our blocks map
if let Some(block_tokens) = blocks.get(name) {
result.extend_from_slice(block_tokens);
}
// Skip to after the endblock
let mut depth = 1;
while i < template_tokens.len() && depth > 0 {
match template_tokens[i] {
Token::BlockKeyword => depth += 1,
Token::EndBlockKeyword => {
depth -= 1;
if depth == 0 {
// Add EndBlockKeyword
result.push(template_tokens[i].clone());
break;
}
},
_ => {}
}
i += 1;
}
i += 1; // Move past EndBlockKeyword
} else {
result.push(template_tokens[i].clone());
i += 1;
}
} else {
result.push(template_tokens[i].clone());
i += 1;
}
},
_ => {
// Copy all other tokens as-is
result.push(template_tokens[i].clone());
i += 1;
}
}
}
Ok(result)
}
}
impl Default for TemplateManager {
fn default() -> Self {
TemplateManager::new("./template")
}
}
/// To see whether the dir is a relative dir or a absolute dir
/// If absolute dir, return the path
/// If relative dir, return the path with the ori path
pub fn get_full_dir(path: &str, ori: &str) -> String {
if path.starts_with("/") || path.starts_with("\\") {
// Return path without the first character
let path = path.trim_start_matches("/").trim_start_matches("\\").to_string();
return path
} else {
// Remove everything after the last separator in the original path
let ori = if let Some(pos) = ori.rfind('/') {
&ori[..pos]
} else if let Some(pos) = ori.rfind('\\') {
&ori[..pos]
} else {
ori
};
// Handle relative path
// First check if the original path ends with a separator
if ori.ends_with("/") || ori.ends_with("\\") {
format!("{}{}", ori, path)
} else {
// Add a separator between ori and path
format!("{}/{}", ori, path)
}
}
}