pug

A Pug template engine implementation in MoonBit.

pug
jade
moon add Milky2018/pug@0.1.1
Download zip
Author
Version
0.1.1
License
Apache-2.0
Last updated
6 months ago
Downloads
19

Dependencies

README

#pug

A Pug template engine implementation in MoonBit.

#Features

  • Basic HTML tag generation
  • ID and class shorthand syntax (div#id.class)
  • Attributes (a(href="url"))
  • Nested elements via indentation
  • Doctype declaration
  • HTML comments
  • Pretty printing
  • Interpolation (#{variable})
  • Compile API for reusable templates with locals

#Usage

///|
test "usage example" {
let pug =
#|doctype html
#|html
#| head
#| title Hello Pug
#| body
#| h1#greeting.title Hello, World!
#| p This is rendered from Pug.

// Render to compact HTML
let html = @pug.render(pug)
inspect(
html,
content="<!DOCTYPE html><html><head><title>Hello Pug</title></head><body><h1 id=\"greeting\" class=\"title\">Hello, World!</h1><p>This is rendered from Pug.</p></body></html>",
)

// Render to pretty-printed HTML
let _pretty_html = @pug.render_pretty(pug)

}

#Pug Syntax

#Tags

div p Hello World span.highlight Text

#IDs and Classes

div#main div.container div#app.container.active

#Implicit div

#main .container

#Attributes

a(href="https://example.com") Click me input(type="text", name="username", placeholder="Enter name") img(src="photo.jpg", alt="A photo")

#Nesting

html head title My Page body h1 Welcome p This is content.

#Comments

// This is a rendered comment //- This is not rendered

#Doctype

doctype html

#Interpolation

p Hello #{name}! p Welcome to #{place}

#Compile API

For reusable templates, use the compile API:

///|
test "compile api" {
// Compile template once
let template = @pug.compile("p Hello #{name}!")

// Render with different locals
let locals1 = @pug.Locals::new()
locals1.set("name", "Alice")
inspect(template.render(locals1), content="<p>Hello Alice!</p>")
let locals2 = @pug.Locals::new()
locals2.set("name", "Bob")
inspect(template.render(locals2), content="<p>Hello Bob!</p>")
}

Or render directly with locals:

///|
test "render with locals" {
let locals = @pug.Locals::new()
locals.set("name", "World")
let html = @pug.render_with_locals("p Hello #{name}!", locals)
inspect(html, content="<p>Hello World!</p>")
}

#License

Apache-2.0

#
Attribute

pub struct Attribute {
name : String
value : String?
unescaped : Bool
}

Represents an attribute in a Pug element
impl Eq for Attribute
impl Show for Attribute
impl ToJson for Attribute

#
CompiledTemplate

pub struct CompiledTemplate {
doc : Document
}

Compiled template - holds parsed document for repeated rendering

#
CompiledTemplate::render

fn CompiledTemplate::render(self : CompiledTemplate, locals : Locals) -> String

Render a compiled template with locals

#
CompiledTemplate::render_with_options

fn CompiledTemplate::render_with_options(self : CompiledTemplate, locals : Locals, options : RenderOptions) -> String

Render a compiled template with locals and options

#
Document

pub struct Document {
nodes : Array[Node]
}

A Pug document is a list of nodes
impl ToJson for Document

#
Document::iter

fn Document::iter(self : Document) -> Iter[Node]

#
Document::new

fn Document::new() -> Document

#
Document::push

fn Document::push(self : Document, node : Node) -> Unit

#
Document::render

fn Document::render(self : Document, options~ : RenderOptions) -> String

Render a document to HTML string (without locals)

#
Document::render_with_locals

fn Document::render_with_locals(self : Document, locals : Locals, options~ : RenderOptions) -> String

Render a document to HTML string with locals

#
Document::render_with_registry

fn Document::render_with_registry(self : Document, locals : Locals, registry : TemplateRegistry, options~ : RenderOptions) -> String

Render a document with registry support for extends and includes

#
Document::to_html

fn Document::to_html(self : Document) -> String

Render a document to HTML string with default options

#
Document::to_pretty_html

fn Document::to_pretty_html(self : Document) -> String

Render a document to pretty-printed HTML string

#
Lexer

type Lexer

Lexer state - uses Iter[Char] for character iteration

#
Lexer::new

fn Lexer::new(input : String) -> Lexer

#
Lexer::tokenize

fn Lexer::tokenize(self : Lexer) -> Array[Token]

Tokenize entire input

#
Lexer::tokenize_line

fn Lexer::tokenize_line(self : Lexer) -> Array[Token]

Tokenize a single line, returning tokens

#
Locals

pub type Locals Map[String, String]

Locals map for template interpolation

#
Locals::get

fn Locals::get(self : Locals, key : String) -> String?

#
Locals::inner

#deprecated("Use `struct T(A)` to declare a newtype and use `.0` access the underlying type instead.")
fn Locals::inner(self : Locals) -> Map[String, String]
Convert newtype to its underlying type, automatically derived.

#
Locals::new

fn Locals::new() -> Locals

#
Locals::set

fn Locals::set(self : Locals, key : String, value : String) -> Unit

#
Locals::set_array

fn Locals::set_array(self : Locals, key : String, values : Array[String]) -> Unit

Set an array value (stub for future implementation)

#
Locals::set_nested_array

fn Locals::set_nested_array(self : Locals, key : String, rows : Array[Array[String]]) -> Unit

Set a nested array value (stub for future implementation)

#
Locals::set_object

fn Locals::set_object(self : Locals, key : String, obj : Map[String, String]) -> Unit

Set an object value (stub for future implementation)

#
Node

pub enum Node {
Element(String, String, Array[String], Array[Attribute], Array[Node], Bool)
Text(String)
Interpolation(String)
UnescapedInterpolation(String)
Comment(String, Bool)
Doctype(String)
Conditional(String, Array[Node], Array[Node], Bool)
Each(String, String, String, Array[Node], Array[Node])
Case(String, Array[(String, Array[Node])], Array[Node])
When(String, Array[Node])
Default(Array[Node])
MixinDef(String, Array[(String, String)], Array[Node])
MixinCall(String, Array[String], Array[Node], Array[(String, String)])
Block
VarAssign(String, String)
While(String, Array[Node])
NamedBlock(String, Array[Node], String)
Include(String)
IncludeFiltered(String, String)
Extends(String)
Filter(String, String)
}

Represents a node in the Pug AST
impl Eq for Node
impl Show for Node
impl ToJson for Node

#
Parser

type Parser

Parser state

#
Parser::new

fn Parser::new(tokens : Array[Token]) -> Parser

#
Parser::parse

fn Parser::parse(self : Parser) -> Document

Parse tokens into a Document

#
RenderOptions

pub struct RenderOptions {
pretty : Bool
indent_str : String
}

Render options

#
RenderOptions::default

fn RenderOptions::default() -> RenderOptions

#
RenderOptions::pretty

#
TemplateRegistry

pub struct TemplateRegistry {
templates : Map[String, String]
}

Template registry for storing and resolving templates Used for extends and include functionality

#
TemplateRegistry::contains

fn TemplateRegistry::contains(self : TemplateRegistry, path : String) -> Bool

Check if a template exists

#
TemplateRegistry::ensure_loaded

fn TemplateRegistry::ensure_loaded(self : TemplateRegistry, path : String) -> Unit raise
IOError

Load a template from file if not already loaded

#
TemplateRegistry::get

fn TemplateRegistry::get(self : TemplateRegistry, path : String) -> String?

Get a template by path

#
TemplateRegistry::load_file

fn TemplateRegistry::load_file(self : TemplateRegistry, path : String) -> Unit raise
IOError

Load a template from file and register it

#
TemplateRegistry::new

#
TemplateRegistry::register

fn TemplateRegistry::register(self : TemplateRegistry, path : String, source : String) -> Unit

Register a template with a path

#
Token

pub enum Token {
Indent(Int)
Tag(String)
Id(String)
Class(String)
AttrName(String)
AttrValue(String)
Text(String)
Interpolation(String)
UnescapedInterpolation(String)
TagInterpolation(String)
LParen
RParen
Equals
UnescapedEquals
Newline
Comment(String)
UnbufferedComment(String)
Doctype(String)
BlockExpand
PipedText(String)
BlockTextMarker
BlockText(String)
If(String)
ElseIf(String)
Else
Unless(String)
Each(String, String, String)
Case(String)
When(String)
Default
Block
MixinDef(String, Array[(String, String)])
MixinCall(String, Array[String], Array[(String, String)])
BufferedOutput(String)
UnescapedBufferedOutput(String)
VarAssign(String, String)
While(String)
NamedBlock(String)
AppendBlock(String)
PrependBlock(String)
Include(String)
IncludeFiltered(String, String)
Extends(String)
Filter(String, String)
SelfClose
EOF
}

Token types for Pug lexer
impl Eq for Token
impl Show for Token

#
apply_filter

fn apply_filter(filter : String, content : String) -> String

Apply a filter to content Supports: plain (as-is), markdown (simple conversion)

#
compile

fn compile(input : String) -> CompiledTemplate

Compile a Pug template string into a reusable template

#
eval_expression

fn eval_expression(expr : String, locals : Locals) -> String

Evaluate a simple expression with locals Supports: variable lookup, string concat (+), string literals, ternary (? :) Example: baseUrl + '/path' with {baseUrl: "http://example.com"} Example: active ? 'on' : 'off' with {active: "true"}

#
eval_js_expression

fn eval_js_expression(expr : String, locals : Locals) -> String

Evaluate an expression - stub implementation for non-JS backends Falls back to simple variable lookup from eval_expression in spec.mbt

#
exec_js_statement

fn exec_js_statement(stmt : String, locals : Locals) -> Unit

Execute a statement - stub implementation for non-JS backends Handles simple "var name = value" patterns only

#
get_doctype

fn get_doctype(name : String) -> String

Get doctype string for various doctype names Supports: html, xml, transitional, strict, frameset, 1.1, basic, mobile

#
locals_to_json

fn locals_to_json(locals : Locals) -> String

Convert Locals to JSON string (stub - same implementation) Converts "true"/"false" to boolean values for proper JS semantics

#
parse

fn parse(input : String) -> Document

Convenience function to parse a Pug string

#
parse_with_registry

fn parse_with_registry(input : String, registry : TemplateRegistry) -> Document

Parse Pug and resolve all includes using registry Returns a Document with Include nodes replaced by actual content

#
render

fn render(input : String) -> String

Convenience function: parse Pug and render to HTML

#
render_file

fn render_file(path : String) -> String raise
IOError

Render a Pug file - automatically loads includes/extends This is the simple API similar to pug.renderFile()

#
render_file_with_locals

fn render_file_with_locals(path : String, locals : Locals) -> String raise
IOError

Render a Pug file with locals

#
render_pretty

fn render_pretty(input : String) -> String

Convenience function: parse Pug and render to pretty HTML

#
render_with_locals

fn render_with_locals(input : String, locals : Locals) -> String

Convenience function: parse Pug and render to HTML with locals

#
render_with_registry

fn render_with_registry(input : String, locals : Locals, registry : TemplateRegistry) -> String

Convenience function: render with registry

#
tokenize

fn tokenize(input : String) -> Array[Token]

Convenience function to tokenize a string

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io