1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
4 local l
= require('lexer')
5 local token
, word_match
= l
.token
, l
.word_match
6 local P
, R
, S
, V
= lpeg
.P
, lpeg
.R
, lpeg
.S
, lpeg
.V
8 local M
= {_NAME
= 'django'}
11 local ws
= token(l
.WHITESPACE
, l
.space^
1)
14 local comment
= token(l
.COMMENT
, '{#' * (l
.any
- l
.newline
- '#}')^
0 *
18 local string = token(l
.STRING
, l
.delimited_range('"', false, true))
21 local keyword
= token(l
.KEYWORD
, word_match
{
22 'as', 'block', 'blocktrans', 'by', 'endblock', 'endblocktrans', 'comment',
23 'endcomment', 'cycle', 'date', 'debug', 'else', 'extends', 'filter',
24 'endfilter', 'firstof', 'for', 'endfor', 'if', 'endif', 'ifchanged',
25 'endifchanged', 'ifnotequal', 'endifnotequal', 'in', 'load', 'not', 'now',
26 'or', 'parsed', 'regroup', 'ssi', 'trans', 'with', 'widthratio'
30 local func
= token(l
.FUNCTION
, word_match
{
31 'add', 'addslashes', 'capfirst', 'center', 'cut', 'date', 'default',
32 'dictsort', 'dictsortreversed', 'divisibleby', 'escape', 'filesizeformat',
33 'first', 'fix_ampersands', 'floatformat', 'get_digit', 'join', 'length',
34 'length_is', 'linebreaks', 'linebreaksbr', 'linenumbers', 'ljust', 'lower',
35 'make_list', 'phone2numeric', 'pluralize', 'pprint', 'random', 'removetags',
36 'rjust', 'slice', 'slugify', 'stringformat', 'striptags', 'time', 'timesince',
37 'title', 'truncatewords', 'unordered_list', 'upper', 'urlencode', 'urlize',
38 'urlizetrunc', 'wordcount', 'wordwrap', 'yesno',
42 local identifier
= token(l
.IDENTIFIER
, l
.word
)
45 local operator
= token(l
.OPERATOR
, S(':,.|'))
51 {'identifier', identifier
},
54 {'operator', operator
},
58 local html
= l
.load('html')
61 local django_start_rule
= token('django_tag', '{' * S('{%'))
62 local django_end_rule
= token('django_tag', S('%}') * '}')
63 l
.embed_lexer(html
, M
, django_start_rule
, django_end_rule
)
64 -- Modify HTML patterns to embed Django.
65 html
._RULES
['comment'] = html
._RULES
['comment'] + comment
68 django_tag
= l
.STYLE_EMBEDDED
71 local _foldsymbols
= html
._foldsymbols
72 _foldsymbols
._patterns
[#_foldsymbols
._patterns
+ 1] = '{[%%{]'
73 _foldsymbols
._patterns
[#_foldsymbols
._patterns
+ 1] = '[%%}]}'
74 _foldsymbols
.django_tag
= {['{{'] = 1, ['}}'] = -1, ['{%'] = 1, ['%}'] = -1}
75 M
._foldsymbols
= _foldsymbols