sherwood 0.3.0

A static site generator with built-in development server
Documentation
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use anyhow::{Result, anyhow};
use include_dir::{Dir, include_dir};
use sailfish::{TemplateOnce, runtime::RenderError};
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, thiserror::Error)]
pub enum TemplateError {
    #[error("Template not found: {template_name}")]
    TemplateNotFound { template_name: String },

    #[error("Invalid template format: {template_name} - {details}")]
    InvalidTemplate {
        template_name: String,
        details: String,
    },

    #[error("Template validation failed: {template_name} - {reason}")]
    ValidationFailed {
        template_name: String,
        reason: String,
    },

    #[error("No templates directory found at: {path}")]
    TemplatesDirectoryNotFound { path: String },

    #[error("Template compilation error: {template_name} - {source}")]
    CompilationError {
        template_name: String,
        #[source]
        source: RenderError,
    },
}

// Embed templates directory at compile time
static TEMPLATES: Dir = include_dir!("$CARGO_MANIFEST_DIR/templates");

#[derive(TemplateOnce)]
#[template(path = "default.stpl")]
struct PageTemplate {
    title: String,
    content: String,
    css_file: Option<String>,
    body_attrs: String,
}

#[derive(TemplateOnce)]
#[template(path = "blog_post.stpl")]
struct BlogPostTemplate {
    title: String,
    url: String,
    date: Option<String>,
    excerpt: Option<String>,
}

#[derive(Debug)]
pub struct TemplateInfo {
    pub name: String,
    pub path: PathBuf,
    pub size: usize,
    pub is_valid: bool,
}

pub struct TemplateManager {
    templates_dir: PathBuf,
    available_templates: Vec<String>,
}

impl TemplateManager {
    pub fn new(templates_dir: &Path) -> Result<Self> {
        let templates_dir = templates_dir.to_path_buf();

        // Templates directory is optional - we have embedded fallbacks

        let available_templates = Self::discover_templates(&templates_dir)?;

        Ok(Self {
            templates_dir,
            available_templates,
        })
    }

    /// Discover all available template files in the templates directory
    fn discover_templates(templates_dir: &Path) -> Result<Vec<String>> {
        let mut templates = Vec::new();

        // First, add embedded templates
        templates.extend(get_available_templates());

        // Then scan the templates directory for additional templates
        if templates_dir.exists() && templates_dir.is_dir() {
            for entry in fs::read_dir(templates_dir)? {
                let entry = entry?;
                let path = entry.path();

                if path.is_file()
                    && let Some(extension) = path.extension()
                    && extension == "stpl"
                    && let Some(name) = path.file_name().and_then(|n| n.to_str())
                {
                    templates.push(name.to_string());
                }
            }
        }

        // Remove duplicates and sort
        templates.sort();
        templates.dedup();

        Ok(templates)
    }

    /// Get information about all available templates
    pub fn get_template_info(&self) -> Vec<TemplateInfo> {
        self.available_templates
            .iter()
            .map(|name| {
                let path = self.templates_dir.join(name);
                let size = if path.exists() {
                    fs::metadata(&path).map(|m| m.len() as usize).unwrap_or(0)
                } else {
                    // Check embedded templates
                    TEMPLATES
                        .get_file(name)
                        .map(|f| f.contents().len())
                        .unwrap_or(0)
                };

                TemplateInfo {
                    name: name.clone(),
                    path: path.clone(),
                    size,
                    is_valid: self.validate_template(name).is_ok(),
                }
            })
            .collect()
    }

    /// Validate a specific template
    pub fn validate_template(&self, template_name: &str) -> Result<()> {
        // Check if template exists
        if !self
            .available_templates
            .contains(&template_name.to_string())
        {
            return Err(TemplateError::TemplateNotFound {
                template_name: template_name.to_string(),
            }
            .into());
        }

        // Try to read the template content
        let template_content = self.get_template_content(template_name)?;

        // Basic validation checks
        self.validate_template_syntax(template_name, &template_content)?;

        // Try to compile the template (this is a more thorough validation)
        self.validate_template_compilation(template_name)?;

        Ok(())
    }

    /// Get template content from filesystem or embedded templates
    fn get_template_content(&self, template_name: &str) -> Result<String> {
        // Try filesystem first
        let fs_path = self.templates_dir.join(template_name);
        if fs_path.exists() {
            return Ok(fs::read_to_string(&fs_path)?);
        }

        // Fall back to embedded templates
        if let Some(embedded_file) = TEMPLATES.get_file(template_name) {
            return Ok(embedded_file
                .contents_utf8()
                .ok_or_else(|| TemplateError::InvalidTemplate {
                    template_name: template_name.to_string(),
                    details: "Template contains invalid UTF-8".to_string(),
                })?
                .to_string());
        }

        Err(TemplateError::TemplateNotFound {
            template_name: template_name.to_string(),
        }
        .into())
    }

    /// Basic template syntax validation
    fn validate_template_syntax(&self, template_name: &str, content: &str) -> Result<()> {
        // Check for balanced template delimiters
        let open_count = content.matches("<%").count();
        let close_count = content.matches("%>").count();

        if open_count != close_count {
            return Err(TemplateError::ValidationFailed {
                template_name: template_name.to_string(),
                reason: format!(
                    "Unbalanced template delimiters: {} opening, {} closing",
                    open_count, close_count
                ),
            }
            .into());
        }

        // Check for obvious syntax errors
        if content.matches("<%").any(|m| m.ends_with("%>")) {
            return Err(TemplateError::ValidationFailed {
                template_name: template_name.to_string(),
                reason: "Empty template blocks found".to_string(),
            }
            .into());
        }

        Ok(())
    }

