|
14 | 14 |
|
15 | 15 | import django
|
16 | 16 | import django.template
|
17 |
| -from django.template.base import ( |
18 |
| - Lexer, TextNode, NodeList, Template, |
19 |
| - TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR, |
20 |
| -) |
| 17 | +from django.template.base import Lexer, TextNode, NodeList, Template |
21 | 18 | from django.templatetags.i18n import BlockTranslateNode
|
| 19 | + |
22 | 20 | try:
|
23 | 21 | from django.template.defaulttags import VerbatimNode
|
24 | 22 | except ImportError:
|
25 | 23 | # Django 1.4 didn't have VerbatimNode
|
26 | 24 | VerbatimNode = None
|
27 | 25 |
|
| 26 | +try: |
| 27 | + from django.template.base import TokenType |
| 28 | + |
| 29 | + def _token_name(token_type): |
| 30 | + token_type.name.capitalize() |
| 31 | + |
| 32 | +except ImportError: |
| 33 | + # Django <2.1 uses separate constants for token types |
| 34 | + from django.template.base import ( |
| 35 | + TOKEN_BLOCK, TOKEN_MAPPING, TOKEN_TEXT, TOKEN_VAR |
| 36 | + ) |
| 37 | + |
| 38 | + class TokenType: |
| 39 | + TEXT = TOKEN_TEXT |
| 40 | + VAR = TOKEN_VAR |
| 41 | + BLOCK = TOKEN_BLOCK |
| 42 | + |
| 43 | + def _token_name(token_type): |
| 44 | + return TOKEN_MAPPING[token_type] |
| 45 | + |
28 | 46 |
|
29 | 47 | class DjangoTemplatePluginException(Exception):
|
30 | 48 | """Used for any errors from the plugin itself."""
|
@@ -298,20 +316,20 @@ def lines(self):
|
298 | 316 | if SHOW_PARSING:
|
299 | 317 | print(
|
300 | 318 | "%10s %2d: %r" % (
|
301 |
| - TOKEN_MAPPING[token.token_type], |
| 319 | + _token_name(token.token_type), |
302 | 320 | token.lineno,
|
303 | 321 | token.contents,
|
304 | 322 | )
|
305 | 323 | )
|
306 |
| - if token.token_type == TOKEN_BLOCK: |
| 324 | + if token.token_type == TokenType.BLOCK: |
307 | 325 | if token.contents == "endcomment":
|
308 | 326 | comment = False
|
309 | 327 | continue
|
310 | 328 |
|
311 | 329 | if comment:
|
312 | 330 | continue
|
313 | 331 |
|
314 |
| - if token.token_type == TOKEN_BLOCK: |
| 332 | + if token.token_type == TokenType.BLOCK: |
315 | 333 | if token.contents.startswith("endblock"):
|
316 | 334 | inblock = False
|
317 | 335 | elif token.contents.startswith("block"):
|
@@ -340,10 +358,10 @@ def lines(self):
|
340 | 358 |
|
341 | 359 | source_lines.add(token.lineno)
|
342 | 360 |
|
343 |
| - elif token.token_type == TOKEN_VAR: |
| 361 | + elif token.token_type == TokenType.VAR: |
344 | 362 | source_lines.add(token.lineno)
|
345 | 363 |
|
346 |
| - elif token.token_type == TOKEN_TEXT: |
| 364 | + elif token.token_type == TokenType.TEXT: |
347 | 365 | if extends and not inblock:
|
348 | 366 | continue
|
349 | 367 | # Text nodes often start with newlines, but we don't want to
|
|
0 commit comments