parse

fun parse(context: Context, cssAssetPath: String): Map<String, SpanStyle>

Parses a CSS theme file from assets into a color map. Results are not cached here - callers should use lazy to cache per theme.

Silently returns an empty map on any error. Use parseAsset if you need to distinguish between a missing file and an empty/unparseable theme.


fun parse(cssText: String): Map<String, SpanStyle>

Parses CSS text directly into a color map. Extracts SpanStyle for each .hljs-* selector block.

Implementation: a small recursive-descent parser over a hand-written tokenizer. Highlight.js theme CSS uses a strict, predictable subset of CSS - flat top-level rules, occasional @media / @supports / @keyframes blocks, no nested rules - so a tiny grammar handles every known theme without pulling in a full CSS engine.

The parser:

  1. Skips comments and at-rule blocks (so @media rules can't clobber main rules).

  2. Walks each top-level selectors { declarations } rule.

  3. Filters individual selectors: only standalone .hljs[-...] chains are accepted; descendant combinators, pseudo-elements (::selection), and pseudo-classes (:hover) are skipped.

  4. Delegates declaration parsing to parseDeclarations (unchanged).

  5. Merges into the result map via mergeSpanStyle so split rules for the same selector accumulate (CSS cascade behaviour).