| Crates.io | pagebake |
| lib.rs | pagebake |
| version | 0.2.0 |
| created_at | 2025-02-09 22:25:48.572616+00 |
| updated_at | 2025-03-06 22:35:55.055552+00 |
| description | Simple Static Site Generator library |
| homepage | |
| repository | https://github.com/boralg/pagebake |
| max_upload_size | |
| id | 1549354 |
| size | 32,562 |
pagebake is a simple, modular static site generator library. Inspired by axum, pagebake provides an intuitive API for defining routes, handling redirects, and rendering static HTML pages.
Routing and Rendering: Define custom routes that map to page-rendering functions. Use simple closures to generate HTML content.
Redirect Support: Easily configure redirects that work with static hosting services, or out-of-the box via plain HTML.
Fallback Handlers: Specify fallback pages for unmatched routes.
Router Composition: Merge and nest routers to build modular and scalable site architectures.
Flexible Output Options: Render your site directly to disk or generate an in-memory map of files.
Add pagebake to your Cargo.toml:
cargo add pagebake
Clone the repository:
git clone https://github.com/boralg/pagebake.git
cd pagebake
If you use Nix, simply activate the development shell:
nix develop
Create a new router and register your routes with rendering functions or redirects:
use pagebake::render::RenderConfig;
use pagebake::{get, redirect, Router};
fn main() {
let router = Router::new()
.route("/", get(|| "<h1>Home</h1>".to_owned()))
.route("/about", get(|| "<h1>About</h1>".to_owned()))
.route("/old-home", redirect("/"))
.fallback(|| "<h1>Not Found</h1>".to_owned());
let config = RenderConfig::default();
let _ = router.render(std::path::Path::new("./public"), config);
}
Organize your site by nesting routers for different sections:
use pagebake::{get, redirect, Router};
fn main() {
// Create a sub-router for blog-related pages.
let blog_router = Router::new()
.route("/", get(|| "<h1>Welcome to the Blog</h1>".to_owned()))
.route("/post", get(|| "<h1>Blog Post</h1>".to_owned()))
.route("/old", redirect("/"))
.fallback(|| "<h1>Blog 404: Page Not Found</h1>".to_owned());
// Nest the blog router under the "/blog" prefix.
// This prepends "/blog" to all routes from the blog_router.
let router = Router::new()
.route("/", get(|| "<h1>Home</h1>".to_owned()))
.nest("/blog", blog_router);
// At this point, the following routes are available:
// - "/" renders "<h1>Home</h1>"
// - "/blog/" renders "<h1>Welcome to the Blog</h1>"
// - "/blog/post" renders "<h1>Blog Post</h1>"
// - "/blog/old" performs a redirect to "/blog/"
// - The fallback route for unmatched blog paths would typically become a page at path "/blog/404"
}
pagebake supports custom redirect page rendering. By default, a simple HTML page is generated that uses meta tags and JavaScript to perform the redirect. Custom renderers can also be configured.
For redirect list generation (e.g. for Cloudflare Pages or Static Web Server), use the provided configurations in the redirects module.
The same applies to route lists, which can be used to generate sitemaps.
use pagebake::redirects::RedirectList;
use pagebake::render::RenderConfig;
use pagebake::routes::RouteList;
use pagebake::{get, redirect, Router};
fn main() {
let router = Router::new()
.route("/", get(|| "<h1>Home</h1>".to_owned()))
.route("/old-page", redirect("/"));
let redirect: Box<dyn Fn(&str) -> String> = Box::new(|target| {
format!(
r#"<!DOCTYPE HTML>
<script>
window.location.href = "{0}";
</script>"#,
target
)
});
let config = RenderConfig {
redirect_page_renderer: Some(redirect),
redirect_lists: vec![RedirectList::for_cloudflare_pages()],
route_lists: vec![RouteList::sitemap("http://localhost:8080".to_string())],
..Default::default()
};
// This will generate:
// - An HTML page at "/old-page.html" using the custom redirect renderer.
// - A "_redirects" file with the list of all redirects.
// - A sitemap.xml containing all non-redirect routes.
let _ = router.render(std::path::Path::new("./public"), config);
}
Contributions to pagebake are welcome! If you have suggestions, encounter issues, or want to contribute new features, please open an issue or submit a pull request.