#static-site-generator #site #static #blog #web #generator #website

polysite

Highly customizable, polymorphic static site generator library

1 unstable release

0.0.1 Jan 25, 2024

#74 in #blog

MPL-2.0 license

51KB
1K SLoC

polysite

Highly customizable, polymorphic static site generator library, polysite.

polysite is inspired by Hakyll written in Haskell.

Difference from other static site generator

I know some famous static site generator, like Zola. But Zola is not enough customizable for me.

polysite aims to support creating your original static site generator.

License

Mozilla Public License 2.0 is applied.


lib.rs:

Highly customizable, polymorphic static site generator library, polysite.

This crate is inspired by Hakyll written in Haskell.

Difference from other static site generator

I know Zola, static site generator written in Rust. But zola is not enough customizable for me. So I create this crate.

How to use

If you would like to simply build site written in Markdown, use compiler::markdown::MarkdownCompiler. The example is in examples/simple_markdown.rs.

How to create compiler

If you would like to create new compiler, implement Compiler trait for your compiler type. Compiler::compile method is used for compile source file.

compile! macro is provided for ease of creating boxed Future.

If you would like to pipe some compilers, use pipe! macro.

If you would like to create small compiler using closure, use compiler::utils::GenericCompiler.

Metadata

polysite uses metadata to save compile result and metadata can be used in other compilation.

There are some default metadata:

  • _rule: Compiling rule name
  • _version: Compiling file version
  • _source: source file path
  • _target: target file path
  • _path: absolute URL path
  • _body: Content body. Some compilers save the result to this metadata.

You can use these default metadata to create new compiler.

Example

Practical example is here. Other examples are in repository.

use polysite::{
    compiler::{
        file::CopyCompiler, markdown::MarkdownCompiler, metadata::SetMetadata,
        template::TemplateEngine,
    },
    *,
};

#[tokio::main]
async fn main() {
    simple_logger::SimpleLogger::new().env().init().unwrap();
    let template_engine = TemplateEngine::new("templates/**").unwrap().get();
    Builder::new(Config::default())
        .add_step(
            [Rule::new("metadata").set_create(["metadata"]).set_compiler(
                SetMetadata::new()
                    .global("site_title", "Hello, polysite!")
                    .unwrap()
                    .global("site_url", "https://example.com")
                    .unwrap()
                    .get(),
            )],
        )
        .add_step([Rule::new("posts")
            .set_globs(["posts/**/*.md"])
            .set_compiler(
                MarkdownCompiler::new(template_engine.clone(), "practical.html", None)
                    .wait_snapshot("posts", 1)
                    .get(),
            )])
        .add_step([
            Rule::new("markdown").set_globs(["**/*.md"]).set_compiler(
                MarkdownCompiler::new(template_engine.clone(), "practical.html", None).get(),
            ),
            Rule::new("others")
                .set_globs(["**/*"])
                .set_compiler(CopyCompiler::new().get()),
        ])
        .build()
        .await
        .unwrap();
}

Dependencies

~12–23MB
~330K SLoC