Skip to content

Commit a436dc3

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

2 files changed

Lines changed: 99 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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,104 @@ 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+
// https://github.com/renpy/vscode-language-renpy
160+
// Ren'Py script language support.
161+
hljs.registerLanguage("renpy", (hljs) => {
162+
const IDENT = /[A-Za-z_][A-Za-z0-9_]*/;
163+
const INTERPOLATION = {
164+
className: "subst",
165+
begin: /\[[^\]\n]+\]/
166+
};
167+
const TEXT_TAG = {
168+
className: "subst",
169+
begin: /\{\/?[A-Za-z#][^{}\n]*\}/
170+
};
171+
const STRING_CONTAINS = [
172+
hljs.BACKSLASH_ESCAPE,
173+
INTERPOLATION,
174+
TEXT_TAG
175+
];
176+
const QUOTE_STRING_MODE = {
177+
className: "string",
178+
begin: /"/,
179+
end: /"/,
180+
contains: STRING_CONTAINS
181+
};
182+
const APOS_STRING_MODE = {
183+
className: "string",
184+
begin: /'/,
185+
end: /'/,
186+
contains: STRING_CONTAINS
187+
};
188+
const BACKTICK_STRING_MODE = {
189+
className: "string",
190+
begin: /`/,
191+
end: /`/,
192+
contains: STRING_CONTAINS
193+
};
194+
const RENPY_KEYWORDS = [
195+
"add", "at", "behind", "call", "camera", "centered", "contains",
196+
"default", "define", "drag", "draggroup", "early", "elif", "else",
197+
"event", "expression", "extend", "for", "from", "hide", "hotspot",
198+
"if", "image", "init", "jump", "key", "label", "layeredimage",
199+
"menu", "nvl", "on", "onlayer", "pass", "pause", "play", "python",
200+
"queue", "repeat", "return", "scene", "screen", "set", "show",
201+
"side", "sound", "spacing", "stop", "style", "style_prefix", "text",
202+
"textbutton", "time", "timer", "transform", "translate", "use",
203+
"voice", "while", "with", "window", "zorder"
204+
].join(" ");
205+
const PYTHON_KEYWORDS = [
206+
"and", "as", "assert", "async", "await", "break", "class",
207+
"continue", "def", "del", "except", "False", "finally", "for",
208+
"from", "global", "import", "in", "is", "lambda", "None",
209+
"nonlocal", "not", "or", "raise", "True", "try", "yield"
210+
].join(" ");
211+
212+
return {
213+
name: "Ren'Py",
214+
aliases: ["rpy"],
215+
keywords: {
216+
keyword: `${RENPY_KEYWORDS} ${PYTHON_KEYWORDS}`,
217+
literal: "True False None yes no",
218+
built_in: "Character DynamicCharacter Solid Null Fade Dissolve MoveTransition Pause SetVariable ToggleVariable Show Hide Jump Call Return Function Play Queue Stop"
219+
},
220+
contains: [
221+
hljs.HASH_COMMENT_MODE,
222+
QUOTE_STRING_MODE,
223+
APOS_STRING_MODE,
224+
BACKTICK_STRING_MODE,
225+
hljs.NUMBER_MODE,
226+
{
227+
className: "meta",
228+
begin: /^\s*\$+/,
229+
end: /$/,
230+
contains: [
231+
hljs.HASH_COMMENT_MODE,
232+
QUOTE_STRING_MODE,
233+
APOS_STRING_MODE,
234+
BACKTICK_STRING_MODE,
235+
hljs.NUMBER_MODE
236+
]
237+
},
238+
{
239+
className: "section",
240+
begin: /^\s*(label|screen|transform|style|image|layeredimage|translate)\s+/,
241+
end: /:/,
242+
keywords: "label screen transform style image layeredimage translate"
243+
},
244+
{
245+
className: "title",
246+
begin: new RegExp(`^\\s*${IDENT.source}(?=\\s+["'\`])`)
247+
},
248+
{
249+
className: "variable",
250+
begin: /\[[^\]\n]+\]/
251+
}
252+
]
253+
};
254+
});
255+
158256
// https://github.com/siyuan-note/siyuan/pull/15345
159257
hljs.registerLanguage('template', function (hljs) {
160258

0 commit comments

Comments
 (0)