#formats

    Parsers and front matter. Everything becomes Json, so the rest of the pipeline never sees YAML, TOML or INI syntax.

    Import as justjavac/fus/formats.

    #What it provides

    • parse_yaml, parse_json, parse_toml, parse_ini return Json; malformed input yields an empty object instead of raising, so a bad file never aborts a build on its own.
    • parse_format(text, ext) dispatches by extension, and parse_ok(text, ext) answers whether the text parses at all.
    • split_front_matter(text) returns (front, body) for the three delimiters (--- YAML, ;;; JSON, +++ TOML); has_unclosed_front_matter lets check report the common mistake.
    • FormatRegistry adds formats without touching the build: register, register_alias, set_fallback, supports, extensions, parse. FormatRegistry::builtin() covers the four formats above.

    #Example

    let (front, body) = split_front_matter(source)
    let title = @core.as_str(@core.get(front, "title"))

    Front matter is parsed before templating, so a template can read every field of the page it renders.

    FormatRegistry

    pub struct FormatRegistry {
    parsers : Map[String, (StringView) ->
    Json
    ]
    aliases : Map[String, String]
    fallback :
    Ref
    [String]
    }

    A registry of data formats, keyed by the extension that selects them.

    FormatRegistry::builtin() covers YAML, JSON, TOML and INI. New formats (CSV, JSON5, KDL, ...) are added with register and are then usable for configuration, data files and front matter alike.

    FormatRegistry::builtin

    Create a registry with YAML, JSON, TOML and INI registered.

    YAML owns .yaml / .yml and is also the fallback for unknown extensions, matching the behaviour of the single-format loader Fus shipped with.

    FormatRegistry::extensions

    fn FormatRegistry::extensions(self : FormatRegistry) -> Array[String]

    Every registered extension, sorted, for diagnostics and docs.

    FormatRegistry::new

    Create an empty registry with no fallback: unknown formats yield {}.

    FormatRegistry::parse

    fn FormatRegistry::parse(self : FormatRegistry, text : StringView, ext : StringView) ->
    Json

    Parse text as ext.

    An unregistered extension falls back to the configured fallback parser (YAML in builtin), and a parser error yields an empty object: a broken data file should not abort a build.

    FormatRegistry::register

    fn FormatRegistry::register(self : FormatRegistry, ext : StringView, parser : (StringView) ->
    Json
    ) -> Unit

    Register parser for one extension (case-insensitive, no leading dot).

    FormatRegistry::register_alias

    fn FormatRegistry::register_alias(self : FormatRegistry, from : StringView, to : StringView) -> Unit

    Point from at the parser already registered for to.

    FormatRegistry::set_fallback

    fn FormatRegistry::set_fallback(self : FormatRegistry, ext : StringView) -> Unit

    Choose the parser used when an extension is not registered.

    FormatRegistry::supports

    fn FormatRegistry::supports(self : FormatRegistry, ext : StringView) -> Bool

    Whether an extension can be parsed by this registry.

    format_of_path

    fn format_of_path(path : StringView) -> String

    Lowercase extension of path without the leading dot, ready for parse.

    has_unclosed_front_matter

    fn has_unclosed_front_matter(text : StringView) -> Bool

    Detect a front matter block that opens but never closes.

    parse_format

    fn parse_format(text : StringView, format : StringView) ->
    Json

    Parse text as the named format using the built-in registry.

    parse_ini

    fn parse_ini(text : StringView) ->
    Json

    Parse a small INI document: [section] headers and key = value pairs.

    Top-level keys stay at the root, section keys become nested objects, and scalars are typed so port = 8000 works without a schema.

    parse_json

    fn parse_json(text : StringView) ->
    Json

    Parse JSON with the core parser.

    parse_ok

    fn parse_ok(text : StringView, ext : StringView) -> Bool

    Whether a document parses cleanly. Parsers used by the registry are deliberately forgiving; configuration loading uses this check to warn instead of silently falling back to every default.

    parse_toml

    fn parse_toml(text : StringView) ->
    Json

    Parse TOML with moonbit-community/toml.

    parse_yaml

    fn parse_yaml(text : StringView) ->
    Json

    Parse YAML with moonbit-community/yaml, taking the first document.

    split_front_matter

    fn split_front_matter(text : StringView) -> (
    Json
    , String)

    Split front matter using the built-in format registry.

    split_front_matter_with

    fn split_front_matter_with(text : StringView, formats : FormatRegistry) -> (
    Json
    , String)

    Split a source file into front matter data and the remaining body.

    --- opens YAML, ;;; opens JSON and +++ opens TOML front matter; the block ends at the next line holding the same delimiter. Anything else is body text and the data is an empty object.