html_parser_examples

Runnable examples and documentation for bobzhang/html_parser.

html
parser
formatter
examples
moon add bobzhang/html_parser_examples@0.1.0
Download zip
Author
Version
0.1.0
License
Apache-2.0
Last updated
2 months ago
Downloads
22

Dependencies

README

#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
)

#
compact_fragment

fn compact_fragment(input : String) -> String raise

#
pretty_fragment

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

#
run_htmlfmt

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

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io