ssg 0.0.38

A Content-First Open Source Static Site Generator (SSG) crafted in Rust.
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright © 2023 - 2026 Static Site Generator (SSG). All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Draft filtering plugin.
//!
//! Removes content files with `draft: true` in their frontmatter
//! before compilation, unless the `--drafts` flag is set.

use crate::plugin::{Plugin, PluginContext};
use crate::MAX_DIR_DEPTH;
use anyhow::Result;
use std::{
    fs,
    path::{Path, PathBuf},
};

/// Plugin that filters draft content before compilation.
///
/// In `before_compile`, scans content files for `draft: true` in
/// frontmatter and renames them to `.md.draft` so staticdatagen
/// skips them. In `after_compile`, restores the originals.
#[derive(Debug, Clone, Copy)]
pub struct DraftPlugin {
    include_drafts: bool,
}

impl DraftPlugin {
    /// Creates a new `DraftPlugin`.
    ///
    /// If `include_drafts` is true, draft files are left in place.
    #[must_use]
    pub const fn new(include_drafts: bool) -> Self {
        Self { include_drafts }
    }
}

impl Plugin for DraftPlugin {
    fn name(&self) -> &'static str {
        "drafts"
    }

    fn before_compile(&self, ctx: &PluginContext) -> Result<()> {
        if self.include_drafts || !ctx.content_dir.exists() {
            return Ok(());
        }

        let md_files = collect_md_files(&ctx.content_dir)?;
        let mut hidden = 0usize;

        for path in &md_files {
            if is_draft(path)? {
                let draft_path = path.with_extension("md.draft");
                fs::rename(path, &draft_path)?;
                hidden += 1;
            }
        }

        if hidden > 0 {
            log::info!(
                "[drafts] Hidden {hidden} draft file(s) (use --drafts to include)"
            );
        }
        Ok(())
    }

    fn after_compile(&self, ctx: &PluginContext) -> Result<()> {
        if self.include_drafts || !ctx.content_dir.exists() {
            return Ok(());
        }

        // Restore hidden drafts
        let draft_files = collect_draft_files(&ctx.content_dir)?;
        for draft_path in &draft_files {
            let original = draft_path.with_extension("");
            if !original.exists() {
                fs::rename(draft_path, &original)?;
            }
        }
        Ok(())
    }
}

/// Checks if a Markdown file has `draft: true` in its frontmatter.
fn is_draft(path: &Path) -> Result<bool> {
    let content = fs::read_to_string(path)?;

    // Quick check: look for draft field in YAML frontmatter
    if !content.starts_with("---") {
        return Ok(false);
    }

    // Find the closing ---
    if let Some(end) = content[3..].find("---") {
        let frontmatter = &content[3..3 + end];
        // Check for draft: true (handles various YAML formats)
        for line in frontmatter.lines() {
            let trimmed = line.trim();
            if trimmed == "draft: true"
                || trimmed == "draft: True"
                || trimmed == "draft: TRUE"
                || trimmed == "draft: yes"
            {
                return Ok(true);
            }
        }
    }

    Ok(false)
}

fn collect_md_files(dir: &Path) -> Result<Vec<PathBuf>> {
    crate::walk::walk_files_bounded_depth(dir, "md", MAX_DIR_DEPTH)
}

