marmite 0.4.0

Marmite is the easiest static site generator.
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
use serde_json::Value;
use std::fs;
use std::process::Command;
use tempfile::TempDir;

#[test]
fn test_marmite_binary_help() {
    let output = Command::new("cargo")
        .args(["run", "--quiet", "--", "--help"])
        .output()
        .expect("Failed to execute marmite");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("marmite"));
    assert!(stdout.contains("Usage:"));
}

#[test]
fn test_marmite_version() {
    let output = Command::new("cargo")
        .args(["run", "--quiet", "--", "--version"])
        .output()
        .expect("Failed to execute marmite");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("marmite"));
}

#[test]
fn test_minimal_site_generation() {
    let temp_dir = TempDir::new().unwrap();
    let input_dir = temp_dir.path().join("input");
    let output_dir = temp_dir.path().join("output");

    // Create input directory structure
    fs::create_dir_all(&input_dir).unwrap();
    fs::create_dir_all(input_dir.join("content")).unwrap();

    // Create config file
    let config_path = input_dir.join("marmite.yaml");
    fs::write(&config_path, "name: Test Site\ntagline: Test").unwrap();

    // Create a simple content file
    let content_path = input_dir.join("content").join("test.md");
    fs::write(&content_path, "# Test Page\n\nThis is a test.").unwrap();

    // Generate site using marmite binary
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            input_dir.to_str().unwrap(),
            output_dir.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute marmite");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify output
    assert!(output_dir.join("index.html").exists());
    assert!(output_dir.join("test.html").exists());
}

#[test]
fn test_site_initialization() {
    let temp_dir = TempDir::new().unwrap();
    let site_dir = temp_dir.path().join("new_site");

    // Initialize new site
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--init-site",
        ])
        .output()
        .expect("Failed to execute marmite");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Verify structure was created
    assert!(site_dir.exists());
    assert!(site_dir.join("marmite.yaml").exists());
    assert!(site_dir.join("content").exists());
    assert!(site_dir.join("content").join("media").exists());
    assert!(
        site_dir.join("content").join("pages").exists(),
        "content/pages/ should be created"
    );
    assert!(
        site_dir.join("content").join("posts").exists(),
        "content/posts/ should be created"
    );
    assert!(
        site_dir
            .join("content")
            .join("pages")
            .join("about.md")
            .exists(),
        "about.md should be in content/pages/"
    );
    assert!(
        site_dir
            .join("content")
            .join("posts")
            .join("welcome.md")
            .exists(),
        "welcome.md should be in content/posts/"
    );
    assert!(site_dir.join("custom.css").exists());
    assert!(site_dir.join("custom.js").exists());
}

#[test]
fn test_new_with_directory_flag() {
    let temp_dir = TempDir::new().unwrap();
    let site_dir = temp_dir.path().join("dir_site");

    // Initialize site first
    Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--init-site",
        ])
        .output()
        .expect("Failed to init site");

    // Create a post in a specific directory
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "Test Post",
            "-d",
            "posts",
        ])
        .output()
        .expect("Failed to create post");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: Value = serde_json::from_str(stdout.trim()).expect("Output should be valid JSON");
    let file_path = json["file"].as_str().unwrap();
    assert!(
        file_path.contains("posts"),
        "Output file path should contain 'posts' directory: {file_path}"
    );
    assert_eq!(json["title"].as_str().unwrap(), "Test Post");
    assert_eq!(json["slug"].as_str().unwrap(), "test-post");
    assert!(
        json["date"].as_str().is_some(),
        "Posts should have a date field"
    );

    // Create a page in the pages directory
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "Contact",
            "-p",
            "-d",
            "pages",
        ])
        .output()
        .expect("Failed to create page");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    assert!(
        site_dir
            .join("content")
            .join("pages")
            .join("contact.md")
            .exists(),
        "Page should be created in content/pages/"
    );

    // Create in a new directory (should auto-create it)
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "Tutorial One",
            "-d",
            "tutorials",
        ])
        .output()
        .expect("Failed to create in new dir");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    assert!(
        site_dir.join("content").join("tutorials").exists(),
        "Directory should be auto-created"
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: Value = serde_json::from_str(stdout.trim()).expect("Output should be valid JSON");
    let file_path = json["file"].as_str().unwrap();
    assert!(
        file_path.contains("tutorials"),
        "Output file path should contain 'tutorials' directory: {file_path}"
    );
}

