Skip to main content

Crate webfluent

Crate webfluent 

Source
Expand description

§WebFluent

The Web-First Language — a programming language that compiles to HTML, CSS, JavaScript, and PDF.

WebFluent provides 50+ built-in UI components, signal-based reactivity, client-side routing, internationalization (i18n), animations, static site generation (SSG), and PDF document output — all with zero runtime dependencies.

§Quick Start

Use WebFluent as a template engine to render .wf templates with JSON data:

use webfluent::Template;
use serde_json::json;

let tpl = Template::from_str(r#"
    Page Home (path: "/", title: "Hello") {
        Container {
            Heading("Hello, {name}!", h1)
            Text("Welcome to WebFluent.")
        }
    }
"#).unwrap();

let html = tpl.render_html(&json!({"name": "World"})).unwrap();
// Returns a full HTML document with embedded CSS

let fragment = tpl.render_html_fragment(&json!({"name": "World"})).unwrap();
// Returns just the HTML fragment (no <html> wrapper)

§Rendering to PDF

use webfluent::Template;
use serde_json::json;

let tpl = Template::from_file("templates/invoice.wf").unwrap();
let pdf_bytes = tpl.render_pdf(&json!({
    "number": "INV-001",
    "customer": { "name": "Acme Corp" },
    "items": [{ "name": "Widget", "price": 9.99 }],
    "paid": true
})).unwrap();

std::fs::write("invoice.pdf", pdf_bytes).unwrap();

§Theming

Override design tokens or switch themes:

use webfluent::Template;
use serde_json::json;

let html = Template::from_str("Page P (path: \"/\") { Container { Text(\"Hello\") } }")
    .unwrap()
    .with_theme("dark")
    .with_tokens(&[("color-primary", "#8B5CF6")])
    .render_html(&json!({}))
    .unwrap();

§Architecture

The compilation pipeline:

  1. Lexer (lexer) — tokenizes .wf source into a token stream
  2. Parser (parser) — builds an AST from tokens
  3. Linter (linter) — runs accessibility and PDF validation checks
  4. Codegen (codegen) — generates HTML, CSS, JS, SSG pages, or PDF output
  5. Themes (themes) — provides design tokens and component CSS
  6. Runtime (runtime) — embeds the JavaScript runtime for reactivity and routing

§Crate Features

This crate exposes the full compiler pipeline. For most use cases, the Template API is the simplest entry point. For full control, use the lexer, parser, and codegen modules directly.

Re-exports§

pub use template::Template;
pub use error::WebFluentError;
pub use error::Result;

Modules§

codegen
Code generation — compiles the AST to various output formats.
config
Project configuration — loads and manages webfluent.app.json.
error
Error types and diagnostics.
lexer
Lexical analysis — tokenizes .wf source code.
linter
Linting — compile-time checks for accessibility and PDF validation.
parser
Parsing — builds an abstract syntax tree from tokens.
runtime
JavaScript runtime — embedded runtime for reactivity, routing, and DOM helpers.
template
Template engine — render .wf templates with JSON data.
themes
Design system — theme tokens and component CSS.