typstify-generator 0.1.3

Static site generation engine
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
//! HTML template system for page generation.
//!
//! Provides a lightweight template system using string interpolation rather than
//! heavy template engines like Tera or Handlebars.

use std::collections::HashMap;

use thiserror::Error;

/// Template rendering errors.
#[derive(Debug, Error)]
pub enum TemplateError {
    /// Missing required variable.
    #[error("missing required variable: {0}")]
    MissingVariable(String),

    /// Template not found.
    #[error("template not found: {0}")]
    NotFound(String),

    /// Invalid template syntax.
    #[error("invalid template syntax: {0}")]
    InvalidSyntax(String),
}

/// Result type for template operations.
pub type Result<T> = std::result::Result<T, TemplateError>;

/// Template context with variables for interpolation.
#[derive(Debug, Clone, Default)]
pub struct TemplateContext {
    variables: HashMap<String, String>,
}

impl TemplateContext {
    /// Create a new empty context.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert a variable into the context.
    pub fn insert(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.variables.insert(key.into(), value.into());
    }

    /// Create context with initial variables.
    pub fn with_var(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.insert(key, value);
        self
    }

    /// Get a variable value.
    #[must_use]
    pub fn get(&self, key: &str) -> Option<&str> {
        self.variables.get(key).map(String::as_str)
    }

    /// Check if a variable exists.
    #[must_use]
    pub fn contains(&self, key: &str) -> bool {
        self.variables.contains_key(key)
    }
}

/// A simple template that supports variable interpolation.
///
/// Variables are specified as `{{ variable_name }}` in the template string.
#[derive(Debug, Clone)]
pub struct Template {
    name: String,
    content: String,
}

impl Template {
    /// Create a new template with the given name and content.
    #[must_use]
    pub fn new(name: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            content: content.into(),
        }
    }

    /// Get the template name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Render the template with the given context.
    ///
    /// Replaces all `{{ variable }}` placeholders with values from context.
    pub fn render(&self, context: &TemplateContext) -> Result<String> {
        let mut result = self.content.clone();
        let mut pos = 0;

        while let Some(start) = result[pos..].find("{{") {
            let start = pos + start;
            let end = result[start..]
                .find("}}")
                .ok_or_else(|| TemplateError::InvalidSyntax("unclosed {{ delimiter".to_string()))?;
            let end = start + end + 2;

            let var_name = result[start + 2..end - 2].trim();

            // Check for optional variable syntax: {{ variable? }}
            let (var_name, optional) = if let Some(stripped) = var_name.strip_suffix('?') {
                (stripped, true)
            } else {
                (var_name, false)
            };

            let value = match context.get(var_name) {
                Some(v) => v.to_string(),
                None if optional => String::new(),
                None => return Err(TemplateError::MissingVariable(var_name.to_string())),
            };

            result.replace_range(start..end, &value);
            pos = start + value.len();
        }

        Ok(result)
    }
}

/// Registry of templates.
#[derive(Debug, Clone, Default)]
pub struct TemplateRegistry {
    templates: HashMap<String, Template>,
}

impl TemplateRegistry {
    /// Create a new registry with default templates.
    #[must_use]
    pub fn new() -> Self {
        let mut registry = Self::default();
        registry.register_defaults();
        registry
    }

    /// Register default built-in templates.
    fn register_defaults(&mut self) {
        self.register(Template::new("base", DEFAULT_BASE_TEMPLATE));
        self.register(Template::new("page", DEFAULT_PAGE_TEMPLATE));
        self.register(Template::new("post", DEFAULT_POST_TEMPLATE));
        self.register(Template::new("list", DEFAULT_LIST_TEMPLATE));
        self.register(Template::new("taxonomy", DEFAULT_TAXONOMY_TEMPLATE));
        self.register(Template::new("redirect", DEFAULT_REDIRECT_TEMPLATE));
        self.register(Template::new("tags_index", DEFAULT_TAGS_INDEX_TEMPLATE));
        self.register(Template::new(
            "categories_index",
            DEFAULT_CATEGORIES_INDEX_TEMPLATE,
        ));
        self.register(Template::new("archives", DEFAULT_ARCHIVES_TEMPLATE));
        self.register(Template::new("section", DEFAULT_SECTION_TEMPLATE));
    }

