-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
django.js
65 lines (62 loc) · 2.12 KB
/
django.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// @ts-nocheck
import refractorMarkupTemplating from './markup-templating.js'
django.displayName = 'django'
django.aliases = ['jinja2']
/** @type {import('../core.js').Syntax} */
export default function django(Prism) {
Prism.register(refractorMarkupTemplating)
// Django/Jinja2 syntax definition for Prism.js <http://prismjs.com> 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)
}