A port of Pygments (generic syntax highlighter) to MoonBit
Dependencies
///|
test "lex some Python" {
let lexer = @lexers.get_lexer_by_name("python")
let tokens = @pygments.lex("print('hi')\n", lexer)
inspect(
tokens.map(t => "\{t.0} \{t.1.escape()}").join("\n"),
content=(
#|Token.Name.Builtin "print"
#|Token.Punctuation "("
#|Token.Literal.String.Single "'"
#|Token.Literal.String.Single "hi"
#|Token.Literal.String.Single "'"
#|Token.Punctuation ")"
#|Token.Text.Whitespace "\n"
),
)
}///|
test "highlight to HTML" {
let html = @pygments.highlight(
"print('hi')\n",
@pygments.get_lexer_by_name("python"),
@pygments.get_formatter_by_name("html", options={ "noclasses": "True" }),
)
inspect(html.has_prefix("<div class=\"highlight\""), content="true")
}///|
test "lexer lookup" {
inspect(@lexers.get_lexer_for_filename("main.rs").name(), content="Rust")
inspect(
@lexers.guess_lexer("#!/usr/bin/env python3\nimport os\n").name(),
content="Python",
)
}moonx bobzhang/pygments/cmd/scat main.py # 16-color, follows the terminal theme
moonx bobzhang/pygments/cmd/scat -s monokai -n lib.rs # a style, with line numbers
cat build.log | moonx bobzhang/pygments/cmd/scat -l console
moonx bobzhang/pygments/cmd/scat --langs # all 600+ languages# run straight from mooncakes.io, no checkout needed
moonx bobzhang/pygments/cmd/pygmentize -f terminal256 -O style=monokai main.py
# or build it (native or wasm)
moon build --target native --release cmd/pygmentize
./_build/native/release/build/cmd/pygmentize/pygmentize.exe -f terminal256 -O style=monokai main.py| Package | Contents |
|---|---|
| regex/ | Python-re compatible backtracking regex engine |
| token/ | token type hierarchy (Token.Name.Builtin, …) |
| lexer/ | lexer runtime: RegexLexer/ExtendedRegexLexer interpreter, DelegatingLexer, do_insertions, options |
| lexers/ | all lexers: tables generated from upstream (gen_*.mbt) plus hand-written parts, registry |
| styles/, formatters/, filters/ | styles, output formatters and token filters |
| cmd/scat | cat with syntax highlighting |
| cmd/pygmentize | Python-compatible pygmentize |
| pystr/ | Python string semantics used by the formatters (repr, case mapping, …) |
| cmd/*_oracle | differential test drivers against Python |
moon test # unit tests (also --target js / wasm-gc)
scripts/conformance.sh # differential tests against Python 3.14 + Pygments
scripts/regen.sh # regenerate all generated MoonBit filestest {
let f = @formatters.get_formatter_by_name("html", options={ "nowrap": "true" })
inspect(
f.format([(@token.keyword, "if")]),
content="<span class=\"k\">if</span>\n",
)
}test {
inspect(
@formatters.get_formatter_for_filename("out/a.tex").name(),
content="LaTeX",
)
}test {
let lx = @lexers.get_lexer_by_name("py")
inspect(lx.name(), content="Python")
}test {
let html = @pygments.highlight(
"x = 1\n",
@pygments.get_lexer_by_name("python"),
@pygments.get_formatter_by_name("html"),
)
inspect(
html,
content=(
#|<div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
#|</pre></div>
#|
),
)
}test {
let lexer = @lexers.get_lexer_by_name("python")
let tokens = @pygments.lex("x = 1\n", lexer)
inspect(tokens[0].0, content="Token.Name")
inspect(tokens.map(t => t.1).join(""), content="x = 1\n")
}Install
Download zipA port of Pygments (generic syntax highlighter) to MoonBit
Dependencies