#[test]
fn test_new_json_output_format() {
    let temp_dir = TempDir::new().unwrap();
    let site_dir = temp_dir.path().join("json_site");

    Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--init-site",
        ])
        .output()
        .expect("Failed to init site");

    // Create a post and verify JSON output
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "My Post",
            "-t",
            "rust,web",
        ])
        .output()
        .expect("Failed to create post");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: Value = serde_json::from_str(stdout.trim()).expect("Output should be valid JSON");
    assert_eq!(json["title"].as_str().unwrap(), "My Post");
    assert_eq!(json["slug"].as_str().unwrap(), "my-post");
    assert!(json["file"].as_str().unwrap().ends_with(".md"));
    assert!(json["date"].as_str().is_some());
    assert_eq!(json["tags"].as_str().unwrap(), "rust,web");

    // Create a page and verify no date field
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "About",
            "-p",
        ])
        .output()
        .expect("Failed to create page");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: Value = serde_json::from_str(stdout.trim()).expect("Output should be valid JSON");
    assert_eq!(json["title"].as_str().unwrap(), "About");
    assert!(json["date"].is_null(), "Pages should not have a date field");
}

#[test]
fn test_new_with_lang_standalone() {
    let temp_dir = TempDir::new().unwrap();
    let site_dir = temp_dir.path().join("lang_site");

    Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--init-site",
        ])
        .output()
        .expect("Failed to init site");

    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "Ola Mundo",
            "--lang",
            "pt",
        ])
        .output()
        .expect("Failed to create post with lang");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: Value = serde_json::from_str(stdout.trim()).expect("Output should be valid JSON");
    assert_eq!(json["language"].as_str().unwrap(), "pt");

    let file_path = json["file"].as_str().unwrap();
    let content = fs::read_to_string(file_path).unwrap();
    assert!(
        content.contains("language: pt"),
        "Frontmatter should contain language field"
    );
}

#[test]
fn test_new_with_invalid_lang() {
    let temp_dir = TempDir::new().unwrap();
    let site_dir = temp_dir.path().join("invalid_lang_site");

    Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--init-site",
        ])
        .output()
        .expect("Failed to init site");

    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "Test",
            "--lang",
            "xyz",
        ])
        .output()
        .expect("Failed to run command");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Invalid language code"),
        "Should report invalid language code: {stderr}"
    );
}

#[test]
fn test_new_translation_subfolder() {
    let temp_dir = TempDir::new().unwrap();
    let site_dir = temp_dir.path().join("trans_sub_site");

    Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--init-site",
        ])
        .output()
        .expect("Failed to init site");

    // Create original post in a subfolder
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "Hello World",
            "-p",
            "-d",
            "hello-world",
        ])
        .output()
        .expect("Failed to create original post");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // Create translation - should auto-place in the same subfolder
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "Ola Mundo",
            "--lang",
            "pt",
            "--translates",
            "hello-world",
        ])
        .output()
        .expect("Failed to create translation");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: Value = serde_json::from_str(stdout.trim()).expect("Output should be valid JSON");
    assert_eq!(json["language"].as_str().unwrap(), "pt");
    assert_eq!(json["translates"].as_str().unwrap(), "hello-world");

    let file_path = json["file"].as_str().unwrap();
    assert!(
        file_path.contains("hello-world/pt-ola-mundo.md"),
        "Translation should be in the original's subfolder with lang prefix: {file_path}"
    );
    assert!(
        std::path::Path::new(file_path).exists(),
        "Translation file should exist"
    );

    // Verify frontmatter does NOT include translates (subfolder grouping handles it)
    let content = fs::read_to_string(file_path).unwrap();
    assert!(content.contains("language: pt"));
    assert!(
        !content.contains("translates:"),
        "Subfolder translations should not have translates frontmatter"
    );
}

#[test]
fn test_new_translation_root_level() {
    let temp_dir = TempDir::new().unwrap();
    let site_dir = temp_dir.path().join("trans_root_site");

    Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--init-site",
        ])
        .output()
        .expect("Failed to init site");

    // Create original post at root level (page so filename is predictable)
    Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "About Page",
            "-p",
        ])
        .output()
        .expect("Failed to create original page");

    // Create translation
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "Pagina Sobre",
            "-p",
            "--lang",
            "pt",
            "--translates",
            "about-page",
        ])
        .output()
        .expect("Failed to create translation");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    let json: Value = serde_json::from_str(stdout.trim()).expect("Output should be valid JSON");

    let file_path = json["file"].as_str().unwrap();
    let content = fs::read_to_string(file_path).unwrap();
    assert!(content.contains("language: pt"));
    assert!(
        content.contains("translates: about-page"),
        "Root-level translations should have translates frontmatter: {content}"
    );
}

