highlight_cr

Version, currently 0.1.04 versions

github.com/OrelSokolov/highlight.cr

Порт highlight.js 11.12.0 на Crystal — 1:1 по результатам. Синтаксическая подсветка: движок режимов (subLanguage, автодетект, relevance), HTML-эмиттер с tiered-scope классами и common-набор из 37 языков. Без внешних зависимостей.

0 stars
2 dependents
License: not declared

Nothing has been indexed for 0.1.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  highlight_cr:
    github: OrelSokolov/highlight.cr
    version: ~> 0.1.0

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.1.0. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

highlight.cr

A Crystal port of highlight.js 11.12.0 — syntax highlighting with byte-for-byte identical HTML output to the original JavaScript library. No external dependencies.

Parity is verified by an oracle test suite: 96/96 test cases produce exactly the same HTML and relevance scores as the vendored highlight.js 11.12.0 (crystal spec spec/oracle_spec.cr).

Since the emitted markup is identical (same hljs-* classes, same structure), any highlight.js CSS theme works as-is.

Installation

Add this to your application's shard.yml:

dependencies:
  highlight_cr:
    github: OrelSokolov/highlight.cr

Usage

require "highlight_cr"

result = Highlight.highlight("puts 'hello'", "ruby")
puts result.value     # => puts <span class="hljs-string">'hello'</span>
puts result.relevance # => 1

# Auto-detection
auto = Highlight.highlight_auto("def foo; end")
puts auto.language    # => "ruby"

# Custom instance with only the languages you need
hljs = Highlight::HLJS.new
hljs.register_language("ruby")
result = hljs.highlight(code, "ruby")

What is ported from the original library

highlight.js featurehighlight.crNotes
Highlighting engine (mode compiler, subLanguage, continuations, relevance, illegal handling)✅ PortedFull port of lib/core.js
HTML emitter (tiered scope class names, sublanguages)✅ PortedByte-identical output
Auto-detection (highlightAuto, secondBest, tie-breaking by registration order and superset rules)✅ Ported
Common bundle — all 36 common languages + ERB✅ PortedSee list below
Language aliases (registerAliases)✅ Ported
Plugins (before:highlight, after:highlight)✅ Ported
configure (classPrefix, languages)✅ Ported
Token stream (Highlight.tokens)✅ PortedFlat {scope, text} pairs for GUI consumers
DOM API (highlightElement, highlightAll, etc.)❌ Not portedhighlight.cr is a pure server-side library; there is no DOM in Crystal
Web Worker / browser builds❌ Not portedN/A for Crystal
__emitTokens-style languages❌ Not portedNone exist in the common bundle
Languages beyond the common bundle✅ ERB portedOthers can be ported following the same scheme (see CONVENTIONS.md)
CSS themes➖ ReusedUse any highlight.js theme; markup is identical

Languages (37)

xml, bash, c, cpp, csharp, css, markdown, diff, ruby, go, graphql, ini, java, javascript, json, kotlin, less, lua, makefile, perl, objectivec, php, php-template, plaintext, python, python-repl, r, rust, scss, shell, sql, swift, yaml, typescript, vbnet, wasm, erb

Intentional differences from the JS original

  • Byte indices: the engine operates on byte offsets (byte_begin, byte_slice) where the original uses UTF-16 code units; results match on all oracle snippets.
  • Regexes: JS regexes are translated to PCRE2 by JSRegex.translate — \w/\d/\b/\s are expanded to JS (non-Unicode) semantics, . becomes [^\n\r\u2028\u2029], \uXXXX becomes \x{...}.
  • Typing: relevance is Int32 | Float64 (integers render without .0); JS null overrides in inherit are expressed via clear_* flags.

Development

crystal spec spec/oracle_spec.cr        # run the 98 oracle cases
node oracle/cases.js > oracle/expected.yml  # regenerate expectations
crystal run spec/diff_dbg.cr -- <lang> <snippet>  # find first output divergence

Architecture notes live in PLAN.md, porting conventions in CONVENTIONS.md.