bim 0.1.0

A content-first static site generator for blogs, digital gardens, and knowledge bases
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
//! Complete integration tests for the bim static site generator
//!
//! These tests verify the full functionality of the package including
//! content discovery, rendering, and all major features.

use bim::collections::Collection;
use bim::config::Config;
use bim::content::{Page, Site};
use bim::discovery::discover_site;
use bim::render::build_site;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

/// Creates a minimal test site structure
fn create_test_site() -> TempDir {
    let dir = TempDir::new().unwrap();
    let root = dir.path();

    // Create blog directory with a post (using [#tag] syntax)
    fs::create_dir_all(root.join("blog")).unwrap();
    fs::write(
        root.join("blog/2024-01-15-hello-world.md"),
        "# Hello World\n\nThis is my first blog post.\n\n[#rust] [#programming]",
    )
    .unwrap();

    // Create notes directory (using standard markdown link syntax)
    fs::create_dir_all(root.join("notes/rust")).unwrap();
    fs::write(
        root.join("notes/rust/ownership.md"),
        "# Rust Ownership\n\nOwnership is Rust's key feature.\n\nSee also: [hello world](/blog/hello-world)",
    )
    .unwrap();

    // Create index page
    fs::write(
        root.join("index.md"),
        "# My Site\n\nWelcome to my site.\n\n{{recent blog 3}}",
    )
    .unwrap();

    // Create navigation
    fs::write(root.join("nav.md"), "- [Blog](/blog)\n- [Notes](/notes)").unwrap();

    // Create layouts directory with default layout
    fs::create_dir_all(root.join("layouts")).unwrap();
    fs::write(
        root.join("layouts/default.html"),
        r#"<!DOCTYPE html>
<html lang="{{lang}}" dir="{{dir}}">
<head>
    <meta charset="utf-8">
    <title>{{title}}</title>
</head>
<body>
    <nav>{{nav}}</nav>
    <article>{{content}}</article>
</body>
</html>"#,
    )
    .unwrap();

    // Create static directory (files are copied without the static/ prefix)
    fs::create_dir_all(root.join("static/css")).unwrap();
    fs::write(root.join("static/css/style.css"), "body { margin: 0; }").unwrap();

    // Create tags directory for tag pages
    fs::create_dir_all(root.join("tags")).unwrap();

    dir
}

#[test]
fn test_full_site_build() {
    let source = create_test_site();
    let output = TempDir::new().unwrap();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: output.path().to_path_buf(),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");
    build_site(&site, &config).expect("Site build failed");

    // Verify output structure
    assert!(output.path().join("index.html").exists());
    assert!(output.path().join("blog/hello-world/index.html").exists());
    assert!(output.path().join("notes/rust/ownership/index.html").exists());
    assert!(output.path().join("feed.xml").exists());
    assert!(output.path().join("sitemap.xml").exists());
    // Static files are copied without the static/ prefix
    assert!(output.path().join("css/style.css").exists());
}

#[test]
fn test_blog_collection_discovery() {
    let source = create_test_site();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    let blog_posts: Vec<&Page> = site.pages_by_collection(Collection::Blog);
    assert!(!blog_posts.is_empty(), "Should have blog posts");

    let hello_post = blog_posts.iter().find(|p| p.url.contains("hello-world"));
    assert!(hello_post.is_some(), "Should find hello-world post");

    let post = hello_post.unwrap();
    assert_eq!(post.title, "Hello World");
    assert!(post.date.is_some(), "Blog post should have a date");
}

#[test]
fn test_notes_collection_discovery() {
    let source = create_test_site();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    let notes: Vec<&Page> = site.pages_by_collection(Collection::Notes);
    assert!(!notes.is_empty(), "Should have notes");

    let ownership_note = notes.iter().find(|p| p.url.contains("ownership"));
    assert!(ownership_note.is_some(), "Should find ownership note");
}

#[test]
fn test_tag_extraction() {
    let source = create_test_site();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    let blog_posts: Vec<&Page> = site.pages_by_collection(Collection::Blog);
    let hello_post = blog_posts.iter().find(|p| p.url.contains("hello-world")).unwrap();

    assert!(!hello_post.tags.is_empty(), "Post should have tags");
    assert!(
        hello_post.tags.iter().any(|t| t.name == "rust"),
        "Post should have #rust tag"
    );
}

#[test]
fn test_internal_link_extraction() {
    let source = create_test_site();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    let notes: Vec<&Page> = site.pages_by_collection(Collection::Notes);
    let ownership_note = notes.iter().find(|p| p.url.contains("ownership")).unwrap();

    assert!(!ownership_note.links.is_empty(), "Note should have internal links");
}

#[test]
fn test_navigation_loading() {
    let source = create_test_site();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    assert!(!site.nav_html.is_empty(), "Should have navigation HTML");
    assert!(site.nav_html.contains("Blog"), "Nav should contain Blog link");
    assert!(site.nav_html.contains("Notes"), "Nav should contain Notes link");
}

