zorto 0.24.0

The AI-native static site generator (SSG) with executable code blocks
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
//! Built-in site templates for `zorto init`.

use std::path::Path;

/// A file to be written during site initialization.
struct TemplateFile {
    /// Relative path from the site root.
    path: &'static str,
    /// File contents.
    content: &'static str,
    /// Whether the file should be executable (bin/ scripts).
    executable: bool,
}

// ── Default (starter) template ──────────────────────────────────────────

const DEFAULT_FILES: &[TemplateFile] = &[
    TemplateFile {
        path: "config.toml",
        content: include_str!("../templates/default/config.toml"),
        executable: false,
    },
    TemplateFile {
        path: "content/_index.md",
        content: include_str!("../templates/default/content/_index.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/posts/_index.md",
        content: include_str!("../templates/default/content/posts/_index.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/posts/hello.md",
        content: include_str!("../templates/default/content/posts/hello.md"),
        executable: false,
    },
];

// ── Blog template ───────────────────────────────────────────────────────

const BLOG_FILES: &[TemplateFile] = &[
    TemplateFile {
        path: "config.toml",
        content: include_str!("../templates/blog/config.toml"),
        executable: false,
    },
    TemplateFile {
        path: "content/_index.md",
        content: include_str!("../templates/blog/content/_index.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/posts/_index.md",
        content: include_str!("../templates/blog/content/posts/_index.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/posts/hello-world.md",
        content: include_str!("../templates/blog/content/posts/hello-world.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/posts/getting-started.md",
        content: include_str!("../templates/blog/content/posts/getting-started.md"),
        executable: false,
    },
];

// ── Docs template ───────────────────────────────────────────────────────

const DOCS_FILES: &[TemplateFile] = &[
    TemplateFile {
        path: "config.toml",
        content: include_str!("../templates/docs/config.toml"),
        executable: false,
    },
    TemplateFile {
        path: "content/_index.md",
        content: include_str!("../templates/docs/content/_index.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/guide/_index.md",
        content: include_str!("../templates/docs/content/guide/_index.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/guide/introduction.md",
        content: include_str!("../templates/docs/content/guide/introduction.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/guide/installation.md",
        content: include_str!("../templates/docs/content/guide/installation.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/guide/configuration.md",
        content: include_str!("../templates/docs/content/guide/configuration.md"),
        executable: false,
    },
];

// ── Business template ────────────────────────────────────────────────────

const BUSINESS_FILES: &[TemplateFile] = &[
    TemplateFile {
        path: "config.toml",
        content: include_str!("../templates/business/config.toml"),
        executable: false,
    },
    TemplateFile {
        path: "content/_index.md",
        content: include_str!("../templates/business/content/_index.md"),
        executable: false,
    },
];

// ── Presentation template ────────────────────────────────────────────────

const PRESENTATION_FILES: &[TemplateFile] = &[
    TemplateFile {
        path: "config.toml",
        content: include_str!("../templates/presentation/config.toml"),
        executable: false,
    },
    TemplateFile {
        path: "content/_index.md",
        content: include_str!("../templates/presentation/content/_index.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/intro/_index.md",
        content: include_str!("../templates/presentation/content/intro/_index.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/intro/title.md",
        content: include_str!("../templates/presentation/content/intro/title.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/intro/bullets.md",
        content: include_str!("../templates/presentation/content/intro/bullets.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/intro/code.md",
        content: include_str!("../templates/presentation/content/intro/code.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/intro/fragments.md",
        content: include_str!("../templates/presentation/content/intro/fragments.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/intro/columns.md",
        content: include_str!("../templates/presentation/content/intro/columns.md"),
        executable: false,
    },
    TemplateFile {
        path: "content/intro/thank-you.md",
        content: include_str!("../templates/presentation/content/intro/thank-you.md"),
        executable: false,
    },
    TemplateFile {
        path: "templates/base.html",
        content: include_str!("../templates/presentation/templates/base.html"),
        executable: false,
    },
    TemplateFile {
        path: "templates/index.html",
        content: include_str!("../templates/presentation/templates/index.html"),
        executable: false,
    },
    TemplateFile {
        path: "templates/section.html",
        content: include_str!("../templates/presentation/templates/section.html"),
        executable: false,
    },
    TemplateFile {
        path: "templates/page.html",
        content: include_str!("../templates/presentation/templates/page.html"),
        executable: false,
    },
    TemplateFile {
        path: "templates/presentation.html",
        content: include_str!("../templates/presentation/templates/presentation.html"),
        executable: false,
    },
];

/// Template metadata for interactive selection.
pub struct TemplateInfo {
    pub name: &'static str,
    pub description: &'static str,
}

/// Available templates with descriptions.
pub const TEMPLATES: &[TemplateInfo] = &[
    TemplateInfo {
        name: "default",
        description: "Clean starter site",
    },
    TemplateInfo {
        name: "blog",
        description: "Blog with example posts",
    },
    TemplateInfo {
        name: "docs",
        description: "Documentation site",
    },
    TemplateInfo {
        name: "business",
        description: "Business / landing page",
    },
    TemplateInfo {
        name: "presentation",
        description: "reveal.js slide deck (one file per slide)",
    },
];

/// Available template names.
pub const TEMPLATE_NAMES: &[&str] = &["default", "blog", "docs", "business", "presentation"];

/// Write all files for the given template into `target`.
pub fn write_template(target: &Path, template: &str) -> anyhow::Result<()> {
    let files = match template {
        "default" => DEFAULT_FILES,
        "blog" => BLOG_FILES,
        "docs" => DOCS_FILES,
        "business" => BUSINESS_FILES,
        "presentation" => PRESENTATION_FILES,
        _ => anyhow::bail!(
            "unknown template \"{template}\". Available templates: {}",
            TEMPLATE_NAMES.join(", ")
        ),
    };

    for file in files {
        let dest = target.join(file.path);
        if let Some(parent) = dest.parent() {
            std::fs::create_dir_all(parent)?;
        }
        std::fs::write(&dest, file.content)?;

        #[cfg(unix)]
        if file.executable {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&dest, std::fs::Permissions::from_mode(0o755))?;
        }
    }

    // Ensure static/ directory exists even if no static files are in the template.
    std::fs::create_dir_all(target.join("static"))?;

    Ok(())
}

/// Rewrite the config.toml at `target` with user-provided values.
///
/// This does a simple string replacement on the template-generated config.
pub fn customize_config(
    target: &Path,
    title: &str,
    base_url: &str,
    theme: Option<&str>,
    author: Option<&str>,
) -> anyhow::Result<()> {
    let config_path = target.join("config.toml");
    let content = std::fs::read_to_string(&config_path)?;

    let mut lines: Vec<String> = Vec::new();
    let mut has_theme = false;

    for line in content.lines() {
        if line.starts_with("base_url") {
            lines.push(format!("base_url = \"{base_url}\""));
        } else if line.starts_with("title") && !line.starts_with("title =")
            || line.starts_with("title =")
        {
            lines.push(format!("title = \"{}\"", title.replace('\"', "\\\"")));
        } else if line.starts_with("theme") {
            has_theme = true;
            if let Some(t) = theme {
                lines.push(format!("theme = \"{t}\""));
            } else {
                lines.push(line.to_string());
            }
        } else {
            lines.push(line.to_string());
        }
    }

    // If theme was requested but not already in the config, insert it after base_url
    if let Some(t) = theme {
        if !has_theme {
            if let Some(pos) = lines.iter().position(|l| l.starts_with("base_url")) {
                lines.insert(pos + 1, format!("theme = \"{t}\""));
            }
        }
    }

    // If author was provided and template has an [extra] section, update it
    if let Some(author_name) = author {
        if let Some(pos) = lines.iter().position(|l| l.starts_with("author =")) {
            lines[pos] = format!("author = \"{}\"", author_name.replace('\"', "\\\""));
        }
    }

    // Update copyright_html with the actual site name and (optionally)
    // author. When no author is provided we drop the "by X" segment
    // entirely rather than baking in a literal "Author" placeholder.
    if let Some(pos) = lines
        .iter()
        .position(|l| l.trim_start().starts_with("copyright_html"))
    {
        let safe_title = title.replace('\'', "&#39;");
        let by_segment = match author {
            Some(a) => format!(" by {}", a.replace('\'', "&#39;")),
            None => String::new(),
        };
        lines[pos] = format!(
            "copyright_html = '<a href=\"/\">{safe_title}</a>{by_segment} via <a href=\"https://zorto.dev\" target=\"_blank\" rel=\"noopener\">Zorto</a>'"
        );
    }

    let mut output = lines.join("\n");
    if !output.ends_with('\n') {
        output.push('\n');
    }
    std::fs::write(&config_path, output)?;
    Ok(())
}

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

    fn write_minimal_config(target: &Path) {
        std::fs::create_dir_all(target).unwrap();
        std::fs::write(
            target.join("config.toml"),
            "base_url = \"https://example.com\"\n\
             title = \"X\"\n\
             [extra]\n\
             copyright_html = '<a href=\"/\">X</a> via <a href=\"https://zorto.dev\" target=\"_blank\" rel=\"noopener\">Zorto</a>'\n",
        )
        .unwrap();
    }

    #[test]
    fn customize_config_omits_by_when_no_author() {
        let dir = tempfile::tempdir().unwrap();
        write_minimal_config(dir.path());
        customize_config(dir.path(), "My Site", "https://example.com", None, None).unwrap();
        let cfg = std::fs::read_to_string(dir.path().join("config.toml")).unwrap();
        assert!(
            cfg.contains("My Site</a> via"),
            "expected `<title></a> via` with no `by` segment, got:\n{cfg}"
        );
        assert!(
            !cfg.contains(" by "),
            "expected no ` by ` segment when author is None, got:\n{cfg}"
        );
        assert!(
            !cfg.contains("Author"),
            "expected no literal `Author` placeholder, got:\n{cfg}"
        );
    }

    #[test]
    fn customize_config_includes_by_when_author_provided() {
        let dir = tempfile::tempdir().unwrap();
        write_minimal_config(dir.path());
        customize_config(
            dir.path(),
            "My Site",
            "https://example.com",
            None,
            Some("Alice"),
        )
        .unwrap();
        let cfg = std::fs::read_to_string(dir.path().join("config.toml")).unwrap();
        assert!(
            cfg.contains("by Alice via"),
            "expected `by Alice via`, got:\n{cfg}"
        );
    }

    #[test]
    fn customize_config_escapes_apostrophe_in_author() {
        let dir = tempfile::tempdir().unwrap();
        write_minimal_config(dir.path());
        customize_config(
            dir.path(),
            "My Site",
            "https://example.com",
            None,
            Some("O'Brien"),
        )
        .unwrap();
        let cfg = std::fs::read_to_string(dir.path().join("config.toml")).unwrap();
        // Apostrophes in the author would otherwise close the TOML
        // literal-string copyright_html value.
        assert!(
            cfg.contains("O&#39;Brien"),
            "expected apostrophe to be HTML-encoded, got:\n{cfg}"
        );
    }
}