fn collect_draft_files(dir: &Path) -> Result<Vec<PathBuf>> {
    crate::walk::walk_files_bounded_depth(dir, "draft", MAX_DIR_DEPTH)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::test_support::init_logger;
    use std::path::PathBuf;
    use tempfile::{tempdir, TempDir};

    // -------------------------------------------------------------------
    // Test fixtures
    // -------------------------------------------------------------------

    /// Builds `<root>/content` and returns the temp dir guard, the
    /// content path, and a `PluginContext` rooted at it.
    fn make_content_layout() -> (TempDir, PathBuf, PluginContext) {
        init_logger();
        let dir = tempdir().expect("create tempdir");
        let content = dir.path().join("content");
        fs::create_dir_all(&content).expect("mkdir content");
        let ctx =
            PluginContext::new(&content, dir.path(), dir.path(), dir.path());
        (dir, content, ctx)
    }

    /// Writes a Markdown file with the given frontmatter draft flag.
    fn write_md(dir: &Path, name: &str, draft_value: Option<&str>) {
        let body = match draft_value {
            Some(v) => format!("---\ntitle: T\ndraft: {v}\n---\nbody"),
            None => "---\ntitle: T\n---\nbody".to_string(),
        };
        fs::write(dir.join(name), body).expect("write md");
    }

    // -------------------------------------------------------------------
    // Constructor + derive surface
    // -------------------------------------------------------------------

    #[test]
    fn new_table_driven_constructs_plugin_with_supplied_flag() {
        let cases = [(true, true), (false, false)];
        for (input, expected) in cases {
            let plugin = DraftPlugin::new(input);
            assert_eq!(
                plugin.include_drafts, expected,
                "include_drafts({input}) should be {expected}"
            );
        }
    }

    #[test]
    fn draft_plugin_is_copy_after_move() {
        // Guards the `Copy` derive added in v0.0.34.
        let plugin = DraftPlugin::new(false);
        let _copy = plugin;
        assert_eq!(plugin.name(), "drafts");
    }

    #[test]
    fn name_returns_static_drafts_identifier() {
        assert_eq!(DraftPlugin::new(false).name(), "drafts");
        assert_eq!(DraftPlugin::new(true).name(), "drafts");
    }

    // -------------------------------------------------------------------
    // is_draft — table-driven over every YAML truthy spelling
    // -------------------------------------------------------------------

    #[test]
    fn is_draft_table_driven_truthy_values_return_true() {
        let cases: &[&str] = &[
            "---\ntitle: T\ndraft: true\n---\n",
            "---\ntitle: T\ndraft: True\n---\n",
            "---\ntitle: T\ndraft: TRUE\n---\n",
            "---\ntitle: T\ndraft: yes\n---\n",
            // leading/trailing whitespace on the line is trimmed
            "---\ntitle: T\n  draft: true  \n---\n",
        ];
        let dir = tempdir().expect("tempdir");
        for (i, body) in cases.iter().enumerate() {
            let path = dir.path().join(format!("d{i}.md"));
            fs::write(&path, body).unwrap();
            assert!(
                is_draft(&path).unwrap(),
                "case {i} {body:?} should be detected as draft"
            );
        }
    }

    #[test]
    fn is_draft_table_driven_falsy_values_return_false() {
        let cases: &[&str] = &[
            // explicit false
            "---\ntitle: T\ndraft: false\n---\n",
            // unrecognised value
            "---\ntitle: T\ndraft: maybe\n---\n",
            // missing field entirely
            "---\ntitle: T\n---\n",
            // YAML 1.2 strict — `True` is not the same as `true`
            // in many parsers; we accept it (covered above) but
            // `tRue` and `Yes` are NOT in the accepted list.
            "---\ntitle: T\ndraft: tRue\n---\n",
            "---\ntitle: T\ndraft: Yes\n---\n",
        ];
        let dir = tempdir().expect("tempdir");
        for (i, body) in cases.iter().enumerate() {
            let path = dir.path().join(format!("p{i}.md"));
            fs::write(&path, body).unwrap();
            assert!(
                !is_draft(&path).unwrap(),
                "case {i} {body:?} should NOT be detected as draft"
            );
        }
    }

    #[test]
    fn is_draft_no_frontmatter_returns_false() {
        // The `!content.starts_with("---")` early return at line 88.
        let dir = tempdir().expect("tempdir");
        let path = dir.path().join("plain.md");
        fs::write(&path, "# No frontmatter\nJust prose.\n").unwrap();
        assert!(!is_draft(&path).unwrap());
    }

    #[test]
    fn is_draft_unterminated_frontmatter_returns_false() {
        // The `if let Some(end) = ...find("---")` branch at line 93
        // must take the implicit `None` path when the closing `---`
        // is missing — function should return Ok(false), not error.
        let dir = tempdir().expect("tempdir");
        let path = dir.path().join("unterminated.md");
        fs::write(&path, "---\ntitle: T\ndraft: true\nno closing fence here\n")
            .unwrap();
        assert!(!is_draft(&path).unwrap());
    }

    #[test]
    fn is_draft_empty_file_returns_false() {
        let dir = tempdir().expect("tempdir");
        let path = dir.path().join("empty.md");
        fs::write(&path, "").unwrap();
        assert!(!is_draft(&path).unwrap());
    }

    #[test]
    fn is_draft_missing_file_returns_err() {
        let dir = tempdir().expect("tempdir");
        let result = is_draft(&dir.path().join("does-not-exist.md"));
        assert!(result.is_err());
    }

    // -------------------------------------------------------------------
    // before_compile — short-circuit paths
    // -------------------------------------------------------------------

    #[test]
    fn before_compile_with_include_drafts_does_not_rename_anything() {
        let (_tmp, content, ctx) = make_content_layout();
        write_md(&content, "draft.md", Some("true"));
        write_md(&content, "published.md", None);

        DraftPlugin::new(true).before_compile(&ctx).unwrap();
        assert!(content.join("draft.md").exists());
        assert!(!content.join("draft.md.draft").exists());
        assert!(content.join("published.md").exists());
    }

    #[test]
    fn before_compile_missing_content_dir_returns_ok() {
        // The `!ctx.content_dir.exists()` short-circuit at line 43.
        let dir = tempdir().expect("tempdir");
        let missing = dir.path().join("missing-content");
        let ctx =
            PluginContext::new(&missing, dir.path(), dir.path(), dir.path());

        DraftPlugin::new(false)
            .before_compile(&ctx)
            .expect("missing content dir is not an error");
        assert!(!missing.exists());
    }

    #[test]
    fn before_compile_renames_only_drafts_leaves_published_intact() {
        let (_tmp, content, ctx) = make_content_layout();
        write_md(&content, "draft.md", Some("true"));
        write_md(&content, "published.md", None);

        DraftPlugin::new(false).before_compile(&ctx).unwrap();
        assert!(!content.join("draft.md").exists());
        assert!(content.join("draft.md.draft").exists());
        assert!(content.join("published.md").exists());
    }

    #[test]
    fn before_compile_recurses_into_subdirectories() {
        // collect_md_files walks the tree — drafts in nested dirs
        // must also be hidden.
        let (_tmp, content, ctx) = make_content_layout();
        let nested = content.join("blog").join("2026");
        fs::create_dir_all(&nested).unwrap();
        write_md(&nested, "secret.md", Some("true"));
        write_md(&content, "live.md", None);

        DraftPlugin::new(false).before_compile(&ctx).unwrap();
        assert!(nested.join("secret.md.draft").exists());
        assert!(!nested.join("secret.md").exists());
        assert!(content.join("live.md").exists());
    }

    #[test]
    fn before_compile_no_drafts_yields_no_renames() {
        let (_tmp, content, ctx) = make_content_layout();
        write_md(&content, "a.md", None);
        write_md(&content, "b.md", Some("false"));

        DraftPlugin::new(false).before_compile(&ctx).unwrap();
        assert!(content.join("a.md").exists());
        assert!(content.join("b.md").exists());
    }

    // -------------------------------------------------------------------
    // after_compile — restoration paths
    // -------------------------------------------------------------------

    #[test]
    fn after_compile_with_include_drafts_short_circuits() {
        // The `self.include_drafts` short-circuit at line 67 must not
        // attempt to restore anything.
        let (_tmp, content, ctx) = make_content_layout();
        // Pre-place a .draft file to prove it is NOT touched.
        fs::write(content.join("ghost.md.draft"), "---\n---\n").unwrap();

        DraftPlugin::new(true).after_compile(&ctx).unwrap();
        assert!(content.join("ghost.md.draft").exists());
        assert!(!content.join("ghost.md").exists());
    }

    #[test]
    fn after_compile_missing_content_dir_returns_ok() {
        let dir = tempdir().expect("tempdir");
        let missing = dir.path().join("missing");
        let ctx =
            PluginContext::new(&missing, dir.path(), dir.path(), dir.path());
        DraftPlugin::new(false).after_compile(&ctx).unwrap();
    }

    #[test]
    fn after_compile_restores_draft_extension_to_md() {
        let (_tmp, content, ctx) = make_content_layout();
        fs::write(content.join("post.md.draft"), "---\n---\n").unwrap();

        DraftPlugin::new(false).after_compile(&ctx).unwrap();
        assert!(content.join("post.md").exists());
        assert!(!content.join("post.md.draft").exists());
    }

    #[test]
    fn after_compile_does_not_overwrite_existing_original() {
        // The `if !original.exists()` guard at line 75 must skip the
        // rename when an original-named file is already present.
        // Otherwise we'd silently clobber user content.
        let (_tmp, content, ctx) = make_content_layout();
        fs::write(content.join("post.md"), "USER WROTE THIS").unwrap();
        fs::write(content.join("post.md.draft"), "STALE DRAFT").unwrap();

        DraftPlugin::new(false).after_compile(&ctx).unwrap();
        let body = fs::read_to_string(content.join("post.md")).unwrap();
        assert_eq!(
            body, "USER WROTE THIS",
            "existing original must not be clobbered"
        );
        assert!(
            content.join("post.md.draft").exists(),
            "stale draft is left in place when original exists"
        );
    }

    #[test]
    fn before_and_after_round_trip_restores_original_content() {
        // End-to-end: hide a draft, then restore it, and prove the
        // file is byte-identical to before.
        let (_tmp, content, ctx) = make_content_layout();
        let payload = "---\ntitle: T\ndraft: true\n---\nDRAFT BODY";
        fs::write(content.join("d.md"), payload).unwrap();

        let plugin = DraftPlugin::new(false);
        plugin.before_compile(&ctx).unwrap();
        plugin.after_compile(&ctx).unwrap();

        let restored = fs::read_to_string(content.join("d.md")).unwrap();
        assert_eq!(restored, payload);
    }

    // -------------------------------------------------------------------
    // collect_md_files / collect_draft_files — recursion + filtering
    // -------------------------------------------------------------------

    #[test]
    fn collect_md_files_returns_empty_for_missing_directory() {
        let dir = tempdir().expect("tempdir");
        let result = collect_md_files(&dir.path().join("missing")).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn collect_md_files_filters_non_md_extensions() {
        let dir = tempdir().expect("tempdir");
        fs::write(dir.path().join("a.md"), "").unwrap();
        fs::write(dir.path().join("b.txt"), "").unwrap();
        fs::write(dir.path().join("c.html"), "").unwrap();

        let result = collect_md_files(dir.path()).unwrap();
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn collect_md_files_recurses_into_nested_subdirectories() {
        let dir = tempdir().expect("tempdir");
        let nested = dir.path().join("a").join("b");
        fs::create_dir_all(&nested).unwrap();
        fs::write(dir.path().join("top.md"), "").unwrap();
        fs::write(nested.join("deep.md"), "").unwrap();

        let result = collect_md_files(dir.path()).unwrap();
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn collect_draft_files_filters_non_draft_extensions() {
        let dir = tempdir().expect("tempdir");
        fs::write(dir.path().join("a.md.draft"), "").unwrap();
        fs::write(dir.path().join("b.md"), "").unwrap();

        let result = collect_draft_files(dir.path()).unwrap();
        assert_eq!(result.len(), 1);
    }

    #[test]
    fn collect_draft_files_respects_max_dir_depth_guard() {
        // The `depth > MAX_DIR_DEPTH` continue at line 135 is only
        // reached with a tree deeper than the limit. Closes line 136.
        let dir = tempdir().expect("tempdir");
        let mut current = dir.path().to_path_buf();
        for i in 0..MAX_DIR_DEPTH + 2 {
            current = current.join(format!("d{i}"));
            fs::create_dir_all(&current).unwrap();
            fs::write(current.join("p.md.draft"), "").unwrap();
        }
        let result = collect_draft_files(dir.path()).unwrap();
        // At most MAX_DIR_DEPTH+1 files (depths 0..=MAX) survive.
        assert!(result.len() <= MAX_DIR_DEPTH + 1);
    }

    #[test]
    fn collect_md_files_respects_max_dir_depth_guard() {
        let dir = tempdir().expect("tempdir");
        let mut current = dir.path().to_path_buf();
        for i in 0..MAX_DIR_DEPTH + 2 {
            current = current.join(format!("d{i}"));
            fs::create_dir_all(&current).unwrap();
            fs::write(current.join("p.md"), "").unwrap();
        }
        let result = collect_md_files(dir.path()).unwrap();
        assert!(result.len() <= MAX_DIR_DEPTH + 1);
    }

    #[test]
    fn collect_draft_files_recurses_into_nested_subdirectories() {
        let dir = tempdir().expect("tempdir");
        let nested = dir.path().join("a");
        fs::create_dir_all(&nested).unwrap();
        fs::write(dir.path().join("top.md.draft"), "").unwrap();
        fs::write(nested.join("nested.md.draft"), "").unwrap();

        let result = collect_draft_files(dir.path()).unwrap();
        assert_eq!(result.len(), 2);
    }
}