UNPKG

2.94 kBJavaScriptView Raw
1// Django/Jinja2 syntax definition for Prism.js <http://prismjs.com> syntax highlighter.
2// Mostly it works OK but can paint code incorrectly on complex html/template tag combinations.
3
4var _django_template = {
5 'property': {
6 pattern: /(?:{{|{%)[\s\S]*?(?:%}|}})/g,
7 greedy: true,
8 inside: {
9 'string': {
10 pattern: /("|')(?:\\.|(?!\1)[^\\\r\n])*\1/,
11 greedy: true
12 },
13 'keyword': /\b(?:\||load|verbatim|widthratio|ssi|firstof|for|url|ifchanged|csrf_token|lorem|ifnotequal|autoescape|now|templatetag|debug|cycle|ifequal|regroup|comment|filter|endfilter|if|spaceless|with|extends|block|include|else|empty|endif|endfor|as|endblock|endautoescape|endverbatim|trans|endtrans|[Tt]rue|[Ff]alse|[Nn]one|in|is|static|macro|endmacro|call|endcall|set|endset|raw|endraw)\b/,
14 'operator' : /[-+=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]|\b(?:or|and|not)\b/,
15 'function': /\b(?:_|abs|add|addslashes|attr|batch|callable|capfirst|capitalize|center|count|cut|d|date|default|default_if_none|defined|dictsort|dictsortreversed|divisibleby|e|equalto|escape|escaped|escapejs|even|filesizeformat|first|float|floatformat|force_escape|forceescape|format|get_digit|groupby|indent|int|iriencode|iterable|join|last|length|length_is|linebreaks|linebreaksbr|linenumbers|list|ljust|lower|make_list|map|mapping|number|odd|phone2numeric|pluralize|pprint|random|reject|rejectattr|removetags|replace|reverse|rjust|round|safe|safeseq|sameas|select|selectattr|sequence|slice|slugify|sort|string|stringformat|striptags|sum|time|timesince|timeuntil|title|trim|truncate|truncatechars|truncatechars_html|truncatewords|truncatewords_html|undefined|unordered_list|upper|urlencode|urlize|urlizetrunc|wordcount|wordwrap|xmlattr|yesno)\b/,
16 'important': /\b-?\d+(?:\.\d+)?\b/,
17 'variable': /\b\w+?\b/,
18 'punctuation' : /[[\];(),.:]/
19 }
20 }
21};
22
23Prism.languages.django = Prism.languages.extend('markup', {'comment': /(?:<!--|{#)[\s\S]*?(?:#}|-->)/});
24// Updated html tag pattern to allow template tags inside html tags
25Prism.languages.django.tag.pattern = /<\/?(?!\d)[^\s>\/=$<]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^>=]+))?)*\s*\/?>/i;
26Prism.languages.insertBefore('django', 'entity', _django_template);
27Prism.languages.insertBefore('inside', 'tag', _django_template, Prism.languages.django.tag);
28
29if (Prism.languages.javascript) {
30 // Combine js code and template tags painting inside <script> blocks
31 Prism.languages.insertBefore('inside', 'string', _django_template, Prism.languages.django.script);
32 Prism.languages.django.script.inside.string.inside = _django_template;
33}
34if (Prism.languages.css) {
35 // Combine css code and template tags painting inside <style> blocks
36 Prism.languages.insertBefore('inside', 'atrule', {'tag': _django_template.property}, Prism.languages.django.style);
37 Prism.languages.django.style.inside.string.inside = _django_template;
38}
39
40// Add an Jinja2 alias
41Prism.languages.jinja2 = Prism.languages.django;