2个版本

0.1.1 2020年2月11日
0.1.0 2020年2月11日

#48 in #static-site

GPL-3.0-or-later

43KB
881

libssg

静态站点生成库

构建自己的可执行静态生成器,其中包含您的构建逻辑,而不是使用配置文件和命令行参数。受 Hakyll 启发。

use libssg::*;
/*
 * $ tree
 * .
 * ├── Cargo.toml etc
 * ├── src
 * │   └── main.rs
 * ├── css
 * │   └── *.css
 * ├── images
 * │   └── *.png
 * ├── index.md
 * ├── posts
 * │   └── *.md
 * ├── _site
 * │   ├── css
 * │   │   └── *.css
 * │   ├── images
 * │   │   └── *.png
 * │   ├── index.html
 * │   ├── posts
 * │   │   └── *.html
 * │   └── rss.xml
 * └── templates
 *     ├── default.hbs
 *     └── post.hbs
*/


fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut state = State::new()?;
    state
        .then(match_pattern(
            "^posts/*",
            Route::SetExtension("html"),
               Renderer::Pipeline(vec![
                   Renderer::LoadAndApplyTemplate("templates/post.hbs"),
                   Renderer::LoadAndApplyTemplate("templates/default.hbs"),
               ]),
            compiler_seq(
                pandoc(),
                Box::new(|state, path| {
                    let path = path
                        .strip_prefix(&state.output_dir().parent().unwrap())
                        .unwrap_or(&path)
                        .to_path_buf();
                    if state.verbosity() > 3 {
                        println!("adding {} to RSS snapshot", path.display());
                    }
                    let uuid = uuid_from_path(&path);
                    state.add_to_snapshot("main-rss-feed".into(), uuid);
                    Ok(Default::default())
                }),
            ),
        ))
        .then(match_pattern(
            "index.md",
            Route::SetExtension("html"),
            Renderer::LoadAndApplyTemplate("templates/default.hbs"),
            pandoc(),
        ))
        .then(copy("^images/*", Route::Id))
        .then(copy("^css/*", Route::Id))
        .then(build_rss_feed(
            "rss.xml".into(),
            rss_feed(
                "main-rss-feed".into(),
                RssItem {
                    title: "example page".into(),
                    description: "example using libssg".into(),
                    link: "http://example.local".into(),
                    last_build_date: String::new(),
                    pub_date: "Thu, 01 Jan 1970 00:00:00 +0000".to_string(),
                    ttl: 1800,
                },
            ),
        ))
        .finish()?;
    Ok(())
}

cargo run 并将输出保存到 ./_site/

设置 $FORCE$VERBOSITY (0..5) 以更改行为。

依赖项

~5–15MB
~182K SLoC