aphid 0.2.2

A static site generator for blogs and wikis, with wiki-links across both.
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
use std::fs;
use std::path::{Path, PathBuf};

use chrono::Local;

use crate::Error;
use crate::agent::{self, AgentTool};
use crate::config::Config;
use crate::content::slug::Slug;
use crate::output::OutputWriter;
use crate::render::{BuiltSite, Theme};

struct Scaffold {
    dir: PathBuf,
    title: String,
}

pub fn new(name: &str, agent: Option<AgentTool>) -> Result<(), Error> {
    let dir = PathBuf::from(name);
    if dir.exists() {
        return Err(Error::Scaffold {
            message: format!("directory '{}' already exists", dir.display()),
        });
    }
    let title = title_from_name(dir.file_name().and_then(|n| n.to_str()).unwrap_or(name));
    Scaffold::create(dir, title, agent)?;

    tracing::info!(name, "created new site");
    println!("\n  To get started:\n");
    println!("    cd {name}");
    println!("    aphid serve\n");
    print_agent_footer(agent);
    Ok(())
}

pub fn init(path: &Path, agent: Option<AgentTool>) -> Result<(), Error> {
    if path.join("aphid.toml").exists() {
        return Err(Error::Scaffold {
            message: format!(
                "directory '{}' already contains an aphid.toml",
                path.display()
            ),
        });
    }
    let title = path
        .file_name()
        .and_then(|n| n.to_str())
        .map(title_from_name)
        .unwrap_or_else(|| "My Site".to_string());
    Scaffold::create(path.to_path_buf(), title, agent)?;

    tracing::info!(path = %path.display(), "initialized site");
    if path == Path::new(".") {
        println!("\n  To get started:\n");
        println!("    aphid serve\n");
    } else {
        println!("\n  To get started:\n");
        println!("    cd {}", path.display());
        println!("    aphid serve\n");
    }
    print_agent_footer(agent);
    Ok(())
}

fn print_agent_footer(agent: Option<AgentTool>) {
    if let Some(tool) = agent {
        println!("  Agent instructions written for {}.\n", tool.label());
    }
}

pub fn new_blog_post(config_path: &Path, title: &str) -> Result<(), Error> {
    let config = Config::from_path(config_path)?;
    let path = write_blog_post(&config.source_dir, title)?;
    println!("  Created {}", path.display());
    Ok(())
}

pub fn new_wiki_page(config_path: &Path, title: &str) -> Result<(), Error> {
    let config = Config::from_path(config_path)?;
    let path = write_wiki_page(&config.source_dir, title)?;
    println!("  Created {}", path.display());
    Ok(())
}

pub fn new_page(config_path: &Path, title: &str) -> Result<(), Error> {
    let config = Config::from_path(config_path)?;
    let path = write_page(&config.source_dir, title)?;
    println!("  Created {}", path.display());
    Ok(())
}

fn write_blog_post(content_dir: &Path, title: &str) -> Result<PathBuf, Error> {
    let date = Local::now().format("%Y-%m-%d");
    let slug = Slug::from(title);
    let filename = format!("{date}_{slug}.md");
    let content = format!(
        "\
---
title: {title}
slug: {slug}
author: Your Name
created: {date}
description: \"\"
tags: []
---

",
    );
    let path = content_dir.join("blog").join(&filename);
    if path.exists() {
        return Err(Error::Scaffold {
            message: format!("file '{}' already exists", path.display()),
        });
    }
    write_file(&path, &content)?;
    Ok(path)
}

fn write_wiki_page(content_dir: &Path, title: &str) -> Result<PathBuf, Error> {
    let slug = Slug::from(title);
    let filename = format!("{slug}.md");
    let content = format!(
        "\
---
title: {title}
---

",
    );
    let path = content_dir.join("wiki").join(&filename);
    if path.exists() {
        return Err(Error::Scaffold {
            message: format!("file '{}' already exists", path.display()),
        });
    }
    write_file(&path, &content)?;
    Ok(path)
}

fn write_page(content_dir: &Path, title: &str) -> Result<PathBuf, Error> {
    let slug = Slug::from(title);
    let filename = format!("{slug}.md");
    let content = format!(
        "\
---
title: {title}
---

",
    );
    let path = content_dir.join("pages").join(&filename);
    if path.exists() {
        return Err(Error::Scaffold {
            message: format!("file '{}' already exists", path.display()),
        });
    }
    write_file(&path, &content)?;
    Ok(path)
}

fn title_from_name(name: &str) -> String {
    name.split(['-', '_'])
        .filter(|s| !s.is_empty())
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                None => String::new(),
                Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

fn write_file(path: &Path, content: &str) -> Result<(), Error> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(path, content)?;
    Ok(())
}

impl Scaffold {
    /// Scaffold a complete site at `dir`: write all initial files, build once,
    /// and — if requested — write agent instruction files.
    fn create(dir: PathBuf, title: String, agent: Option<AgentTool>) -> Result<Self, Error> {
        fs::create_dir_all(&dir)?;
        let scaffold = Self { dir, title };
        scaffold.write_all()?;
        scaffold.build_site()?;
        if let Some(tool) = agent {
            agent::init(tool, &scaffold.dir)?;
            tracing::info!(tool = ?tool, "wrote agent instructions");
        }
        Ok(scaffold)
    }

