html_parser_examples

    Runnable examples and documentation for bobzhang/html_parser.

    html
    parser
    formatter
    examples
    Download zip
    Author
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    44 minutes ago
    Downloads
    70

    #HTML Parser Examples

    This workspace module contains runnable examples for bobzhang/html_parser. From the repository root, the moon.work workspace makes the examples use the local parser checkout.

    #HTML Formatter

    The examples/cmd/htmlfmt command formats an HTML fragment with the public parser and serializer API:

    moon run --target native examples/cmd/htmlfmt -- "<article><p>Hello <b>MoonBit</b></p></article>"

    It prints:

    <article> <p>Hello <b>MoonBit</b></p> </article>

    Pass --compact to serialize without pretty-printing:

    moon run --target native examples/cmd/htmlfmt -- --compact "<p class='intro'>Hello <b>MoonBit</b></p>"

    The formatting helper can also be used directly from MoonBit:

    ///|
    test "examples readme formats a fragment" {
    assert_eq(
    @html_parser_examples.pretty_fragment(
    "<article><p>Hello <b>MoonBit</b></p></article>",
    ),
    "<article>\n <p>Hello <b>MoonBit</b></p>\n</article>",
    )
    assert_eq(
    @html_parser_examples.compact_fragment(
    "<p class='intro'>Hello <b>MoonBit</b></p>",
    ),
    "<p class=\"intro\">Hello <b>MoonBit</b></p>",
    )
    }

    The same parser backs the CLI command:

    ///|
    test "examples readme runs htmlfmt" {
    let result = @html_parser_examples.run_htmlfmt([
    "--compact", "<p class='intro'>Hello <b>MoonBit</b></p>",
    ])
    assert_eq(result.exit_code, 0)
    assert_eq(result.stdout, "<p class=\"intro\">Hello <b>MoonBit</b></p>")
    }

    HtmlFmtResult

    pub(all) struct HtmlFmtResult {
    exit_code : Int
    stdout : String
    stderr : String
    } derive(Eq,
    Debug
    )

    Result returned by the example htmlfmt command runner.

    exit_code follows command-line conventions: 0 for formatted output and 2 for argument parsing errors. stdout carries formatted HTML on success; stderr carries parser help or diagnostics when argument parsing fails.

    HtmlFmtResult::equal

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn HtmlFmtResult::equal(HtmlFmtResult, HtmlFmtResult) -> Bool
    automatically derived

    HtmlFmtResult::not_equal

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn HtmlFmtResult::not_equal(x : HtmlFmtResult, y : HtmlFmtResult) -> Bool
    automatically derived

    HtmlFmtResult::to_repr

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn HtmlFmtResult::to_repr(HtmlFmtResult) ->
    Repr
    automatically derived

    compact_fragment

    fn compact_fragment(input : String) -> String raise

    Format an HTML fragment as compact markup.

    This parses the same fragment input as pretty_fragment, but serializes it without layout whitespace so the result is suitable for golden-output checks or CLI output that should stay on one line.

    pretty_fragment

    fn pretty_fragment(input : String, indent_size? : Int) -> String raise

    Format an HTML fragment as readable, indented markup.

    The fragment is parsed with the library's default fragment context and then serialized with pretty printing enabled. indent_size controls the number of spaces used for each nested element level.

    run_htmlfmt

    fn run_htmlfmt(args : ArrayView[String]) -> HtmlFmtResult raise

    Run the example htmlfmt command with an argument vector.

    The command accepts one HTML fragment positional argument and an optional --compact flag. Successful runs return formatted HTML in stdout; parse errors from the command-line parser are converted into a nonzero HtmlFmtResult instead of escaping as exceptions.