Expand description
C ABI for colorfulcode, for native apps written in Swift, Kotlin, C++, C#
and friends. The matching header is include/colorfulcode.h.
Highlighting returns a flat array of cc_tokens that covers the input,
and cc_theme_style maps each token kind to a color, so drawing comes
down to one attribute per token.
Conventions:
- Languages and themes are referred to by index. Indices are only stable within one build of the library, so persist ids or names instead.
- Input strings must be UTF-8. Functions taking source code return NULL for invalid UTF-8 rather than guessing.
- Null pointers are accepted everywhere and treated as missing input.
- Panics never cross the boundary: they are caught, or abort in release
builds where
panic = "abort".
Structs§
- CcStyle
- How a token kind is drawn.
- CcToken
- A highlighted span of the input,
start..end. - CcTokens
- The result of
cc_highlight. Opaque to C.
Constants§
- CC_
ANSI_ 256 cc_render_ansiflag: use the 256 color palette instead of 24 bit colors.- CC_
HTML_ LINES cc_render_htmlflag: wrap every line in<span class="cc-line">.- CC_
HTML_ WRAP cc_render_htmlflag: wrap the output in<pre class="cc"><code>.- CC_
UTF16_ OFFSETS cc_highlightflag: report offsets in UTF-16 code units instead of bytes.
Functions§
- cc_
highlight ⚠ - Highlights
lenbytes of UTF-8code. An unknown language index means plain text. Returns NULL for invalid UTF-8. Free withcc_tokens_free. - cc_
kind_ name - Lowercase name of a token kind, for example
"keyword". NULL if out of range. - cc_
language_ count - Number of languages.
- cc_
language_ ⚠detect - Guesses the language from the content alone, scoring the first few KB against every language. Opt-in: highlighting never calls it. Returns -1 when unsure or for invalid UTF-8.
- cc_
language_ ⚠find - Finds a language by id, alias, name or extension. Returns -1 if unknown.
- cc_
language_ ⚠from_ content - Guesses the language from a shebang or similar first line. Returns -1 if unknown.
- cc_
language_ ⚠from_ path - Guesses the language from a file path. Returns -1 if unknown.
- cc_
language_ id - Canonical id of a language, for example
"rust". NULL if out of range. - cc_
language_ name - Display name of a language, for example
"Rust". NULL if out of range. - cc_
render_ ⚠ansi - Renders text with ANSI color escapes. Out of range themes mean the
default theme. Takes
CC_ANSI_256. Returns NULL for invalid UTF-8 or NUL bytes in the input. Free withcc_string_free. - cc_
render_ ⚠html - Renders HTML. A valid theme index gives inline styles, -1 gives class
names. Takes
CC_HTML_*flags. Returns NULL for invalid UTF-8 or NUL bytes in the input. Free withcc_string_free. - cc_
string_ ⚠free - Frees a string returned by this library. NULL is ignored.
- cc_
theme_ background - Background color as
0xRRGGBB. Out of range means the default theme. - cc_
theme_ count - Number of themes.
- cc_
theme_ ⚠css - CSS for the class based HTML, scoped to
selector(NULL means".cc"). Free withcc_string_free. - cc_
theme_ ⚠find - Finds a theme by name (case insensitive). Returns -1 if unknown.
- cc_
theme_ foreground - Default text color as
0xRRGGBB. Out of range means the default theme. - cc_
theme_ is_ dark - 1 for a dark theme, 0 for a light one. Out of range means the default theme.
- cc_
theme_ name - Name of a theme, for example
"dracula". NULL if out of range. - cc_
theme_ style - Style of a token kind. Out of range themes mean the default theme and unknown kinds mean plain text.
- cc_
tokens_ ⚠data - The tokens, valid until
cc_tokens_free. NULL for NULL. - cc_
tokens_ ⚠free - Frees tokens. NULL is ignored.
- cc_
tokens_ ⚠len - Number of tokens. 0 for NULL.
- cc_
version - Library version, for example
"0.1.0".