    /// Register a template.
    pub fn register(&mut self, template: Template) {
        self.templates.insert(template.name.clone(), template);
    }

    /// Get a template by name.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&Template> {
        self.templates.get(name)
    }

    /// Render a named template with the given context.
    pub fn render(&self, name: &str, context: &TemplateContext) -> Result<String> {
        let template = self
            .get(name)
            .ok_or_else(|| TemplateError::NotFound(name.to_string()))?;
        template.render(context)
    }
}

/// Default base HTML template.
/// Uses external CSS and JS files for better caching and smaller HTML files.
pub const DEFAULT_BASE_TEMPLATE: &str = r##"<!DOCTYPE html>
<html lang="{{ lang }}" class="scroll-smooth">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ title }}{{ site_title_suffix? }}</title>
    <meta name="description" content="{{ description? }}">
    <meta name="author" content="{{ author? }}">
    <link rel="canonical" href="{{ canonical_url }}">
    {{ hreflang? }}
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/assets/style.css">
    {{ custom_css? }}
    <script>
        // Inline critical JS to prevent FOUC (Flash of Unstyled Content)
        (function() {
            const saved = localStorage.getItem('theme');
            const theme = saved || (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
            document.documentElement.setAttribute('data-theme', theme);
        })();
    </script>
</head>
<body>
    <header>
        <div class="container">
            <nav>
                <a href="{{ nav_home_url }}" class="site-title">{{ site_title }}</a>
                <div class="nav-links">
                    <a href="{{ nav_posts_url }}">Posts</a>
                    <a href="{{ nav_archives_url }}">Archives</a>
                    <a href="{{ nav_tags_url }}">Tags</a>
                    <a href="{{ nav_about_url }}">About</a>
                    <div class="nav-actions">
                        <div class="search-wrapper" id="searchWrapper">
                            <input type="text" class="search-input" id="searchInput" placeholder="Search..." autocomplete="off">
                            <button class="search-btn" id="searchBtn" aria-label="Search" type="button">
                                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z" />
                                </svg>
                            </button>
                            <div class="search-results" id="searchResults"></div>
                        </div>
                        {{ lang_switcher? }}
                        <button class="theme-toggle" aria-label="Toggle theme" type="button">
                            <svg class="icon-sun" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z" />
                            </svg>
                            <svg class="icon-moon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z" />
                            </svg>
                        </button>
                    </div>
                </div>
            </nav>
        </div>
    </header>
    <main>
        <div class="container">
            {{ content }}
        </div>
    </main>
    <footer>
        <div class="container">
            <p>&copy; {{ year }} {{ site_title }}. Built with <a href="https://github.com/longcipher/typstify">Typstify</a>.</p>
        </div>
    </footer>
    <script src="/assets/main.js" defer></script>
    {{ custom_js? }}
</body>
</html>"##;

/// Default page template (for standalone pages).
pub const DEFAULT_PAGE_TEMPLATE: &str = r#"<article class="page">
    <h1>{{ title }}</h1>
    <div class="content">
        {{ content }}
    </div>
</article>"#;

/// Default post template (for blog posts with metadata).
pub const DEFAULT_POST_TEMPLATE: &str = r#"<article class="post">
    <header>
        <h1>{{ title }}</h1>
        <time datetime="{{ date_iso }}">{{ date_formatted }}</time>
        {{ tags_html? }}
    </header>
    <div class="content">
        {{ content }}
    </div>
</article>"#;

/// Default list template (for index pages).
pub const DEFAULT_LIST_TEMPLATE: &str = r#"<section class="post-list">
    <h1>{{ title }}</h1>
    <ul>
        {{ items }}
    </ul>
    <div class="pagination">{{ pagination? }}</div>
</section>"#;

/// Default taxonomy term template (for tag/category pages).
pub const DEFAULT_TAXONOMY_TEMPLATE: &str = r#"<section class="taxonomy post-list">
    <h1>{{ taxonomy_name }}: <span>{{ term }}</span></h1>
    <ul>
        {{ items }}
    </ul>
    <div class="pagination">{{ pagination? }}</div>
</section>"#;

/// Default redirect template for URL aliases.
pub const DEFAULT_REDIRECT_TEMPLATE: &str = r#"<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="refresh" content="0; url={{ redirect_url }}">
    <link rel="canonical" href="{{ redirect_url }}">
    <title>Redirecting...</title>
</head>
<body>
    <p>Redirecting to <a href="{{ redirect_url }}">{{ redirect_url }}</a></p>
</body>
</html>"#;

/// Default tags index template (lists all tags with counts).
pub const DEFAULT_TAGS_INDEX_TEMPLATE: &str = r#"<section class="taxonomy-index">
    <h1>Tags</h1>
    <div class="tags-cloud">
        {{ items }}
    </div>
</section>"#;

/// Default categories index template (lists all categories with counts).
pub const DEFAULT_CATEGORIES_INDEX_TEMPLATE: &str = r#"<section class="taxonomy-index">
    <h1>Categories</h1>
    <ul class="categories-list">
        {{ items }}
    </ul>
</section>"#;

/// Default archives template (lists all posts grouped by year).
pub const DEFAULT_ARCHIVES_TEMPLATE: &str = r#"<section class="archives">
    <h1>Archives</h1>
    {{ items }}
</section>"#;

/// Default section template (lists all posts in a section).
pub const DEFAULT_SECTION_TEMPLATE: &str = r#"<section class="section-list post-list">
    <h1>{{ title }}</h1>
    <p class="section-description">{{ description? }}</p>
    <ul>
        {{ items }}
    </ul>
    <div class="pagination">{{ pagination? }}</div>
</section>"#;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_template_simple_render() {
        let template = Template::new("test", "Hello, {{ name }}!");
        let mut ctx = TemplateContext::new();
        ctx.insert("name", "World");

        let result = template.render(&ctx).unwrap();
        assert_eq!(result, "Hello, World!");
    }

    #[test]
    fn test_template_multiple_variables() {
        let template = Template::new(
            "test",
            "{{ greeting }}, {{ name }}! Welcome to {{ place }}.",
        );
        let ctx = TemplateContext::new()
            .with_var("greeting", "Hello")
            .with_var("name", "User")
            .with_var("place", "Typstify");

        let result = template.render(&ctx).unwrap();
        assert_eq!(result, "Hello, User! Welcome to Typstify.");
    }

    #[test]
    fn test_template_optional_variable() {
        let template = Template::new("test", "Hello{{ suffix? }}!");
        let ctx = TemplateContext::new();

        let result = template.render(&ctx).unwrap();
        assert_eq!(result, "Hello!");

        let ctx = TemplateContext::new().with_var("suffix", ", World");
        let result = template.render(&ctx).unwrap();
        assert_eq!(result, "Hello, World!");
    }

    #[test]
    fn test_template_missing_required_variable() {
        let template = Template::new("test", "Hello, {{ name }}!");
        let ctx = TemplateContext::new();

        let result = template.render(&ctx);
        assert!(matches!(result, Err(TemplateError::MissingVariable(_))));
    }

    #[test]
    fn test_template_registry() {
        let registry = TemplateRegistry::new();

        assert!(registry.get("base").is_some());
        assert!(registry.get("page").is_some());
        assert!(registry.get("post").is_some());
        assert!(registry.get("list").is_some());
        assert!(registry.get("nonexistent").is_none());
    }

    #[test]
    fn test_render_base_template() {
        let registry = TemplateRegistry::new();
        let ctx = TemplateContext::new()
            .with_var("lang", "en")
            .with_var("title", "My Page")
            .with_var("canonical_url", "https://example.com/my-page")
            .with_var("content", "<p>Hello!</p>")
            .with_var("site_title", "My Site")
            .with_var("year", "2026")
            // Navigation URLs
            .with_var("nav_home_url", "/")
            .with_var("nav_posts_url", "/posts")
            .with_var("nav_archives_url", "/archives")
            .with_var("nav_tags_url", "/tags")
            .with_var("nav_about_url", "/about");

        let result = registry.render("base", &ctx).unwrap();
        assert!(result.contains("<!DOCTYPE html>"));
        assert!(result.contains("<title>My Page</title>"));
        assert!(result.contains("<p>Hello!</p>"));
    }
}