Skip to content

Commit 08efdf6

Browse files
committed
docs(highlight): overhaul syntax highlighting grammar and color palette
Rewrite radon-highlight.js and extra.css to use the same token color palette as radon-project.github.io and the Radon REPL terminal. radon-highlight.js: - Inject !important CSS overrides at runtime to beat Material for MkDocs' bundled hljs theme rules, which override standard class names (.hljs-keyword, .hljs-string, .hljs-number, .hljs-comment, .hljs-operator) at higher cascade order. Non-standard names (.hljs-builtin, .hljs-fncall, .hljs-special, .hljs-punctuation) do not need !important. - Rewrite grammar rule order (first-match wins): multi-line comment (#! … !#) → single-line comment (#) → string → builtin → keyword → number → special → fncall → operator → punctuation. - Add multi-line comment rule (#! … !#) before single-line so #! is not consumed by the # rule. - Expand keyword list from 6 to 30+ keywords sourced from core/tokens.py KEYWORDS list. Add 'as', 'elif', 'for', 'while', 'return', 'import', 'from', 'in', 'and', 'or', 'not', 'try', 'catch', 'raise', 'break', 'continue', 'const', 'static', 'assert', 'switch', 'case', 'default', 'fallthrough', 'fallout', 'del', 'step', 'to'. Remove invalid 'end'. Add 'null', 'true', 'false', 'this', '__constructor__' as keyword-like. - Add builtin class sourced from create_global_symbol_table() in core/builtin_funcs.py. Includes all 37 built-in functions plus 5 built-in classes: File, String, Json, Requests, builtins. - Add fncall rule using lookahead (?=\s*\() so ( stays outside the span and falls through to punctuation. - Add punctuation rule for {}()[],.;: - Expand operator regex to cover multi-char operators (->, ++, --, ==, !=, <=, >=, +=, -=, *=, /=, %=) before single-char to prevent partial matches. - Fix number regex to also match floats (\b\d+(?:\.\d+)?\b). - Remove invalid hljs.IMMEDIATE_RE reference from string rule. - Replace hljs.highlightAll() with explicit querySelectorAll targeting .language-text.highlight pre code blocks (MkDocs Pygments TextLexer fallback when no 'rn' lexer is registered) and re-highlight them as 'rn' using block.textContent to strip Pygments line-number anchors. extra.css: - Replace all token colors with the new palette matching radon-project.github.io. - Add !important on .hljs-keyword, .hljs-string, .hljs-number, .hljs-comment, .hljs-operator as a CSS fallback matching the runtime JS injection. - Add new rules: .hljs-builtin (#34d399), .hljs-punctuation (#94a3b8). - Remove .hljs-function (replaced by .hljs-fncall + .hljs-special, both #c084fc). - Set .hljs-identifier to inherit (no coloring for plain identifiers). Token palette: keyword #7dd3fc sky-blue bold builtin #34d399 emerald string #a3e635 lime-green number #fb923c orange operator #f87171 salmon punctuation #94a3b8 slate-gray comment #5c6591 slate-purple italic fncall #c084fc violet special #c084fc violet
1 parent 8057ec5 commit 08efdf6

2 files changed

Lines changed: 86 additions & 48 deletions

File tree

