Skip to content

Commit 053016e

Browse files
Added support for Racket (#2315)
1 parent e27e65a commit 053016e

21 files changed

+566
-3
lines changed

components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

+6
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,12 @@
866866
"title": "R",
867867
"owner": "Golmote"
868868
},
869+
"racket": {
870+
"title": "Racket",
871+
"require": "scheme",
872+
"alias": "rkt",
873+
"owner": "RunDevelopment"
874+
},
869875
"jsx": {
870876
"title": "React JSX",
871877
"require": ["markup", "javascript"],

components/prism-racket.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Prism.languages.racket = Prism.languages.extend('scheme', {});
2+
3+
// Add brackets to racket
4+
// The basic idea here is to go through all pattens of Scheme and replace all occurrences of "(" with the union of "("
5+
// and "["; Similar for ")". This is a bit tricky because "(" can be escaped or inside a character set. Both cases
6+
// have to be handled differently and, of course, we don't want to destroy groups, so we can only replace literal "("
7+
// and ")".
8+
// To do this, we use a regular expression which will parse any JS regular expression. It works because regexes are
9+
// matches from left to right and already matched text cannot be matched again. We use this to first capture all
10+
// escaped characters (not really, we don't get escape sequences but we don't need them). Because we already captured
11+
// all escaped characters, we know that any "[" character is the start of a character set, so we match that character
12+
// set whole.
13+
// With the regex parsed, we only have to replace all escaped "(" (they cannot be unescaped outside of character sets)
14+
// with /[([]/ and replace all "(" inside character sets.
15+
// Note: This method does not work for "(" that are escaped like this /\x28/ or this /\u0028/.
16+
Prism.languages.DFS(Prism.languages.racket, function (key, value) {
17+
if (Prism.util.type(value) === 'RegExp') {
18+
var source = value.source.replace(/\\(.)|\[\^?((?:\\.|[^\\\]])*)\]/g, function (m, g1, g2) {
19+
if (g1) {
20+
if (g1 === '(') {
21+
// replace all '(' characters outside character sets
22+
return '[([]';
23+
}
24+
if (g1 === ')') {
25+
// replace all ')' characters outside character sets
26+
return '[)\\]]';
27+
}
28+
}
29+
if (g2) {
30+
var prefix = m[1] === '^' ? '[^' : '[';
31+
return prefix + g2.replace(/\\(.)|[()]/g, function (m, g1) {
32+
if (m === '(' || g1 === '(') {
33+
// replace all '(' characters inside character sets
34+
return '([';
35+
}
36+
if (m === ')' || g1 === ')') {
37+
// replace all ')' characters inside character sets
38+
return ')\\]';
39+
}
40+
return m;
41+
}) + ']';
42+
}
43+
return m;
44+
});
45+
46+
this[key] = RegExp(source, value.flags);
47+
}
48+
});
49+
50+
Prism.languages.insertBefore('racket', 'string', {
51+
'lang': {
52+
pattern: /^#lang.+/m,
53+
greedy: true,
54+
alias: 'keyword'
55+
}
56+
});
57+
58+
Prism.languages.rkt = Prism.languages.racket;

components/prism-racket.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-racket.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<h2>Full example</h2>
2+
<pre><code>; Source: https://github.com/mbutterick/pollen/blob/master/pollen/private/to-string.rkt
3+
4+
#lang racket/base
5+
(provide (all-defined-out))
6+
7+
(define (to-string x)
8+
(cond
9+
[(string? x) x]
10+
[(or (null? x) (void? x)) ""]
11+
[(or (symbol? x) (number? x) (path? x) (char? x)) (format "~a" x)]
12+
;; special handling for procedures, because if a procedure reaches this func,
13+
;; it usually indicates a failed attempt to use a tag function.
14+
;; meaning, it's more useful to raise an error.
15+
[(procedure? x) (error 'pollen "Can't convert procedure ~a to string" x)]
16+
[else (format "~v" x)]))</code></pre>

plugins/autoloader/prism-autoloader.js

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
],
9898
"qml": "javascript",
9999
"qore": "clike",
100+
"racket": "scheme",
100101
"jsx": [
101102
"markup",
102103
"javascript"
@@ -181,6 +182,7 @@
181182
"pq": "powerquery",
182183
"mscript": "powerquery",
183184
"py": "python",
185+
"rkt": "racket",
184186
"robot": "robotframework",
185187
"rb": "ruby",
186188
"sln": "solution-file",

plugins/autoloader/prism-autoloader.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/show-language/prism-show-language.js

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
"py": "Python",
137137
"q": "Q (kdb+ database)",
138138
"qml": "QML",
139+
"rkt": "Racket",
139140
"jsx": "React JSX",
140141
"tsx": "React TSX",
141142
"renpy": "Ren'py",

0 commit comments

Comments
 (0)