Module: Blusher::Shim

Defined in:
lib/blusher/shim.rb

Overview

The rouge-API drop-in: route every ‘RegexLexer#lex` through carmine when a table exists AND carmine handles the input natively; otherwise fall back to rouge unchanged. Zero-divergence by construction — carmine either returns a byte-identical token stream or declines.

Defined Under Namespace

Classes: TokenStream

Constant Summary collapse

QUALNAME =

qualified rouge token name (“Name.Builtin”) → the Token object.

{}
SHORTNAME =

qualified token name → rouge HTML shortname (“Name.Function” → “nf”, “Text” → “”). Passed to the fused Rust formatter so the whole lex+format pipeline produces one HTML String without materializing Ruby tokens.

{}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dirObject

Directory of per-lexer carmine tables (‘<tag>.json`). Generated by `rake tables` (carmine’s tools/extract.rb over the installed rouge).



57
58
59
# File 'lib/blusher/shim.rb', line 57

def dir
  @dir
end

Class Method Details

.routable?(tag) ⇒ Boolean

Whether a table is worth attempting natively. A table that contains a ‘callback` rule (rouge procs / recursion that carmine can’t execute) DECLINES the moment that rule is reached — and then rouge re-lexes the whole input from scratch, so the carmine attempt was pure wasted work. For a performance drop-in that net-loses, so we skip such lexers up front and let rouge own them. Verdict is a cheap substring scan over the raw table JSON (no parse), cached per tag. Zero-divergence holds: callback-free tables never decline, skipped tables run rouge verbatim.

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/blusher/shim.rb', line 73

def routable?(tag)
  return @routable[tag] if @routable.key?(tag)
  table = table_for(tag)
  @routable[tag] = !table.nil? && !table.include?('"kind":"callback"')
end

.table_for(tag) ⇒ Object



59
60
61
62
63
# File 'lib/blusher/shim.rb', line 59

def table_for(tag)
  return @cache[tag] if @cache.key?(tag)
  path = File.join(@dir, "#{tag}.json")
  @cache[tag] = (File.read(path) if File.exist?(path))
end