-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Django/Jinja2 language definition (#1085)
- Loading branch information
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 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. | ||
|
||
var _django_template = { | ||
'property': { | ||
pattern: /(?:{{|{%)[\w\W]*?(?:%}|}})/g, | ||
greedy: true, | ||
inside: { | ||
'string': { | ||
pattern: /("|')(?:\\\\|\\?[^\\\r\n])*?\1/, | ||
greedy: true | ||
}, | ||
'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/, | ||
'operator' : /[-+=]=?|!=|\*\*?=?|\/\/?=?|<[<=>]?|>[=>]?|[&|^~]|\b(?:or|and|not)\b/, | ||
'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/, | ||
'important': /\b-?\d+(?:\.\d+)?\b/, | ||
'variable': /\b\w+?\b/, | ||
'punctuation' : /[[\];(),.:]/ | ||
} | ||
}, | ||
}; | ||
|
||
Prism.languages.django = Prism.languages.extend('markup', {'comment': /(?:<!--|{#)[\w\W]*?(?:#}|-->)/}); | ||
// Updated html tag pattern to allow template tags inside html tags | ||
Prism.languages.django.tag.pattern = /<\/?(?!\d)[^\s>\/=$<]+(?:\s+[^\s>\/=]+(?:=(?:("|')(?:\\\1|\\?(?!\1)[\w\W])*\1|[^>=]+))?)*\s*\/?>/i; | ||
Prism.languages.insertBefore('django', 'entity', _django_template); | ||
Prism.languages.insertBefore('inside', 'tag', _django_template, Prism.languages.django.tag); | ||
|
||
if (Prism.languages.javascript) { | ||
// Combine js code and template tags painting inside <script> blocks | ||
Prism.languages.insertBefore('inside', 'string', _django_template, Prism.languages.django.script); | ||
Prism.languages.django.script.inside.string.inside = _django_template; | ||
} | ||
if (Prism.languages.css) { | ||
// Combine css code and template tags painting inside <style> blocks | ||
Prism.languages.insertBefore('inside', 'atrule', {'tag': _django_template.property}, Prism.languages.django.style); | ||
Prism.languages.django.style.inside.string.inside = _django_template; | ||
} | ||
|
||
// Add an Jinja2 alias | ||
Prism.languages.jinja2 = Prism.languages.django; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<h1>Django/Jinja2</h1> | ||
<p>To use this language, add the class "language-django" or "language-jinja2".</p> | ||
|
||
<h2>Comment</h2> | ||
<pre><code>{# This is a comment #}</code></pre> | ||
|
||
<h2>Variable</h2> | ||
<pre><code>{{ some_variable }}</code></pre> | ||
|
||
<h2>Template Tag</h2> | ||
<pre><code>{% if some_condition %} | ||
Conditional block | ||
{% endif %} | ||
</code></pre> | ||
|
||
<h2>Full Example</h2> | ||
<pre><code>{# This a Django template example #} | ||
{% extends "base_generic.html" %} | ||
|
||
{% block title %}{{ section.title }}{% endblock %} | ||
|
||
{% block content %} | ||
<h1>{{ section.title }}</h1> | ||
|
||
{% for story in story_list %} | ||
<h2> | ||
<a href="{{ story.get_absolute_url }}"> | ||
{{ story.headline|upper }} | ||
</a> | ||
</h2> | ||
<p>{{ story.tease|truncatewords:"100" }}</p> | ||
{% endfor %} | ||
{% endblock %} | ||
</code></pre> |