docs/assets/radon-highlight.js

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,96 @@ var script1 = document.createElement('script');
22
script1.src = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/highlight.min.js';
33
script1.defer = true;
44
script1.onload = function () {
5+
// Inject Radon color palette with !important to beat Material theme's
6+
// bundled hljs CSS which overrides standard class names (keyword, number,
7+
// operator). Non-standard names (builtin, fncall, special) don't need it.
8+
var rnStyle = document.createElement('style');
9+
rnStyle.textContent =
10+
'.hljs-keyword { color: #7dd3fc !important; font-weight: 600; }\n' +
11+
'.hljs-string { color: #a3e635 !important; }\n' +
12+
'.hljs-number { color: #fb923c !important; }\n' +
13+
'.hljs-comment { color: #5c6591 !important; font-style: italic; }\n' +
14+
'.hljs-operator { color: #f87171 !important; }\n' +
15+
'.hljs-builtin { color: #34d399; }\n' +
16+
'.hljs-special { color: #c084fc; }\n' +
17+
'.hljs-fncall { color: #c084fc; }\n' +
18+
'.hljs-punctuation{ color: #94a3b8; }\n';
19+
document.head.appendChild(rnStyle);
20+
521
hljs.registerLanguage('rn', function (hljs) {
622
return {
723
contains: [
24+
// Multi-line comment FIRST — #! ... !#
25+
// Must come before single-line so #! isn't consumed by the # rule
826
{
9-
className: 'keyword',
10-
begin: '\\b(class|fun|if|else|true|false|null)\\b'
27+
className: 'comment',
28+
begin: '#!',
29+
end: '!#'
1130
},
31+
// Single-line comment — # to end of line
1232
{
13-
className: 'string',
14-
begin: '"', end: '"',
15-
contains: [
16-
{
17-
className: 'escape',
18-
begin: '\\\\.', end: hljs.IMMEDIATE_RE
19-
}
20-
]
33+
className: 'comment',
34+
begin: '#',
35+
end: '$'
2136
},
37+
// Strings second — protect their content from keyword matches
2238
{
23-
className: 'number',
24-
begin: '\\b\\d+\\b'
39+
className: 'string',
40+
begin: '"',
41+
end: '"',
42+
contains: [{ begin: '\\\\.' }]
2543
},
44+
// Builtins before keywords so e.g. "str", "int" don't hit keyword
45+
// Source: create_global_symbol_table() in core/builtin_funcs.py
2646
{
27-
className: 'special',
28-
begin: '(?<=\\b(?:class|import|fun)\\s+)\\w+'
47+
className: 'builtin',
48+
begin: '\\b(print|print_ret|input|input_int|clear|cls|require|exit|len|is_num|is_int|is_float|is_str|is_bool|is_array|is_fun|is_null|arr_append|arr_pop|arr_extend|arr_len|arr_chunk|arr_get|str_len|str_find|str_get|int|float|str|bool|type|pyapi|time_now|license|credits|copyright|help|dir|File|String|Json|Requests|builtins)\\b'
2949
},
3050
{
31-
className: 'special',
32-
begin: '\_\_constructor\_\_|this'
51+
className: 'keyword',
52+
begin: '\\b(var|fun|class|if|elif|else|for|while|return|import|from|in|as|and|or|not|null|true|false|this|__constructor__|break|continue|try|catch|raise|step|to|const|static|assert|switch|case|default|fallthrough|fallout|del)\\b'
3353
},
3454
{
35-
className: 'fncall',
36-
begin: '(?<=\\b)(\\w+)\\s*\\('
55+
className: 'number',
56+
begin: '\\b\\d+(?:\\.\\d+)?\\b'
3757
},
58+
// Name immediately after class/import/fun keyword
3859
{
39-
className: 'fncall',
40-
begin: '(\\)|\\()'
60+
className: 'special',
61+
begin: '(?<=\\b(?:class|import|fun)\\s+)\\w+'
4162
},
63+
// Function calls — lookahead so the ( stays outside the span
4264
{
43-
className: 'comment',
44-
begin: '#',
45-
end: '\\n'
65+
className: 'fncall',
66+
begin: '\\b\\w+(?=\\s*\\()'
4667
},
68+
// Multi-char operators before single-char to avoid partial matches
4769
{
48-
className: 'identifier',
49-
begin: '(\\w+)'
70+
className: 'operator',
71+
begin: '->|\\+\\+|--|==|!=|<=|>=|-=|\\+=|\\*=|/=|%=|[+\\-*/%^=<>!]'
5072
},
5173
{
52-
className: 'operator',
53-
begin: '(\\+|-|=|!|not|and\\^)'
74+
className: 'punctuation',
75+
begin: '[{}()\\[\\],.;:]'
5476
}
5577
]
5678
};
5779
});
58-
hljs.highlightAll();
80+
81+
// MkDocs Pygments has no 'rn' lexer — falls back to TextLexer, producing
82+
// <div class="language-text highlight"><pre><code>…</code></pre>.
83+
// Explicitly highlight those blocks as 'rn'.
84+
hljs.configure({ ignoreUnescapedHTML: true });
85+
86+
document.querySelectorAll(
87+
'.language-text.highlight pre code, pre code.language-rn'
88+
).forEach(function (block) {
89+
var result = hljs.highlight(block.textContent, {
90+
language: 'rn',
91+
ignoreIllegals: true
92+
});
93+
block.innerHTML = result.value;
94+
block.classList.add('hljs');
95+
});
5996
};
6097
document.head.appendChild(script1);

docs/stylesheets/extra.css

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
1+
/* Radon syntax highlight palette — mirrors radon-project.github.io token colors */
2+
/* !important needed on standard hljs class names to beat Material theme's bundled hljs CSS */
13
.hljs-keyword {
2-
color: #007acc;
3-
/* Brighter shade for keywords */
4-
font-style: italic;
4+
color: #7dd3fc !important;
5+
font-weight: 600;
56
}
67

78
.hljs-string {
8-
color: #d14;
9+
color: #a3e635 !important;
910
}
1011

1112
.hljs-number {
12-
color: #c92c2c;
13+
color: #fb923c !important;
1314
}
1415

1516
.hljs-comment {
16-
color: #6a737d;
17-
/* Softer gray for comments */
17+
color: #5c6591 !important;
1818
font-style: italic;
1919
}
2020

21-
.hljs-function {
22-
color: #795da3;
23-
font-weight: bold;
21+
.hljs-operator {
22+
color: #f87171 !important;
2423
}
2524

26-
.hljs-identifier {
27-
color: #2a7bde;
25+
.hljs-function,
26+
.hljs-special {
27+
color: #c084fc;
2828
}
2929

30-
.hljs-operator {
31-
color: #8b7f7f;
32-
font-weight: bold;
30+
.hljs-builtin {
31+
color: #34d399;
3332
}
3433

35-
.hljs-special {
36-
color: rgb(255, 187, 0);
37-
font-weight: bold;
34+
.hljs-identifier {
35+
color: inherit;
36+
}
37+
38+
.hljs-punctuation {
39+
color: #94a3b8;
3840
}
3941

4042
.hljs-fncall {
41-
color: #1c92d2;
42-
font-weight: bold;
43+
color: #c084fc;
4344
}

0 commit comments

Comments
 (0)