    /// Validate template by attempting compilation
    fn validate_template_compilation(&self, template_name: &str) -> Result<()> {
        // For the known templates, we can test compilation
        match template_name {
            "default.stpl" => {
                let template = PageTemplate {
                    title: "test".to_string(),
                    content: "test".to_string(),
                    css_file: Some("/test.css".to_string()),
                    body_attrs: "test".to_string(),
                };
                template
                    .render_once()
                    .map_err(|e| TemplateError::CompilationError {
                        template_name: template_name.to_string(),
                        source: e,
                    })?;
            }
            "blog_post.stpl" => {
                let template = BlogPostTemplate {
                    title: "test".to_string(),
                    url: "test".to_string(),
                    date: Some("2024-01-01".to_string()),
                    excerpt: Some("test".to_string()),
                };
                template
                    .render_once()
                    .map_err(|e| TemplateError::CompilationError {
                        template_name: template_name.to_string(),
                        source: e,
                    })?;
            }
            _ => {
                // For unknown templates, just check if they exist
                // We could potentially implement generic template validation here
            }
        }

        Ok(())
    }

    /// Validate all available templates
    pub fn validate_all_templates(&self) -> Result<Vec<String>> {
        let mut errors = Vec::new();

        for template_name in &self.available_templates {
            if let Err(e) = self.validate_template(template_name) {
                errors.push(format!("Template '{}': {}", template_name, e));
            }
        }

        if errors.is_empty() {
            println!(
                "✅ All {} templates validated successfully",
                self.available_templates.len()
            );
            Ok(errors)
        } else {
            println!("❌ Found {} template validation errors:", errors.len());
            for error in &errors {
                println!("  - {}", error);
            }
            Ok(errors)
        }
    }

    /// Get list of available template names
    pub fn get_available_templates(&self) -> Vec<String> {
        self.available_templates.clone()
    }

    /// Print template information (for debugging)
    pub fn debug_print_templates(&self) {
        println!(
            "📋 Available templates in {}:",
            self.templates_dir.display()
        );
        let info = self.get_template_info();

        for template_info in info {
            let status = if template_info.is_valid { "" } else { "" };
            println!(
                "  {} {} ({} bytes)",
                status, template_info.name, template_info.size
            );
        }

        if self.available_templates.is_empty() {
            println!("  No templates found");
        }
    }

    pub fn render_page(
        &self,
        title: &str,
        content: &str,
        css_file: Option<&str>,
        body_attrs: &str,
    ) -> Result<String> {
        // Validate template before rendering
        self.validate_template("default.stpl")?;

        let template = PageTemplate {
            title: title.to_string(),
            content: content.to_string(),
            css_file: css_file.map(|s| s.to_string()),
            body_attrs: body_attrs.to_string(),
        };

        Ok(template.render_once()?)
    }

    pub fn render_blog_post(
        &self,
        title: &str,
        url: &str,
        date: Option<&str>,
        excerpt: Option<&str>,
    ) -> Result<String> {
        // Validate template before rendering
        self.validate_template("blog_post.stpl")?;

        let template = BlogPostTemplate {
            title: title.to_string(),
            url: url.to_string(),
            date: date.map(|s| s.to_string()),
            excerpt: excerpt.map(|s| s.to_string()),
        };

        Ok(template.render_once()?)
    }

    pub fn get_template_path(&self, template_name: &str) -> PathBuf {
        self.templates_dir.join(template_name)
    }

    pub fn template_exists(&self, template_name: &str) -> bool {
        self.templates_dir.join(template_name).exists()
    }
}

pub fn copy_embedded_templates(output_dir: &Path) -> Result<()> {
    let templates_output_dir = output_dir.join("templates");
    fs::create_dir_all(&templates_output_dir)?;

    for entry in TEMPLATES.entries() {
        if let Some(file) = entry.as_file() {
            let template_name = entry
                .path()
                .file_name()
                .and_then(|n| n.to_str())
                .ok_or_else(|| anyhow::anyhow!("Invalid template name"))?;

            let output_path = templates_output_dir.join(template_name);
            fs::write(
                &output_path,
                file.contents_utf8().ok_or_else(|| {
                    anyhow::anyhow!("Template {} contains invalid UTF-8", template_name)
                })?,
            )?;
        }
    }

    Ok(())
}

pub fn get_available_templates() -> Vec<String> {
    TEMPLATES
        .files()
        .map(|file| {
            file.path()
                .file_name()
                .unwrap()
                .to_string_lossy()
                .to_string()
        })
        .collect()
}

/// Validate templates in the specified directory
pub fn validate_templates(templates_dir: &Option<PathBuf>, verbose: bool) -> Result<()> {
    let templates_path = match templates_dir {
        Some(path) => path.clone(),
        None => {
            // Default to ../templates relative to current directory
            std::env::current_dir()
                .unwrap_or_default()
                .join("../templates")
        }
    };

    match TemplateManager::new(&templates_path) {
        Ok(template_manager) => {
            if verbose {
                template_manager.debug_print_templates();
            }

            let errors = template_manager.validate_all_templates()?;
            if errors.is_empty() {
                println!("🎉 All templates are valid!");
                Ok(())
            } else {
                Err(anyhow!(
                    "Template validation failed with {} errors",
                    errors.len()
                ))
            }
        }
        Err(e) => {
            if verbose {
                eprintln!("❌ Failed to initialize template manager: {}", e);
            }
            Err(e)
        }
    }
}