Skip to content

Commit d39e0c1

Browse files
committed
Add Ren'Py code block highlighting
1 parent 5eb994e commit d39e0c1

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

app/src/protyle/render/highlightRender.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const highlightRender = (element: Element, cdn = Constants.PROTYLE_CDN, z
3232
setCodeTheme(cdn);
3333

3434
addScript(`${cdn}/js/highlight.js/highlight.min.js?v=11.11.1`, "protyleHljsScript").then(() => {
35-
addScript(`${cdn}/js/highlight.js/third-languages.js?v=2.0.1`, "protyleHljsThirdScript").then(() => {
35+
addScript(`${cdn}/js/highlight.js/third-languages.js?v=2.0.2`, "protyleHljsThirdScript").then(() => {
3636
codeElements.forEach((block: HTMLElement) => {
3737
if (block.getAttribute("data-render") === "true") {
3838
return;

app/stage/protyle/js/highlight.js/third-languages.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,68 @@ hljs.registerLanguage("mlir",function(){"use strict";return function(e){
155155
className:"symbol",variants:[{begin:"%"+s+"([:#]\\d+)?"},{begin:"\\^"+s},{
156156
begin:"#"+s}]},e.C_NUMBER_MODE]}}}());
157157

158+
// https://www.renpy.org/doc/html/language_basics.html
159+
// Ren'Py script language support.
160+
hljs.registerLanguage("renpy", (hljs) => {
161+
const IDENT = /[A-Za-z_][A-Za-z0-9_]*/;
162+
const RENPY_KEYWORDS = [
163+
"add", "at", "behind", "call", "camera", "centered", "contains",
164+
"default", "define", "drag", "draggroup", "elif", "else", "event",
165+
"expression", "hide", "hotspot", "if", "image", "init", "jump",
166+
"key", "label", "menu", "nvl", "on", "pass", "python", "queue",
167+
"repeat", "return", "scene", "screen", "show", "side", "sound",
168+
"stop", "style", "text", "time", "timer", "transform", "use",
169+
"voice", "while", "with", "window", "zorder"
170+
].join(" ");
171+
const PYTHON_KEYWORDS = [
172+
"and", "as", "assert", "async", "await", "break", "class",
173+
"continue", "def", "del", "except", "False", "finally", "for",
174+
"from", "global", "import", "in", "is", "lambda", "None",
175+
"nonlocal", "not", "or", "raise", "True", "try", "yield"
176+
].join(" ");
177+
178+
return {
179+
name: "Ren'Py",
180+
aliases: ["rpy"],
181+
keywords: {
182+
keyword: `${RENPY_KEYWORDS} ${PYTHON_KEYWORDS}`,
183+
literal: "True False None yes no",
184+
built_in: "Character DynamicCharacter Solid Null Fade Dissolve MoveTransition Pause SetVariable ToggleVariable Show Hide Jump Call Return Function Play Queue Stop"
185+
},
186+
contains: [
187+
hljs.HASH_COMMENT_MODE,
188+
hljs.QUOTE_STRING_MODE,
189+
hljs.APOS_STRING_MODE,
190+
hljs.NUMBER_MODE,
191+
{
192+
className: "meta",
193+
begin: /^\s*\$+/,
194+
end: /$/,
195+
contains: [
196+
hljs.HASH_COMMENT_MODE,
197+
hljs.QUOTE_STRING_MODE,
198+
hljs.APOS_STRING_MODE,
199+
hljs.NUMBER_MODE
200+
]
201+
},
202+
{
203+
className: "section",
204+
begin: /^\s*(label|screen|transform|style|image)\s+/,
205+
end: /:/,
206+
keywords: "label screen transform style image"
207+
},
208+
{
209+
className: "title",
210+
begin: new RegExp(`^\\s*${IDENT.source}(?=\\s+["'])`)
211+
},
212+
{
213+
className: "variable",
214+
begin: /\[[^\]\n]+\]/
215+
}
216+
]
217+
};
218+
});
219+
158220
// https://github.com/siyuan-note/siyuan/pull/15345
159221
hljs.registerLanguage('template', function (hljs) {
160222

0 commit comments

Comments
 (0)