From a98b5ea6fe8e8cc98a55e0fd793e6e8660ea31c1 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Thu, 30 Dec 2021 19:47:21 +0100 Subject: chore: add prismjs for syntax highlighting --- public/prism/prism-django.js | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 public/prism/prism-django.js (limited to 'public/prism/prism-django.js') diff --git a/public/prism/prism-django.js b/public/prism/prism-django.js new file mode 100644 index 0000000..915b0a1 --- /dev/null +++ b/public/prism/prism-django.js @@ -0,0 +1,58 @@ +// Django/Jinja2 syntax definition for Prism.js syntax highlighter. +// Mostly it works OK but can paint code incorrectly on complex html/template tag combinations. + +(function (Prism) { + Prism.languages.django = { + comment: /^\{#[\s\S]*?#\}$/, + tag: { + pattern: /(^\{%[+-]?\s*)\w+/, + lookbehind: true, + alias: 'keyword', + }, + delimiter: { + pattern: /^\{[{%][+-]?|[+-]?[}%]\}$/, + alias: 'punctuation', + }, + string: { + pattern: /("|')(?:\\.|(?!\1)[^\\\r\n])*\1/, + greedy: true, + }, + filter: { + pattern: /(\|)\w+/, + lookbehind: true, + alias: 'function', + }, + test: { + pattern: /(\bis\s+(?:not\s+)?)(?!not\b)\w+/, + lookbehind: true, + alias: 'function', + }, + function: /\b[a-z_]\w+(?=\s*\()/i, + keyword: + /\b(?:and|as|by|else|for|if|import|in|is|loop|not|or|recursive|with|without)\b/, + operator: /[-+%=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]/, + number: /\b\d+(?:\.\d+)?\b/, + boolean: /[Ff]alse|[Nn]one|[Tt]rue/, + variable: /\b\w+\b/, + punctuation: /[{}[\](),.:;]/, + }; + + var pattern = /\{\{[\s\S]*?\}\}|\{%[\s\S]*?%\}|\{#[\s\S]*?#\}/g; + var markupTemplating = Prism.languages['markup-templating']; + + Prism.hooks.add('before-tokenize', function (env) { + markupTemplating.buildPlaceholders(env, 'django', pattern); + }); + Prism.hooks.add('after-tokenize', function (env) { + markupTemplating.tokenizePlaceholders(env, 'django'); + }); + + // Add an Jinja2 alias + Prism.languages.jinja2 = Prism.languages.django; + Prism.hooks.add('before-tokenize', function (env) { + markupTemplating.buildPlaceholders(env, 'jinja2', pattern); + }); + Prism.hooks.add('after-tokenize', function (env) { + markupTemplating.tokenizePlaceholders(env, 'jinja2'); + }); +})(Prism); -- cgit v1.2.3