README

himeno/mopress/core does not have a README file

#
Frontmatter

type Frontmatter = Map[String, String]

A YAML frontmatter block, parsed as a map of string keys to string values.

#
Html

type Html = String

A rendered HTML string.

#
Rules

type Rules = Array[Rule]

A collection of build rules, as passed to mo_build/mopress.

#
Step

type Step[T, R] = async (Item[T]) -> Item[R]

A pipeline step transforming an Item[T] into an Item[R].

#
Vars

A set of named template variables, as carried by Item.vars.

#
Thingable

pub trait Thingable {
fn to_thing(self : Self) -> Thing
}

Types that can be converted into a Thing, the common output representation written to disk at the end of a build pipeline.
impl Thingable for String
impl Thingable for Bytes
impl Thingable for Json

#
Handler

pub(all) enum Handler {
Text(async (Item[String]) -> Item[Thing])
Binary(async (Item[Bytes]) -> Item[Thing])
Copy
} derive(
Debug
)

The processing handler applied to files matched by a Rule.

#
Handler::run

async fn Handler::run(self : Handler, options : Options, path : String, read_string? : (String) -> String raise, read_bytes? : (String) -> Bytes raise) -> Item[Thing]

Executes this handler against the file at path (relative to options.src), returning the resulting Item[Thing].

read_string and read_bytes allow overriding how the file's contents are read for Text and Binary handlers respectively (defaulting to ordinary filesystem reads), which is primarily useful for testing or for sourcing content from something other than the local filesystem.

#
Item

pub struct Item[T] {
data : T
vars : Map[String,
Value
]
target : String
// private fields
} derive(Eq,
Debug
)

The core unit of data flowing through a mopress build pipeline.

An Item[T] carries a piece of data of type T (e.g. raw text, a parsed Markdown AST, or rendered HTML), a set of template variables (vars) that will be available when the item is eventually rendered through a template, and a target path describing where the item's output should be written.

Steps (functions of the shape (Item[T]) -> Item[R], optionally raising or async) transform an Item from one representation to another while carrying its variables and target path forward, so that a page's content can flow through a chain of transformations — parsing, preprocessing, transforming, rendering, and templating — as a pipeline of Item values.

#
Item::add_vars

fn[T] Item::add_vars(self : Item[T], vars : Map[String,
Value
]) -> Item[T]

Returns a copy of self with the given variables merged into its existing vars, overwriting any existing entries with the same key.

#
Item::base

fn[T] Item::base(self : Item[T], target? : String, extension? : String, vars? : Map[String,
Value
]) -> Item[T]

Returns a copy of self with target, extension, and/or vars overridden by the given optional arguments, leaving data and any unspecified fields unchanged.

extension, when provided, should include its leading dot (e.g. .html), consistent with set_extension.

#
Item::clear_var

fn[T] Item::clear_var(self : Item[T], key : String) -> Item[T]

Returns a copy of self with the variable named key removed from its vars, if present.

#
Item::location

fn[T] Item::location(self : Item[T]) -> String

Returns this item's target path, e.g. for use in resolving relative links, breadcrumbs, or previous/next navigation (see @summary.Summary::breadcrumb and @summary.Summary::prev_next).

#
Item::map

fn[T, R] Item::map(self : Item[T], f : (T) -> R, target? : String, extension? : String, vars? : Map[String,
Value
]) -> Item[R]

Applies f to this item's data to produce a new item with data of type R, optionally overriding target, extension, and/or vars in the same way as Item::base.

extension, when provided, should include its leading dot (e.g. .html), consistent with set_extension.

#
Item::map_with_raise

fn[T, R] Item::map_with_raise(self : Item[T], f : (T) -> R raise, target? : String, extension? : String, vars? : Map[String,
Value
]) -> Item[R] raise

Like Item::map, but f may raise; the resulting error propagates out of this call rather than being caught.

extension, when provided, should include its leading dot (e.g. .html), consistent with set_extension.

#
Item::new

fn[T] Item::new(data : T, target : String, extension? : String, vars? : Map[String,
Value
]) -> Item[T]

Creates a new item with the given data and target path, optionally overriding its extension and/or providing an initial set of template variables.

extension, when provided, should include its leading dot (e.g. .html), consistent with set_extension.

#
Item::set_var

fn[T] Item::set_var(self : Item[T], key : String, value :
Value
) -> Item[T]

Returns a copy of self with the variable named key set to value in its vars, overwriting any existing entry with the same key.