    fn write_all(&self) -> Result<(), Error> {
        self.write_config()?;
        self.write_gitignore()?;
        self.write_theme()?;
        self.write_initial_blog_post()?;
        self.write_initial_wiki_page()?;
        self.write_initial_page()?;
        self.write_home()?;
        self.create_static_dir()?;
        Ok(())
    }

    fn write_config(&self) -> Result<(), Error> {
        let content = format!(
            "title = \"{}\"\nbase_url = \"https://example.com\"\ntheme_dir = \"theme\"\n",
            self.title
        );
        write_file(&self.dir.join("aphid.toml"), &content)
    }

    fn write_theme(&self) -> Result<(), Error> {
        Theme::write_default_to_dir(&self.dir.join("theme"))
    }

    fn write_gitignore(&self) -> Result<(), Error> {
        write_file(&self.dir.join(".gitignore"), "/dist\n")
    }

    fn write_initial_blog_post(&self) -> Result<(), Error> {
        let content_dir = self.dir.join("content");
        write_blog_post(&content_dir, "Hello World")?;
        Ok(())
    }

    fn write_initial_wiki_page(&self) -> Result<(), Error> {
        let content_dir = self.dir.join("content");
        let slug = Slug::from("Getting Started");
        let content = "\
---
title: Getting Started
---

# Writing content

Blog posts live in `content/blog/`, wiki pages in `content/wiki/`, and
standalone pages in `content/pages/`.

# Wiki links

Link between any pages with `[[slug]]` syntax. For example, this page can be
linked from anywhere as `[[getting-started]]`.

# Building

Run `aphid serve` to start the development server, or `aphid build` to render
the site into the `dist/` directory.
";
        write_file(
            &content_dir.join("wiki").join(format!("{slug}.md")),
            content,
        )
    }

    fn write_initial_page(&self) -> Result<(), Error> {
        let content = format!(
            "\
---
title: About
order: 1
---

This is the about page for {title}. Edit `content/pages/about.md` to update it.
",
            title = self.title
        );
        write_file(&self.dir.join("content/pages/about.md"), &content)
    }

    fn write_home(&self) -> Result<(), Error> {
        let content = format!(
            "\
Welcome to **{title}**. This is the home page content.

Edit `content/home.md` to change this text, or delete the file to use the
default home page layout.
",
            title = self.title
        );
        write_file(&self.dir.join("content/home.md"), &content)
    }

    fn create_static_dir(&self) -> Result<(), Error> {
        fs::create_dir_all(self.dir.join("static"))?;
        Ok(())
    }

