vis: implement :set cursorline
[vis.git] / lexers / django.lua
blob13a53417fffa251cd77d4a4bbc9db20da43e9845
1 -- Copyright 2006-2015 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- Django LPeg lexer.
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'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local comment = token(l.COMMENT, '{#' * (l.any - l.newline - '#}')^0 *
15 P('#}')^-1)
17 -- Strings.
18 local string = token(l.STRING, l.delimited_range('"', false, true))
20 -- Keywords.
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'
29 -- Functions.
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',
41 -- Identifiers.
42 local identifier = token(l.IDENTIFIER, l.word)
44 -- Operators.
45 local operator = token(l.OPERATOR, S(':,.|'))
47 M._rules = {
48 {'whitespace', ws},
49 {'keyword', keyword},
50 {'function', func},
51 {'identifier', identifier},
52 {'string', string},
53 {'comment', comment},
54 {'operator', operator},
57 -- Embedded in HTML.
58 local html = l.load('html')
60 -- Embedded Django.
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
67 M._tokenstyles = {
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
77 return M