#
Options

pub(all) struct Options {
src : String
dest : String
} derive(Eq,
Debug
)

The source and destination directories for a build, relative to the project root.
impl Default for Options

#
Options::from_config

Derives build Options (source/destination directories) from a project's BookConfig.

#
Rule

pub(all) enum Rule {
Glob(String, Handler)
Guard(String, (String) -> Bool, Handler)
} derive(
Debug
)

A single rule describing how files matching a pattern should be processed during a build.

#
Rule::check

fn Rule::check(self : Rule, path : String) -> Bool

Reports whether path matches this rule (i.e. whether it should be processed according to this rule's Handler).

#
Rule::handler

fn Rule::handler(self : Rule) -> Handler

Returns this rule's processing Handler.

#
Thing

pub(all) enum Thing {
Doc(String)
Asset(Bytes)
Multiple(Array[(String, Thing)])
Empty
} derive(Eq,
Debug
)

The final, writable output produced for a build item.

#
Thing::write

fn Thing::write(self : Thing, dest : String, target : String) -> Unit raise
IOError

Writes this Thing to disk under dest, using name as the output file name (including extension) — or, for Multiple, as the base directory under which each named sub-output is written.

This is a thin wrapper around the underlying filesystem write operations (see @fs); no additional processing (e.g. templating or transformation) is applied here.

Raises @fs.IOError if writing fails.

#
apply_template

fn apply_template(body : Item[String], template : Array[
TemplateNode
]) -> Item[String]

Renders item's HTML body against the given template AST, replacing item's data with the rendered output.

Any variables previously attached to item via Item::add_vars / Item::set_var (as well as any variables implicitly set by other template-related steps such as import_css/import_js/inject_head/ inject_body) are available to the template as its rendering context.

Rendering errors (e.g. an undefined variable reference, or a failed partial) are tolerated on a per-node basis: the offending template node is simply rendered as empty output, and rendering continues for the rest of the template, rather than the whole render failing. Use apply_template_strict if such errors should instead abort rendering entirely.

#
apply_template_strict

fn apply_template_strict(body : Item[String], template : Array[
TemplateNode
]) -> Item[String] raise

Like apply_template, but raises a template rendering error (see @template.TemplateRenderError) as soon as any node fails to render, instead of tolerating the failure by rendering that node as empty output.

#
extract_markdown

fn[T : Show] extract_markdown(item : Item[T]) -> Item[(Map[String, String], String)]

Splits item's data into its YAML frontmatter and Markdown body.

The data is expected to be Show-able text in the usual frontmatter convention (a ----delimited YAML block at the top of the document, followed by the document body). The resulting item's data is a tuple of the parsed frontmatter (as a Map[String, String]) and the remaining body text.

#
import_css

fn import_css(item : Item[String], urls : Array[String]) -> Item[String]

Adds <link>-style CSS imports (rather than inlined styles) to item, referencing external stylesheets by path/URL rather than embedding their contents — as opposed to use_css, which injects raw CSS code directly.

#
import_js

fn import_js(item : Item[String], urls : Array[String]) -> Item[String]

Adds <script src>-style JS imports (rather than inlined scripts) to item, referencing external scripts by path/URL rather than embedding their contents — as opposed to use_js, which injects raw JS code directly.

#
inject_body

fn inject_body(item : Item[String], code : String) -> Item[String]

Appends html as a raw HTML snippet to be injected at the end of the rendered page's <body>.

#
inject_head

fn inject_head(item : Item[String], code : String) -> Item[String]

Appends html as a raw HTML snippet to be injected into the rendered page's <head>.

#
load_and_apply_template

fn load_and_apply_template(body : Item[String], template_path : String, strict? : Bool) -> Item[String] raise

Loads the template file at path, parses it, and applies it to item, combining @template.parse_template with apply_template (or apply_template_strict when strict is true) in one step.

When strict is false (the default), rendering errors are tolerated on a per-node basis (the offending node renders as empty output); when true, any such error instead raises immediately, aborting the render.

Raises if the template file cannot be read or cannot be parsed (see @template.TemplateParseError) — regardless of strict — or, when strict is true, if rendering itself fails (see @template.TemplateRenderError).

#
mo_build

async fn mo_build(options : Options, rules : Array[Rule]) -> Unit

Runs the build pipeline described by rules against the given options (source/destination directories), writing all resulting output to options.dest.

Each rule's glob pattern (and, for Guard rules, its predicate) is matched against files discovered under options.src; matching files are processed according to the rule's Mode and the result is written to disk via Thing::write.

#
mo_serve

async fn mo_serve(options : Options) -> Unit

Serves the site described by options locally, e.g. for development and live preview, rather than writing output to disk.

#
mopress

async fn mopress(rules : Array[Rule], options? : Options) -> Unit

Convenience entry point that runs mo_build with the given rules against options (defaulting to Options::default() if not provided).

#
mopress_with

async fn[E] mopress_with(options? : Options, init : (Options) -> E, rules : (Options, E) -> Array[Rule]) -> Unit

Like mopress, but allows deriving the set of rules from some intermediate value E computed from options: setup is called with options to produce a value of type E, which is then passed (together with options) to build_rules to produce the final list of rules to run.

This is useful when the rule set itself depends on some computation or resource derived from options (e.g. parsed configuration or a parsed summary/table of contents) that would otherwise need to be recomputed independently by the caller before constructing rules.

#
parse_markdown

fn[T : Show] parse_markdown(item : Item[T]) -> Item[Array[
Block
]]

Parses item's data as Markdown, replacing it with the resulting AST.

#
render_markdown

fn[T : Show] render_markdown(item : Item[T]) -> Item[String]

Parses item's data as Markdown and renders it directly to HTML, replacing the item's data with the rendered HTML string. Frontmatter, if present, is not separated out by this step (see render_markdown_and_frontmatter if frontmatter needs to be preserved/handled).

#
render_markdown_and_frontmatter

fn[T : Show] render_markdown_and_frontmatter(item : Item[T]) -> Item[String]

Like render_markdown, but first separates YAML frontmatter from the Markdown body (see separate_frontmatter) before parsing and rendering, so that frontmatter present in item's data does not get rendered as part of the Markdown body.

#
render_markdown_and_frontmatter_from_ast

fn render_markdown_and_frontmatter_from_ast(item : Item[Array[
Block
]], frontmatter : Map[String, String]) -> Item[String]

Renders an already-parsed Markdown AST directly to HTML, combining it with the given frontmatter map. This is the AST-based counterpart of render_markdown_and_frontmatter, for use when the Markdown has already been parsed (and possibly transformed) rather than starting from raw text.

#
render_markdown_from_ast

fn render_markdown_from_ast(item : Item[Array[
Block
]]) -> Item[String]

Renders an already-parsed Markdown AST directly to HTML, replacing item's data with the resulting HTML string.

#
separate_frontmatter

fn separate_frontmatter(item : Item[String]) -> Item[(Map[String, String], String)]

Splits item's data into its YAML frontmatter and the remaining raw text body, without parsing the body as Markdown (see extract_markdown for the equivalent step used when the data is not necessarily Show but the resulting body is still expected downstream as Markdown source).

#
set_extension

fn[T] set_extension(item : Item[T], extension : String) -> Item[T]

Returns a copy of item with its target extension changed to extension (including the leading dot, e.g. .html), without modifying its data.

#
unify

fn[T : Thingable] unify(item : Item[T]) -> Item[Thing]

Converts item's data into a Thing, the common output representation used when writing build results to disk (see Thing::write).

This is typically the final step applied to an item before it is handed off as the result of a Handler::Text or Handler::Binary handler.

#
use_css

fn use_css(item : Item[String], codes : Array[String]) -> Item[String]

Adds code entries as inline <style> blocks injected just before </head>, one <style> tag per array element — as opposed to import_css, which references external stylesheets via <link>.

#
use_js

fn use_js(item : Item[String], codes : Array[String]) -> Item[String]

Adds code entries as inline <script> blocks injected just before </body>, one <script> tag per array element — as opposed to import_js, which references external scripts via <script src>.

#
use_preprocessor

fn use_preprocessor(item : Item[String], handlers : Array[(Item[String]) -> Item[String] raise]) -> Item[String] raise

Runs item through a sequence of preprocessing steps in order, each receiving the output of the previous one as its input.

This is the in-process counterpart to @bridge.run_markdown_preprocessors's external-command pipeline; preprocessors here are ordinary MoonBit functions rather than external processes.

Raises if any step in preprocessors raises.

#
use_transformer

Runs item through a sequence of AST-transforming steps in order, each receiving the output of the previous one as its input.

This is the in-process counterpart to @bridge.run_markdown_transformers's external-command pipeline; transformers here are ordinary MoonBit functions rather than external processes.

Raises if any step in transformers raises.