# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is Komodo code.
# 
# The Initial Developer of the Original Code is ActiveState Software Inc.
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
# ActiveState Software Inc. All Rights Reserved.
# 
# Contributor(s):
#   ActiveState Software Inc
# 
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
# 
# ***** END LICENSE BLOCK *****

# Lexer for Django -- a python-based (sort of) template language


# How to transition from HTML to Django

# {% tag %}

# {{ variables/values (in Django) }}
# -- could occur in attribute strings as well

family tpl
sublanguage Django

start_style TPL_DEFAULT
end_style TPL_VARIABLE

keyword_style TPL_IDENTIFIER => TPL_WORD

keywords [and
as
block
by
comment
cycle
debug
else
extends
filter
firstof
for
if
ifchanged
ifequal
ifnotequal
in
'include'
load
not
now
or
regroup
reversed
spaceless
ssi
templatetag
url
widthratio

# end tags

endblock
endcomment
endfilter
endfor
endif
endifchanged
endifequal
endifnotequal
endspaceless

# Escape keywords

openblock
closeblock
openvariable
closevariable
openbrace
closebrace

# These are default filter names in django

add
addslashes
capfirst
center
cut
date
default
default_if_none
dictsort
dictsortreversed
divisibleby
escape
filesizeformat
first
fix_ampersands
floatformat
get_digit
join
length
length_is
linebreaks
linebreaksbr
linenumbers
ljust
lower
make_list
phone2numeric
pluralize
pprint
random
removetags
rjust
slice
slugify
stringformat
striptags
time
timesince
timeuntil
title
truncatewords
truncatewords_html
unordered_list
upper
urlencode
urlize
urlizetrunc
wordcount
wordwrap
yesno
]

pattern NMSTART = '_\w\x80-\xff'   # inside cset
pattern CS = '$NMSTART\d'   # inside cset
pattern WS = '\s\t\r\n'           # inside cset
pattern OP = '|\.{}%:,'

initial IN_TPL_DEFAULT

state IN_TPL_DEFAULT:

/[$WS]+/  : #stay
'"' : paint(upto, TPL_DEFAULT), => IN_TPL_DSTRING
'\'' : paint(upto, TPL_DEFAULT), => IN_TPL_SSTRING

# Go process a number, try to do it in regexes
# octal and hex first
/0[Xx][0-9a-fA-F]+/ : paint(upto, TPL_DEFAULT), paint(include, TPL_NUMBER)
/[-+]?0[0-7]*/ : paint(upto, TPL_DEFAULT), paint(include, TPL_NUMBER)
/[1-9]/ : paint(upto, TPL_DEFAULT), => IN_TPL_NUMBER_1
/\.\d+/ : paint(upto, TPL_DEFAULT), => IN_TPL_NUMBER_2
/\.(?=[$NMSTART])/ : paint(upto, TPL_DEFAULT), paint(include, TPL_OPERATOR), \
=> IN_TPL_DEFAULT_OR_IDENTIFIER_1
'.' : paint(upto, TPL_DEFAULT), => IN_TPL_NUMBER_2b
/ssi(?![$CS])/ : paint(upto, TPL_DEFAULT), paint(include, TPL_WORD), => IN_TPL_SSI_1 # Allow unquoted path as an argument

/[$NMSTART][$CS]*/ : paint(upto, TPL_DEFAULT), paint(include, TPL_IDENTIFIER)

# Now a bunch of operators

# First track braces

'{{' : paint(upto, TPL_DEFAULT), paint(include, TPL_OPERATOR), \
	spush_check(IN_TPL_DEFAULT), => IN_TPL_DEFAULT

'}}' : paint(upto, TPL_DEFAULT), paint(include, TPL_OPERATOR), spop_check, => IN_TPL_DEFAULT

/[$OP]/ : paint(upto, TPL_DEFAULT), paint(include, TPL_OPERATOR) #stay

################# Post-dot identifiers

# This always matches because we arrive here via lookahead.
state IN_TPL_DEFAULT_OR_IDENTIFIER_1:
/./ : no_keyword, paint(include, TPL_IDENTIFIER), => IN_TPL_DEFAULT_OR_IDENTIFIER_2

state IN_TPL_DEFAULT_OR_IDENTIFIER_2:
/[$CS]/ : #stay
/[^$CS]/ : paint(upto, TPL_IDENTIFIER), redo, no_keyword, => IN_TPL_DEFAULT

################ Strings ################

# We need counting to handle ${...} expressions, like in Ruby

state IN_TPL_DSTRING:
'"' : paint(include, TPL_STRING), => IN_TPL_DEFAULT
/\\./ : #stay, Don't miscount backslash at end
/$/ : paint(upto, TPL_STRING), => IN_TPL_DEFAULT
'\r' : paint(upto, TPL_STRING), => IN_TPL_DEFAULT

state IN_TPL_SSTRING:
'\'' : paint(include, TPL_STRING), => IN_TPL_DEFAULT
/\\./ : #stay, Don't miscount backslash at end
/$/ : paint(upto, TPL_STRING), => IN_TPL_DEFAULT
'\r' : paint(upto, TPL_STRING), => IN_TPL_DEFAULT

################ Numbers ################

include "tpl_number.udl"

################ Pathnames (SSI arg) ################

state IN_TPL_SSI_1:
/[$WS]+/  : #stay
/$/ : paint(upto, TPL_DEFAULT), => IN_TPL_DEFAULT #Give up
'"' : paint(upto, TPL_DEFAULT), => IN_TPL_DSTRING
'\'' : paint(upto, TPL_DEFAULT), => IN_TPL_SSTRING
/[%\}]/ : paint(upto, TPL_DEFAULT), paint(include, TPL_OPERATOR), => IN_TPL_DEFAULT
/./ : paint(upto, TPL_DEFAULT), => IN_TPL_SSI_2

state IN_TPL_SSI_2:
/$/ : paint(upto, TPL_STRING) => IN_TPL_DEFAULT 
/\\./ : #stay
/[$WS]+/ paint(upto, TPL_STRING),  => IN_TPL_DEFAULT

################ Folding Directives ################

fold "comment" TPL_COMMENT +
fold "filter" TPL_WORD +
fold "for" TPL_WORD +
fold "if" TPL_WORD +
fold "ifchanged" TPL_WORD +
fold "ifequal" TPL_WORD +
fold "ifnotequal" TPL_WORD +
fold "spaceless" TPL_WORD +

fold "endcomment" TPL_COMMENT -
fold "endfilter" TPL_WORD -
fold "endfor" TPL_WORD -
fold "endif" TPL_WORD -
fold "endifchanged" TPL_WORD -
fold "endifequal" TPL_WORD -
fold "endifnotequal" TPL_WORD -
fold "endspaceless" TPL_WORD -
