2 releases

0.1.1 Feb 11, 2020
0.1.0 Feb 11, 2020

#56 in Template engine

31 downloads per month

GPL-3.0-or-later

42KB
881 lines

libssg

static site generation library

Build your own executable static generator that includes your building logic instead of using configuration files and command line arguments. Inspired by 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 and the output is saved at ./_site/.

Set $FORCE, $VERBOSITY (0..5) to change behaviour.

Dependencies

~5MB
~115K SLoC