/
jinja.el
213 lines (191 loc) · 8.2 KB
/
jinja.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
;;; jinja.el --- Jinja mode highlighting
;;
;; Author: Georg Brandl
;; Copyright: (c) 2009 by the Jinja Team
;; Last modified: 2008-05-22 23:04 by gbr
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Mostly ripped off django-mode by Lennart Borgman.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(defconst jinja-font-lock-keywords
(list
; (cons (rx "{% comment %}" (submatch (0+ anything))
; "{% endcomment %}") (list 1 font-lock-comment-face))
'("{# ?\\(.*?\\) ?#}" . (1 font-lock-comment-face))
'("{%-?\\|-?%}\\|{{\\|}}" . font-lock-preprocessor-face)
'("{#\\|#}" . font-lock-comment-delimiter-face)
;; first word in a block is a command
'("{%-?[ \t\n]*\\([a-zA-Z_]+\\)" . (1 font-lock-keyword-face))
;; variables
'("\\({{ ?\\)\\([^|]*?\\)\\(|.*?\\)? ?}}" . (1 font-lock-variable-name-face))
;; keywords and builtins
(cons (rx word-start
(or "in" "as" "recursive" "not" "and" "or" "if" "else"
"import" "with" "without" "context")
word-end)
font-lock-keyword-face)
(cons (rx word-start
(or "true" "false" "none" "loop" "self" "super")
word-end)
font-lock-builtin-face)
;; tests
'("\\(is\\)[ \t]*\\(not\\)[ \t]*\\([a-zA-Z_]+\\)"
(1 font-lock-keyword-face) (2 font-lock-keyword-face)
(3 font-lock-function-name-face))
;; builtin filters
(cons (rx
"|" (* space)
(submatch
(or "abs" "batch" "capitalize" "capture" "center" "count" "default"
"dformat" "dictsort" "e" "escape" "filesizeformat" "first"
"float" "format" "getattribute" "getitem" "groupby" "indent"
"int" "join" "jsonencode" "last" "length" "lower" "markdown"
"pprint" "random" "replace" "reverse" "round" "rst" "slice"
"sort" "string" "striptags" "sum" "textile" "title" "trim"
"truncate" "upper" "urlencode" "urlize" "wordcount" "wordwrap"
"xmlattr")))
(list 1 font-lock-builtin-face))
)
"Minimal highlighting expressions for Jinja mode")
(define-derived-mode jinja-mode nil "Jinja"
"Simple Jinja mode for use with `mumamo-mode'.
This mode only provides syntax highlighting."
;;(set (make-local-variable 'comment-start) "{#")
;;(set (make-local-variable 'comment-end) "#}")
(setq font-lock-defaults '(jinja-font-lock-keywords)))
;; mumamo stuff
(when (require 'mumamo nil t)
(defun mumamo-chunk-jinja3(pos min max)
"Find {# ... #}. Return range and `jinja-mode'.
See `mumamo-find-possible-chunk' for POS, MIN and MAX."
(mumamo-find-possible-chunk pos min max
'mumamo-search-bw-exc-start-jinja3
'mumamo-search-bw-exc-end-jinja3
'mumamo-search-fw-exc-start-jinja3
'mumamo-search-fw-exc-end-jinja3))
(defun mumamo-chunk-jinja2(pos min max)
"Find {{ ... }}. Return range and `jinja-mode'.
See `mumamo-find-possible-chunk' for POS, MIN and MAX."
(mumamo-find-possible-chunk pos min max
'mumamo-search-bw-exc-start-jinja2
'mumamo-search-bw-exc-end-jinja2
'mumamo-search-fw-exc-start-jinja2
'mumamo-search-fw-exc-end-jinja2))
(defun mumamo-chunk-jinja (pos min max)
"Find {% ... %}. Return range and `jinja-mode'.
See `mumamo-find-possible-chunk' for POS, MIN and MAX."
(mumamo-find-possible-chunk pos min max
'mumamo-search-bw-exc-start-jinja
'mumamo-search-bw-exc-end-jinja
'mumamo-search-fw-exc-start-jinja
'mumamo-search-fw-exc-end-jinja))
(defun mumamo-search-bw-exc-start-jinja (pos min)
"Helper for `mumamo-chunk-jinja'.
POS is where to start search and MIN is where to stop."
(let ((exc-start (mumamo-chunk-start-bw-str-inc pos min "{%")))
(and exc-start
(<= exc-start pos)
(cons exc-start 'jinja-mode))))
(defun mumamo-search-bw-exc-start-jinja2(pos min)
"Helper for `mumamo-chunk-jinja2'.
POS is where to start search and MIN is where to stop."
(let ((exc-start (mumamo-chunk-start-bw-str-inc pos min "{{")))
(and exc-start
(<= exc-start pos)
(cons exc-start 'jinja-mode))))
(defun mumamo-search-bw-exc-start-jinja3(pos min)
"Helper for `mumamo-chunk-jinja3'.
POS is where to start search and MIN is where to stop."
(let ((exc-start (mumamo-chunk-start-bw-str-inc pos min "{#")))
(and exc-start
(<= exc-start pos)
(cons exc-start 'jinja-mode))))
(defun mumamo-search-bw-exc-end-jinja (pos min)
"Helper for `mumamo-chunk-jinja'.
POS is where to start search and MIN is where to stop."
(mumamo-chunk-end-bw-str-inc pos min "%}"))
(defun mumamo-search-bw-exc-end-jinja2(pos min)
"Helper for `mumamo-chunk-jinja2'.
POS is where to start search and MIN is where to stop."
(mumamo-chunk-end-bw-str-inc pos min "}}"))
(defun mumamo-search-bw-exc-end-jinja3(pos min)
"Helper for `mumamo-chunk-jinja3'.
POS is where to start search and MIN is where to stop."
(mumamo-chunk-end-bw-str-inc pos min "#}"))
(defun mumamo-search-fw-exc-start-jinja (pos max)
"Helper for `mumamo-chunk-jinja'.
POS is where to start search and MAX is where to stop."
(mumamo-chunk-start-fw-str-inc pos max "{%"))
(defun mumamo-search-fw-exc-start-jinja2(pos max)
"Helper for `mumamo-chunk-jinja2'.
POS is where to start search and MAX is where to stop."
(mumamo-chunk-start-fw-str-inc pos max "{{"))
(defun mumamo-search-fw-exc-start-jinja3(pos max)
"Helper for `mumamo-chunk-jinja3'.
POS is where to start search and MAX is where to stop."
(mumamo-chunk-start-fw-str-inc pos max "{#"))
(defun mumamo-search-fw-exc-end-jinja (pos max)
"Helper for `mumamo-chunk-jinja'.
POS is where to start search and MAX is where to stop."
(mumamo-chunk-end-fw-str-inc pos max "%}"))
(defun mumamo-search-fw-exc-end-jinja2(pos max)
"Helper for `mumamo-chunk-jinja2'.
POS is where to start search and MAX is where to stop."
(mumamo-chunk-end-fw-str-inc pos max "}}"))
(defun mumamo-search-fw-exc-end-jinja3(pos max)
"Helper for `mumamo-chunk-jinja3'.
POS is where to start search and MAX is where to stop."
(mumamo-chunk-end-fw-str-inc pos max "#}"))
;;;###autoload
(define-mumamo-multi-major-mode jinja-html-mumamo
"Turn on multiple major modes for Jinja with main mode `html-mode'.
This also covers inlined style and javascript."
("Jinja HTML Family" html-mode
(mumamo-chunk-jinja
mumamo-chunk-jinja2
mumamo-chunk-jinja3
mumamo-chunk-inlined-style
mumamo-chunk-inlined-script
mumamo-chunk-style=
mumamo-chunk-onjs=
)))
;;;###autoload
(define-mumamo-multi-major-mode jinja-nxhtml-mumamo
"Turn on multiple major modes for Jinja with main mode `nxhtml-mode'.
This also covers inlined style and javascript."
("Jinja nXhtml Family" nxhtml-mode
(mumamo-chunk-jinja
mumamo-chunk-jinja2
mumamo-chunk-jinja3
mumamo-chunk-inlined-style
mumamo-chunk-inlined-script
mumamo-chunk-style=
mumamo-chunk-onjs=
)))
)
(provide 'jinja)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; jinja.el ends here