    fn build_site(&self) -> Result<(), Error> {
        let dir = self.dir.canonicalize()?;
        let config_path = dir.join("aphid.toml");
        let output_dir = dir.join("dist");
        let config = Config::from_path(&config_path)?;
        let theme = Theme::load(&config)?;
        let built = BuiltSite::build(&config, &theme)?;
        if !built.diagnostics.is_empty() {
            return Err(Error::BrokenWikiLinks(
                built.diagnostics.broken_wiki_links.clone(),
            ));
        }
        let writer = OutputWriter::new(&output_dir)?;
        writer.write(&built, &theme, &config.static_dir)?;
        Ok(())
    }
}

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

    #[test]
    fn title_from_hyphenated_name() {
        assert_eq!(title_from_name("my-cool-blog"), "My Cool Blog");
    }

    #[test]
    fn title_from_underscored_name() {
        assert_eq!(title_from_name("my_site"), "My Site");
    }

    #[test]
    fn title_from_plain_name() {
        assert_eq!(title_from_name("mysite"), "Mysite");
    }

    #[test]
    fn title_from_mixed_separators() {
        assert_eq!(title_from_name("my-cool_blog"), "My Cool Blog");
    }

    #[test]
    fn new_creates_complete_site() {
        let tmp = tempfile::tempdir().unwrap();
        let site_dir = tmp.path().join("test-site");

        new(site_dir.to_str().unwrap(), None).unwrap();

        assert!(site_dir.join("aphid.toml").exists());
        assert!(site_dir.join(".gitignore").exists());
        assert!(site_dir.join("content/pages/about.md").exists());
        assert!(site_dir.join("content/wiki/getting-started.md").exists());
        assert!(site_dir.join("content/home.md").exists());
        assert!(site_dir.join("static").is_dir());

        let blog_entries: Vec<_> = fs::read_dir(site_dir.join("content/blog"))
            .unwrap()
            .collect();
        assert_eq!(blog_entries.len(), 1);

        let config = fs::read_to_string(site_dir.join("aphid.toml")).unwrap();
        assert!(config.contains("title = \"Test Site\""));
    }

    #[test]
    fn new_fails_if_directory_exists() {
        let tmp = tempfile::tempdir().unwrap();
        let site_dir = tmp.path().join("existing");
        fs::create_dir(&site_dir).unwrap();

        let err = new(site_dir.to_str().unwrap(), None).unwrap_err();
        assert!(err.to_string().contains("already exists"));
    }

    #[test]
    fn new_with_agent_writes_both() {
        let tmp = tempfile::tempdir().unwrap();
        let site_dir = tmp.path().join("with-agent");

        new(site_dir.to_str().unwrap(), Some(AgentTool::Claude)).unwrap();

        assert!(site_dir.join("aphid.toml").exists());
        assert!(site_dir.join("CLAUDE.md").exists());
        assert!(
            site_dir
                .join(".claude/skills/aphid-content/SKILL.md")
                .exists()
        );
    }

    #[test]
    fn init_creates_in_existing_directory() {
        let tmp = tempfile::tempdir().unwrap();

        init(tmp.path(), None).unwrap();

        assert!(tmp.path().join("aphid.toml").exists());
        assert!(tmp.path().join(".gitignore").exists());
        assert!(tmp.path().join("content/blog").is_dir());
    }

    #[test]
    fn init_fails_if_config_exists() {
        let tmp = tempfile::tempdir().unwrap();
        fs::write(tmp.path().join("aphid.toml"), "").unwrap();

        let err = init(tmp.path(), None).unwrap_err();
        assert!(err.to_string().contains("already contains an aphid.toml"));
    }

    #[test]
    fn init_creates_directory_if_missing() {
        let tmp = tempfile::tempdir().unwrap();
        let nested = tmp.path().join("nested/site");

        init(&nested, None).unwrap();

        assert!(nested.join("aphid.toml").exists());
    }

    #[test]
    fn init_with_agent_writes_both() {
        let tmp = tempfile::tempdir().unwrap();

        init(tmp.path(), Some(AgentTool::Codex)).unwrap();

        assert!(tmp.path().join("aphid.toml").exists());
        assert!(tmp.path().join("AGENTS.md").exists());
        assert!(tmp.path().join(".agents/aphid-content.md").exists());
    }

    fn scaffold_site(tmp: &Path) -> PathBuf {
        let site_dir = tmp.join("site");
        new(site_dir.to_str().unwrap(), None).unwrap();
        site_dir
    }

    #[test]
    fn new_blog_post_creates_file() {
        let tmp = tempfile::tempdir().unwrap();
        let site_dir = scaffold_site(tmp.path());
        let config_path = site_dir.join("aphid.toml");

        new_blog_post(&config_path, "My New Post").unwrap();

        let date = Local::now().format("%Y-%m-%d");
        let expected = site_dir
            .join("content/blog")
            .join(format!("{date}_my-new-post.md"));
        assert!(expected.exists());

        let content = fs::read_to_string(&expected).unwrap();
        assert!(content.contains("title: My New Post"));
        assert!(content.contains("slug: my-new-post"));
    }

    #[test]
    fn new_blog_post_fails_if_file_exists() {
        let tmp = tempfile::tempdir().unwrap();
        let site_dir = scaffold_site(tmp.path());
        let config_path = site_dir.join("aphid.toml");

        new_blog_post(&config_path, "My New Post").unwrap();
        let err = new_blog_post(&config_path, "My New Post").unwrap_err();
        assert!(err.to_string().contains("already exists"));
    }

    #[test]
    fn new_wiki_page_creates_file() {
        let tmp = tempfile::tempdir().unwrap();
        let site_dir = scaffold_site(tmp.path());
        let config_path = site_dir.join("aphid.toml");

        new_wiki_page(&config_path, "My Topic").unwrap();

        let expected = site_dir.join("content/wiki/my-topic.md");
        assert!(expected.exists());

        let content = fs::read_to_string(&expected).unwrap();
        assert!(content.contains("title: My Topic"));
    }

    #[test]
    fn new_wiki_page_fails_if_file_exists() {
        let tmp = tempfile::tempdir().unwrap();
        let site_dir = scaffold_site(tmp.path());
        let config_path = site_dir.join("aphid.toml");

        new_wiki_page(&config_path, "My Topic").unwrap();
        let err = new_wiki_page(&config_path, "My Topic").unwrap_err();
        assert!(err.to_string().contains("already exists"));
    }

    #[test]
    fn new_page_creates_file() {
        let tmp = tempfile::tempdir().unwrap();
        let site_dir = scaffold_site(tmp.path());
        let config_path = site_dir.join("aphid.toml");

        new_page(&config_path, "Contact").unwrap();

        let expected = site_dir.join("content/pages/contact.md");
        assert!(expected.exists());

        let content = fs::read_to_string(&expected).unwrap();
        assert!(content.contains("title: Contact"));
    }

    #[test]
    fn new_page_fails_if_file_exists() {
        let tmp = tempfile::tempdir().unwrap();
        let site_dir = scaffold_site(tmp.path());
        let config_path = site_dir.join("aphid.toml");

        new_page(&config_path, "Contact").unwrap();
        let err = new_page(&config_path, "Contact").unwrap_err();
        assert!(err.to_string().contains("already exists"));
    }
}