#[test]
fn test_draft_exclusion() {
    let source = create_test_site();

    // Add a draft post
    fs::write(
        source.path().join("blog/_draft-post.md"),
        "# Draft Post\n\nThis is a draft.",
    )
    .unwrap();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    let draft = site.pages.iter().find(|p| p.url.contains("draft-post"));
    assert!(draft.is_none(), "Draft should be excluded when include_drafts is false");
}

#[test]
fn test_draft_inclusion() {
    let source = create_test_site();

    // Add a draft post
    fs::write(
        source.path().join("blog/_draft-post.md"),
        "# Draft Post\n\nThis is a draft.",
    )
    .unwrap();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: true,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    let draft = site.pages.iter().find(|p| p.url.contains("draft-post"));
    assert!(draft.is_some(), "Draft should be included when include_drafts is true");
}

#[test]
fn test_static_file_copying() {
    let source = create_test_site();
    let output = TempDir::new().unwrap();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: output.path().to_path_buf(),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");
    build_site(&site, &config).expect("Site build failed");

    // Static files are copied without the static/ prefix
    assert!(output.path().join("css/style.css").exists());
    let css_content = fs::read_to_string(output.path().join("css/style.css")).unwrap();
    assert!(css_content.contains("margin: 0"));
}

#[test]
fn test_rss_feed_generation() {
    let source = create_test_site();
    let output = TempDir::new().unwrap();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: output.path().to_path_buf(),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");
    build_site(&site, &config).expect("Site build failed");

    let rss = fs::read_to_string(output.path().join("feed.xml")).unwrap();
    assert!(rss.contains("<rss"));
    assert!(rss.contains("<channel>"));
    assert!(rss.contains("Hello World"));
}

#[test]
fn test_sitemap_generation() {
    let source = create_test_site();
    let output = TempDir::new().unwrap();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: output.path().to_path_buf(),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");
    build_site(&site, &config).expect("Site build failed");

    let sitemap = fs::read_to_string(output.path().join("sitemap.xml")).unwrap();
    assert!(sitemap.contains("<urlset"));
    assert!(sitemap.contains("<url>"));
    assert!(sitemap.contains("<loc>"));
}

#[test]
fn test_html_output_structure() {
    let source = create_test_site();
    let output = TempDir::new().unwrap();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: output.path().to_path_buf(),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");
    build_site(&site, &config).expect("Site build failed");

    let index_html = fs::read_to_string(output.path().join("index.html")).unwrap();
    assert!(index_html.contains("<!DOCTYPE html>"));
    assert!(index_html.contains("<html"));
    assert!(index_html.contains("<head>"));
    assert!(index_html.contains("<body>"));
    assert!(index_html.contains("<nav>"));
    assert!(index_html.contains("<article>"));
}

#[test]
fn test_site_methods() {
    let site = Site::new();
    assert!(site.pages.is_empty());
    assert!(site.snippets.is_empty());

    let source = create_test_site();
    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    // Test pages_by_collection
    let blog_pages = site.pages_by_collection(Collection::Blog);
    assert!(!blog_pages.is_empty());

    // Test recent_pages
    let recent = site.recent_pages(Collection::Blog, 1);
    assert_eq!(recent.len(), 1);

    // Test find_page_by_url
    let root = site.find_page_by_url("/");
    assert!(root.is_some());
}

#[test]
fn test_page_date_extraction_from_filename() {
    let source = create_test_site();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    let blog_posts: Vec<&Page> = site.pages_by_collection(Collection::Blog);
    let hello_post = blog_posts.iter().find(|p| p.url.contains("hello-world")).unwrap();

    assert!(hello_post.date.is_some());
    let date = hello_post.date.unwrap();
    assert_eq!(date.format("%Y-%m-%d").to_string(), "2024-01-15");
}

#[test]
fn test_url_generation() {
    let source = create_test_site();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: PathBuf::from("_test"),
        include_drafts: false,
        ..Default::default()
    };

    let site = discover_site(&config).expect("Site discovery failed");

    // Root index should be /
    let root = site.find_page_by_url("/");
    assert!(root.is_some());

    // Blog post URL should not have date prefix
    let blog_posts: Vec<&Page> = site.pages_by_collection(Collection::Blog);
    let hello_post = blog_posts.iter().find(|p| p.url.contains("hello-world")).unwrap();
    assert!(!hello_post.url.contains("2024-01-15"));
    assert!(hello_post.url.starts_with("/blog/"));
}

#[test]
fn test_clean_build() {
    let source = create_test_site();
    let output = TempDir::new().unwrap();

    // Create a stale file in output
    fs::create_dir_all(output.path()).unwrap();
    fs::write(output.path().join("stale.txt"), "stale content").unwrap();

    let config = Config {
        source_dir: source.path().to_path_buf(),
        output_dir: output.path().to_path_buf(),
        include_drafts: false,
        ..Default::default()
    };

    // Clean and rebuild
    if config.output_dir.exists() {
        fs::remove_dir_all(&config.output_dir).unwrap();
    }

    let site = discover_site(&config).expect("Site discovery failed");
    build_site(&site, &config).expect("Site build failed");

    // Stale file should be gone
    assert!(!output.path().join("stale.txt").exists());
    // But new content should exist
    assert!(output.path().join("index.html").exists());
}