#[test]
fn test_new_translation_nonexistent_slug() {
    let temp_dir = TempDir::new().unwrap();
    let site_dir = temp_dir.path().join("trans_noexist_site");

    Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--init-site",
        ])
        .output()
        .expect("Failed to init site");

    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            site_dir.to_str().unwrap(),
            "--new",
            "Translation",
            "--lang",
            "pt",
            "--translates",
            "nonexistent-slug",
        ])
        .output()
        .expect("Failed to run command");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("Cannot find content with slug"),
        "Should report missing original content: {stderr}"
    );
}

#[test]
fn test_show_urls_command() {
    let temp_dir = TempDir::new().unwrap();
    let input_dir = temp_dir.path().join("input");

    // Create directory structure
    fs::create_dir_all(&input_dir).unwrap();
    fs::create_dir_all(input_dir.join("content")).unwrap();

    // Create config
    let config_path = input_dir.join("marmite.yaml");
    fs::write(&config_path, "name: Test\nurl: https://example.com").unwrap();

    // Create a few content files
    fs::write(input_dir.join("content").join("page1.md"), "# Page 1").unwrap();
    fs::write(input_dir.join("content").join("page2.md"), "# Page 2").unwrap();

    // Run show-urls command
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            input_dir.to_str().unwrap(),
            "--show-urls",
        ])
        .output()
        .expect("Failed to execute marmite");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("https://example.com/page1.html"));
    assert!(stdout.contains("https://example.com/page2.html"));
    assert!(stdout.contains("https://example.com/index.html"));
}

#[test]
fn test_standard_site_atproto_generation() {
    let temp_dir = TempDir::new().unwrap();
    let input_dir = temp_dir.path().join("input");
    let output_dir = temp_dir.path().join("output");

    // Create input directory structure
    fs::create_dir_all(&input_dir).unwrap();
    fs::create_dir_all(input_dir.join("content")).unwrap();

    // Create config file with atproto configured
    let config_path = input_dir.join("marmite.yaml");
    fs::write(
        &config_path,
        r#"name: Test ATProto Blog
url: https://myblog.com
atproto:
  handle: test.bsky.social
  publication_uri: at://did:plc:123/site.standard.publication/456
"#,
    )
    .unwrap();

    // Create a post
    let content_path = input_dir.join("content").join("first-post.md");
    fs::write(&content_path, "# First Post\n\nHello standard.site!").unwrap();

    // Create a mock state file
    let state_path = input_dir.join(".marmite-atproto-state.json");
    fs::write(
        &state_path,
        r#"{"posts":{"first-post":{"content_hash":"abc","at_uri":"at://did:plc:123/site.standard.document/first-post","last_published":"2026-06-17T17:08:42Z"}}}"#
    ).unwrap();

    // Generate site using marmite binary
    let output = Command::new("cargo")
        .args([
            "run",
            "--quiet",
            "--",
            input_dir.to_str().unwrap(),
            output_dir.to_str().unwrap(),
        ])
        .output()
        .expect("Failed to execute marmite");

    assert!(
        output.status.success(),
        "Command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    // 1. Verify verification file was generated under .well-known
    let wk_file = output_dir
        .join(".well-known")
        .join("site.standard.publication");
    assert!(wk_file.exists());
    let wk_content = fs::read_to_string(&wk_file).unwrap();
    assert_eq!(
        wk_content.trim(),
        "at://did:plc:123/site.standard.publication/456"
    );

    // 2. Verify base layout has the link rel="site.standard.publication" tag
    let index_file = output_dir.join("index.html");
    assert!(index_file.exists());
    let index_content = fs::read_to_string(&index_file).unwrap();
    assert!(index_content.contains(r#"<link rel="site.standard.publication" href="at://did:plc:123/site.standard.publication/456">"#));

    // 3. Verify post has the link rel="site.standard.document" tag
    let post_file = output_dir.join("first-post.html");
    assert!(post_file.exists());
    let post_content = fs::read_to_string(&post_file).unwrap();
    assert!(post_content.contains(r#"<link rel="site.standard.document" href="at://did:plc:123/site.standard.document/first-post">"#));
}