Skip to content

Commit

Permalink
Added support for Racket (#2315)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Apr 27, 2020
1 parent e27e65a commit 053016e
Show file tree
Hide file tree
Showing 21 changed files with 566 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,12 @@
"title": "R",
"owner": "Golmote"
},
"racket": {
"title": "Racket",
"require": "scheme",
"alias": "rkt",
"owner": "RunDevelopment"
},
"jsx": {
"title": "React JSX",
"require": ["markup", "javascript"],
Expand Down
58 changes: 58 additions & 0 deletions components/prism-racket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Prism.languages.racket = Prism.languages.extend('scheme', {});

// Add brackets to racket
// The basic idea here is to go through all pattens of Scheme and replace all occurrences of "(" with the union of "("
// and "["; Similar for ")". This is a bit tricky because "(" can be escaped or inside a character set. Both cases
// have to be handled differently and, of course, we don't want to destroy groups, so we can only replace literal "("
// and ")".
// To do this, we use a regular expression which will parse any JS regular expression. It works because regexes are
// matches from left to right and already matched text cannot be matched again. We use this to first capture all
// escaped characters (not really, we don't get escape sequences but we don't need them). Because we already captured
// all escaped characters, we know that any "[" character is the start of a character set, so we match that character
// set whole.
// With the regex parsed, we only have to replace all escaped "(" (they cannot be unescaped outside of character sets)
// with /[([]/ and replace all "(" inside character sets.
// Note: This method does not work for "(" that are escaped like this /\x28/ or this /\u0028/.
Prism.languages.DFS(Prism.languages.racket, function (key, value) {
if (Prism.util.type(value) === 'RegExp') {
var source = value.source.replace(/\\(.)|\[\^?((?:\\.|[^\\\]])*)\]/g, function (m, g1, g2) {
if (g1) {
if (g1 === '(') {
// replace all '(' characters outside character sets
return '[([]';
}
if (g1 === ')') {
// replace all ')' characters outside character sets
return '[)\\]]';
}
}
if (g2) {
var prefix = m[1] === '^' ? '[^' : '[';
return prefix + g2.replace(/\\(.)|[()]/g, function (m, g1) {
if (m === '(' || g1 === '(') {
// replace all '(' characters inside character sets
return '([';
}
if (m === ')' || g1 === ')') {
// replace all ')' characters inside character sets
return ')\\]';
}
return m;
}) + ']';
}
return m;
});

this[key] = RegExp(source, value.flags);
}
});

Prism.languages.insertBefore('racket', 'string', {
'lang': {
pattern: /^#lang.+/m,
greedy: true,
alias: 'keyword'
}
});

Prism.languages.rkt = Prism.languages.racket;
1 change: 1 addition & 0 deletions components/prism-racket.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions examples/prism-racket.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Full example</h2>
<pre><code>; Source: https://github.com/mbutterick/pollen/blob/master/pollen/private/to-string.rkt

#lang racket/base
(provide (all-defined-out))

(define (to-string x)
(cond
[(string? x) x]
[(or (null? x) (void? x)) ""]
[(or (symbol? x) (number? x) (path? x) (char? x)) (format "~a" x)]
;; special handling for procedures, because if a procedure reaches this func,
;; it usually indicates a failed attempt to use a tag function.
;; meaning, it's more useful to raise an error.
[(procedure? x) (error 'pollen "Can't convert procedure ~a to string" x)]
[else (format "~v" x)]))</code></pre>
2 changes: 2 additions & 0 deletions plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
],
"qml": "javascript",
"qore": "clike",
"racket": "scheme",
"jsx": [
"markup",
"javascript"
Expand Down Expand Up @@ -181,6 +182,7 @@
"pq": "powerquery",
"mscript": "powerquery",
"py": "python",
"rkt": "racket",
"robot": "robotframework",
"rb": "ruby",
"sln": "solution-file",
Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"py": "Python",
"q": "Q (kdb+ database)",
"qml": "QML",
"rkt": "Racket",
"jsx": "React JSX",
"tsx": "React TSX",
"renpy": "Ren'py",
Expand Down
Loading

0 comments on commit 053016e

Please sign in to comment.