3 pygments.lexers.templates |
3 pygments.lexers.templates |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Lexers for various template engines' markup. |
6 Lexers for various template engines' markup. |
7 |
7 |
8 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 import re |
12 import re |
13 |
13 |
177 (r'\$[a-zA-Z_]\w*(\.\w+)*', Name.Variable), |
177 (r'\$[a-zA-Z_]\w*(\.\w+)*', Name.Variable), |
178 (r'[~!%^&*()+=|\[\]:;,.<>/?@-]', Operator), |
178 (r'[~!%^&*()+=|\[\]:;,.<>/?@-]', Operator), |
179 (r'(true|false|null)\b', Keyword.Constant), |
179 (r'(true|false|null)\b', Keyword.Constant), |
180 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
180 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
181 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
181 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
182 (r'"(\\\\|\\"|[^"])*"', String.Double), |
182 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double), |
183 (r"'(\\\\|\\'|[^'])*'", String.Single), |
183 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single), |
184 (r'[a-zA-Z_]\w*', Name.Attribute) |
184 (r'[a-zA-Z_]\w*', Name.Attribute) |
185 ] |
185 ] |
186 } |
186 } |
187 |
187 |
188 def analyse_text(text): |
188 def analyse_text(text): |
250 ], |
250 ], |
251 'funcparams': [ |
251 'funcparams': [ |
252 (r'\$!?\{?', Punctuation, 'variable'), |
252 (r'\$!?\{?', Punctuation, 'variable'), |
253 (r'\s+', Text), |
253 (r'\s+', Text), |
254 (r'[,:]', Punctuation), |
254 (r'[,:]', Punctuation), |
255 (r'"(\\\\|\\"|[^"])*"', String.Double), |
255 (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double), |
256 (r"'(\\\\|\\'|[^'])*'", String.Single), |
256 (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single), |
257 (r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
257 (r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
258 (r"\b[0-9]+\b", Number), |
258 (r"\b[0-9]+\b", Number), |
259 (r'(true|false|null)\b', Keyword.Constant), |
259 (r'(true|false|null)\b', Keyword.Constant), |
260 (r'\(', Punctuation, '#push'), |
260 (r'\(', Punctuation, '#push'), |
261 (r'\)', Punctuation, '#pop'), |
261 (r'\)', Punctuation, '#pop'), |
336 tokens = { |
336 tokens = { |
337 'root': [ |
337 'root': [ |
338 (r'[^{]+', Other), |
338 (r'[^{]+', Other), |
339 (r'\{\{', Comment.Preproc, 'var'), |
339 (r'\{\{', Comment.Preproc, 'var'), |
340 # jinja/django comments |
340 # jinja/django comments |
341 (r'\{[*#].*?[*#]\}', Comment), |
341 (r'\{#.*?#\}', Comment), |
342 # django comments |
342 # django comments |
343 (r'(\{%)(-?\s*)(comment)(\s*-?)(%\})(.*?)' |
343 (r'(\{%)(-?\s*)(comment)(\s*-?)(%\})(.*?)' |
344 r'(\{%)(-?\s*)(endcomment)(\s*-?)(%\})', |
344 r'(\{%)(-?\s*)(endcomment)(\s*-?)(%\})', |
345 bygroups(Comment.Preproc, Text, Keyword, Text, Comment.Preproc, |
345 bygroups(Comment.Preproc, Text, Keyword, Text, Comment.Preproc, |
346 Comment, Comment.Preproc, Text, Keyword, Text, |
346 Comment, Comment.Preproc, Text, Keyword, Text, |
369 r'with(?:(?:out)?\s*context)?|scoped|ignore\s+missing)\b', |
369 r'with(?:(?:out)?\s*context)?|scoped|ignore\s+missing)\b', |
370 Keyword), |
370 Keyword), |
371 (r'(loop|block|super|forloop)\b', Name.Builtin), |
371 (r'(loop|block|super|forloop)\b', Name.Builtin), |
372 (r'[a-zA-Z_][\w-]*', Name.Variable), |
372 (r'[a-zA-Z_][\w-]*', Name.Variable), |
373 (r'\.\w+', Name.Variable), |
373 (r'\.\w+', Name.Variable), |
374 (r':?"(\\\\|\\"|[^"])*"', String.Double), |
374 (r':?"(\\\\|\\[^\\]|[^"\\])*"', String.Double), |
375 (r":?'(\\\\|\\'|[^'])*'", String.Single), |
375 (r":?'(\\\\|\\[^\\]|[^'\\])*'", String.Single), |
376 (r'([{}()\[\]+\-*/%,:~]|[><=]=?|!=)', Operator), |
376 (r'([{}()\[\]+\-*/%,:~]|[><=]=?|!=)', Operator), |
377 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
377 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
378 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
378 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
379 ], |
379 ], |
380 'var': [ |
380 'var': [ |
535 (r'(?s)(<%doc>)(.*?)(</%doc>)', |
535 (r'(?s)(<%doc>)(.*?)(</%doc>)', |
536 bygroups(Name.Tag, Comment.Multiline, Name.Tag)), |
536 bygroups(Name.Tag, Comment.Multiline, Name.Tag)), |
537 (r'(?s)(<%(?:def|method))(\s*)(.*?)(>)(.*?)(</%\2\s*>)', |
537 (r'(?s)(<%(?:def|method))(\s*)(.*?)(>)(.*?)(</%\2\s*>)', |
538 bygroups(Name.Tag, Text, Name.Function, Name.Tag, |
538 bygroups(Name.Tag, Text, Name.Function, Name.Tag, |
539 using(this), Name.Tag)), |
539 using(this), Name.Tag)), |
540 (r'(?s)(<%\w+)(.*?)(>)(.*?)(</%\2\s*>)', |
540 (r'(?s)(<%(\w+)(.*?)(>))(.*?)(</%\2\s*>)', |
541 bygroups(Name.Tag, Name.Function, Name.Tag, |
541 bygroups(Name.Tag, None, None, None, using(PerlLexer), Name.Tag)), |
542 using(PerlLexer), Name.Tag)), |
|
543 (r'(?s)(<&[^|])(.*?)(,.*?)?(&>)', |
542 (r'(?s)(<&[^|])(.*?)(,.*?)?(&>)', |
544 bygroups(Name.Tag, Name.Function, using(PerlLexer), Name.Tag)), |
543 bygroups(Name.Tag, Name.Function, using(PerlLexer), Name.Tag)), |
545 (r'(?s)(<&\|)(.*?)(,.*?)?(&>)', |
544 (r'(?s)(<&\|)(.*?)(,.*?)?(&>)', |
546 bygroups(Name.Tag, Name.Function, using(PerlLexer), Name.Tag)), |
545 bygroups(Name.Tag, Name.Function, using(PerlLexer), Name.Tag)), |
547 (r'</&>', Name.Tag), |
546 (r'</&>', Name.Tag), |
1404 String, Punctuation)), |
1403 String, Punctuation)), |
1405 # directives: evoque, overlay |
1404 # directives: evoque, overlay |
1406 # see doc for handling first name arg: /directives/evoque/ |
1405 # see doc for handling first name arg: /directives/evoque/ |
1407 # + minor inconsistency: the "name" in e.g. $overlay{name=site_base} |
1406 # + minor inconsistency: the "name" in e.g. $overlay{name=site_base} |
1408 # should be using(PythonLexer), not passed out as String |
1407 # should be using(PythonLexer), not passed out as String |
1409 (r'(\$)(evoque|overlay)(\{(%)?)(\s*[#\w\-"\'.]+[^=,%}]+?)?' |
1408 (r'(\$)(evoque|overlay)(\{(%)?)(\s*[#\w\-"\'.]+)?' |
1410 r'(.*?)((?(4)%)\})', |
1409 r'(.*?)((?(4)%)\})', |
1411 bygroups(Punctuation, Name.Builtin, Punctuation, None, |
1410 bygroups(Punctuation, Name.Builtin, Punctuation, None, |
1412 String, using(PythonLexer), Punctuation)), |
1411 String, using(PythonLexer), Punctuation)), |
1413 # directives: if, for, prefer, test |
1412 # directives: if, for, prefer, test |
1414 (r'(\$)(\w+)(\{(%)?)(.*?)((?(4)%)\})', |
1413 (r'(\$)(\w+)(\{(%)?)(.*?)((?(4)%)\})', |
1428 (r'\]#', Comment.Multiline, '#pop'), |
1427 (r'\]#', Comment.Multiline, '#pop'), |
1429 (r'[\]#]', Comment.Multiline) |
1428 (r'[\]#]', Comment.Multiline) |
1430 ], |
1429 ], |
1431 } |
1430 } |
1432 |
1431 |
|
1432 def analyse_text(text): |
|
1433 """Evoque templates use $evoque, which is unique.""" |
|
1434 if '$evoque' in text: |
|
1435 return 1 |
1433 |
1436 |
1434 class EvoqueHtmlLexer(DelegatingLexer): |
1437 class EvoqueHtmlLexer(DelegatingLexer): |
1435 """ |
1438 """ |
1436 Subclass of the `EvoqueLexer` that highlights unlexed data with the |
1439 Subclass of the `EvoqueLexer` that highlights unlexed data with the |
1437 `HtmlLexer`. |
1440 `HtmlLexer`. |
1444 mimetypes = ['text/html+evoque'] |
1447 mimetypes = ['text/html+evoque'] |
1445 |
1448 |
1446 def __init__(self, **options): |
1449 def __init__(self, **options): |
1447 super().__init__(HtmlLexer, EvoqueLexer, **options) |
1450 super().__init__(HtmlLexer, EvoqueLexer, **options) |
1448 |
1451 |
|
1452 def analyse_text(text): |
|
1453 return EvoqueLexer.analyse_text(text) |
|
1454 |
1449 |
1455 |
1450 class EvoqueXmlLexer(DelegatingLexer): |
1456 class EvoqueXmlLexer(DelegatingLexer): |
1451 """ |
1457 """ |
1452 Subclass of the `EvoqueLexer` that highlights unlexed data with the |
1458 Subclass of the `EvoqueLexer` that highlights unlexed data with the |
1453 `XmlLexer`. |
1459 `XmlLexer`. |
1459 filenames = ['*.xml'] |
1465 filenames = ['*.xml'] |
1460 mimetypes = ['application/xml+evoque'] |
1466 mimetypes = ['application/xml+evoque'] |
1461 |
1467 |
1462 def __init__(self, **options): |
1468 def __init__(self, **options): |
1463 super().__init__(XmlLexer, EvoqueLexer, **options) |
1469 super().__init__(XmlLexer, EvoqueLexer, **options) |
|
1470 |
|
1471 def analyse_text(text): |
|
1472 return EvoqueLexer.analyse_text(text) |
1464 |
1473 |
1465 |
1474 |
1466 class ColdfusionLexer(RegexLexer): |
1475 class ColdfusionLexer(RegexLexer): |
1467 """ |
1476 """ |
1468 Coldfusion statements |
1477 Coldfusion statements |
1724 options['requiredelimiters'] = True |
1733 options['requiredelimiters'] = True |
1725 super().__init__(CssLexer, LassoLexer, **options) |
1734 super().__init__(CssLexer, LassoLexer, **options) |
1726 |
1735 |
1727 def analyse_text(text): |
1736 def analyse_text(text): |
1728 rv = LassoLexer.analyse_text(text) - 0.05 |
1737 rv = LassoLexer.analyse_text(text) - 0.05 |
1729 if re.search(r'\w+:.+?;', text): |
1738 if re.search(r'\w+:[^;]+;', text): |
1730 rv += 0.1 |
1739 rv += 0.1 |
1731 if 'padding:' in text: |
1740 if 'padding:' in text: |
1732 rv += 0.1 |
1741 rv += 0.1 |
1733 return rv |
1742 return rv |
1734 |
1743 |
1823 ], |
1832 ], |
1824 'generic': [ |
1833 'generic': [ |
1825 include('variable'), |
1834 include('variable'), |
1826 |
1835 |
1827 # borrowed from DjangoLexer |
1836 # borrowed from DjangoLexer |
1828 (r':?"(\\\\|\\"|[^"])*"', String.Double), |
1837 (r':?"(\\\\|\\[^\\]|[^"\\])*"', String.Double), |
1829 (r":?'(\\\\|\\'|[^'])*'", String.Single), |
1838 (r":?'(\\\\|\\[^\\]|[^'\\])*'", String.Single), |
1830 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
1839 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
1831 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
1840 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
1832 ] |
1841 ] |
1833 } |
1842 } |
1834 |
1843 |
2136 Keyword), |
2145 Keyword), |
2137 (r'(loop|block|parent)\b', Name.Builtin), |
2146 (r'(loop|block|parent)\b', Name.Builtin), |
2138 (_ident_inner, Name.Variable), |
2147 (_ident_inner, Name.Variable), |
2139 (r'\.' + _ident_inner, Name.Variable), |
2148 (r'\.' + _ident_inner, Name.Variable), |
2140 (r'\.[0-9]+', Number), |
2149 (r'\.[0-9]+', Number), |
2141 (r':?"(\\\\|\\"|[^"])*"', String.Double), |
2150 (r':?"(\\\\|\\[^\\]|[^"\\])*"', String.Double), |
2142 (r":?'(\\\\|\\'|[^'])*'", String.Single), |
2151 (r":?'(\\\\|\\[^\\]|[^'\\])*'", String.Single), |
2143 (r'([{}()\[\]+\-*/,:~%]|\.\.|\?|:|\*\*|\/\/|!=|[><=]=?)', Operator), |
2152 (r'([{}()\[\]+\-*/,:~%]|\.\.|\?|:|\*\*|\/\/|!=|[><=]=?)', Operator), |
2144 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
2153 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
2145 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
2154 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
2146 ], |
2155 ], |
2147 'var': [ |
2156 'var': [ |
2216 (r'\s+(\|\s+)?', Text), |
2225 (r'\s+(\|\s+)?', Text), |
2217 (r'\}\}', Comment.Preproc, '#pop'), |
2226 (r'\}\}', Comment.Preproc, '#pop'), |
2218 |
2227 |
2219 # Literals |
2228 # Literals |
2220 (r':?(true|false)', String.Boolean), |
2229 (r':?(true|false)', String.Boolean), |
2221 (r':?"(\\\\|\\"|[^"])*"', String.Double), |
2230 (r':?"(\\\\|\\[^\\]|[^"\\])*"', String.Double), |
2222 (r":?'(\\\\|\\'|[^'])*'", String.Single), |
2231 (r":?'(\\\\|\\[^\\]|[^'\\])*'", String.Single), |
2223 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
2232 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" |
2224 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
2233 r"0[xX][0-9a-fA-F]+[Ll]?", Number), |
2225 |
2234 |
2226 # Variabletext |
2235 # Variabletext |
2227 (r'[a-zA-Z][\w-]*(\(.*\))?', Name.Variable), |
2236 (r'[a-zA-Z][\w-]*(\(.*\))?', Name.Variable), |