ThirdParty/Pygments/pygments/lexers/templates.py

changeset 808
8f85926125ef
parent 684
2f29a0b6e1c7
child 1705
b0fbc9300f2b
equal deleted inserted replaced
805:83ca4d1ff648 808:8f85926125ef
11 11
12 import re 12 import re
13 13
14 from pygments.lexers.web import \ 14 from pygments.lexers.web import \
15 PhpLexer, HtmlLexer, XmlLexer, JavascriptLexer, CssLexer 15 PhpLexer, HtmlLexer, XmlLexer, JavascriptLexer, CssLexer
16 from pygments.lexers.agile import PythonLexer 16 from pygments.lexers.agile import PythonLexer, PerlLexer
17 from pygments.lexers.compiled import JavaLexer 17 from pygments.lexers.compiled import JavaLexer
18 from pygments.lexer import Lexer, DelegatingLexer, RegexLexer, bygroups, \ 18 from pygments.lexer import Lexer, DelegatingLexer, RegexLexer, bygroups, \
19 include, using, this 19 include, using, this
20 from pygments.token import Error, Punctuation, \ 20 from pygments.token import Error, Punctuation, \
21 Text, Comment, Operator, Keyword, Name, String, Number, Other, Token 21 Text, Comment, Operator, Keyword, Name, String, Number, Other, Token
28 'CssSmartyLexer', 'JavascriptSmartyLexer', 'DjangoLexer', 28 'CssSmartyLexer', 'JavascriptSmartyLexer', 'DjangoLexer',
29 'HtmlDjangoLexer', 'CssDjangoLexer', 'XmlDjangoLexer', 29 'HtmlDjangoLexer', 'CssDjangoLexer', 'XmlDjangoLexer',
30 'JavascriptDjangoLexer', 'GenshiLexer', 'HtmlGenshiLexer', 30 'JavascriptDjangoLexer', 'GenshiLexer', 'HtmlGenshiLexer',
31 'GenshiTextLexer', 'CssGenshiLexer', 'JavascriptGenshiLexer', 31 'GenshiTextLexer', 'CssGenshiLexer', 'JavascriptGenshiLexer',
32 'MyghtyLexer', 'MyghtyHtmlLexer', 'MyghtyXmlLexer', 32 'MyghtyLexer', 'MyghtyHtmlLexer', 'MyghtyXmlLexer',
33 'MyghtyCssLexer', 'MyghtyJavascriptLexer', 'MakoLexer', 33 'MyghtyCssLexer', 'MyghtyJavascriptLexer', 'MasonLexer', 'MakoLexer',
34 'MakoHtmlLexer', 'MakoXmlLexer', 'MakoJavascriptLexer', 34 'MakoHtmlLexer', 'MakoXmlLexer', 'MakoJavascriptLexer',
35 'MakoCssLexer', 'JspLexer', 'CheetahLexer', 'CheetahHtmlLexer', 35 'MakoCssLexer', 'JspLexer', 'CheetahLexer', 'CheetahHtmlLexer',
36 'CheetahXmlLexer', 'CheetahJavascriptLexer', 36 'CheetahXmlLexer', 'CheetahJavascriptLexer',
37 'EvoqueLexer', 'EvoqueHtmlLexer', 'EvoqueXmlLexer', 37 'EvoqueLexer', 'EvoqueHtmlLexer', 'EvoqueXmlLexer',
38 'ColdfusionLexer', 'ColdfusionHtmlLexer'] 38 'ColdfusionLexer', 'ColdfusionHtmlLexer',
39 'VelocityLexer', 'VelocityHtmlLexer', 'VelocityXmlLexer',
40 'SspLexer']
39 41
40 42
41 class ErbLexer(Lexer): 43 class ErbLexer(Lexer):
42 """ 44 """
43 Generic `ERB <http://ruby-doc.org/core/classes/ERB.html>`_ (Ruby Templating) 45 Generic `ERB <http://ruby-doc.org/core/classes/ERB.html>`_ (Ruby Templating)
186 if re.search('\{\$.*?\}', text): 188 if re.search('\{\$.*?\}', text):
187 rv += 0.01 189 rv += 0.01
188 return rv 190 return rv
189 191
190 192
193 class VelocityLexer(RegexLexer):
194 """
195 Generic `Velocity <http://velocity.apache.org/>`_ template lexer.
196
197 Just highlights velocity directives and variable references, other
198 data is left untouched by the lexer.
199 """
200
201 name = 'Velocity'
202 aliases = ['velocity']
203 filenames = ['*.vm','*.fhtml']
204
205 flags = re.MULTILINE | re.DOTALL
206
207 identifier = r'[a-zA-Z_][a-zA-Z0-9_]*'
208
209 tokens = {
210 'root': [
211 (r'[^{#$]+', Other),
212 (r'(#)(\*.*?\*)(#)',
213 bygroups(Comment.Preproc, Comment, Comment.Preproc)),
214 (r'(##)(.*?$)',
215 bygroups(Comment.Preproc, Comment)),
216 (r'(#\{?)(' + identifier + r')(\}?)(\s?\()',
217 bygroups(Comment.Preproc, Name.Function, Comment.Preproc, Punctuation),
218 'directiveparams'),
219 (r'(#\{?)(' + identifier + r')(\}|\b)',
220 bygroups(Comment.Preproc, Name.Function, Comment.Preproc)),
221 (r'\$\{?', Punctuation, 'variable')
222 ],
223 'variable': [
224 (identifier, Name.Variable),
225 (r'\(', Punctuation, 'funcparams'),
226 (r'(\.)(' + identifier + r')', bygroups(Punctuation, Name.Variable), '#push'),
227 (r'\}', Punctuation, '#pop'),
228 (r'', Other, '#pop')
229 ],
230 'directiveparams': [
231 (r'(&&|\|\||==?|!=?|[-<>+*%&\|\^/])|\b(eq|ne|gt|lt|ge|le|not|in)\b', Operator),
232 (r'\[', Operator, 'rangeoperator'),
233 (r'\b' + identifier + r'\b', Name.Function),
234 include('funcparams')
235 ],
236 'rangeoperator': [
237 (r'\.\.', Operator),
238 include('funcparams'),
239 (r'\]', Operator, '#pop')
240 ],
241 'funcparams': [
242 (r'\$\{?', Punctuation, 'variable'),
243 (r'\s+', Text),
244 (r',', Punctuation),
245 (r'"(\\\\|\\"|[^"])*"', String.Double),
246 (r"'(\\\\|\\'|[^'])*'", String.Single),
247 (r"0[xX][0-9a-fA-F]+[Ll]?", Number),
248 (r"\b[0-9]+\b", Number),
249 (r'(true|false|null)\b', Keyword.Constant),
250 (r'\(', Punctuation, '#push'),
251 (r'\)', Punctuation, '#pop')
252 ]
253 }
254
255 def analyse_text(text):
256 rv = 0.0
257 if re.search(r'#\{?macro\}?\(.*?\).*?#\{?end\}?', text):
258 rv += 0.25
259 if re.search(r'#\{?if\}?\(.+?\).*?#\{?end\}?', text):
260 rv += 0.15
261 if re.search(r'#\{?foreach\}?\(.+?\).*?#\{?end\}?', text):
262 rv += 0.15
263 if re.search(r'\$\{?[a-zA-Z_][a-zA-Z0-9_]*(\([^)]*\))?(\.[a-zA-Z0-9_]+(\([^)]*\))?)*\}?', text):
264 rv += 0.01
265 return rv
266
267
268 class VelocityHtmlLexer(DelegatingLexer):
269 """
270 Subclass of the `VelocityLexer` that highlights unlexer data
271 with the `HtmlLexer`.
272
273 """
274
275 name = 'HTML+Velocity'
276 aliases = ['html+velocity']
277 alias_filenames = ['*.html','*.fhtml']
278 mimetypes = ['text/html+velocity']
279
280 def __init__(self, **options):
281 super(VelocityHtmlLexer, self).__init__(HtmlLexer, VelocityLexer,
282 **options)
283
284
285 class VelocityXmlLexer(DelegatingLexer):
286 """
287 Subclass of the `VelocityLexer` that highlights unlexer data
288 with the `XmlLexer`.
289
290 """
291
292 name = 'XML+Velocity'
293 aliases = ['xml+velocity']
294 alias_filenames = ['*.xml','*.vm']
295 mimetypes = ['application/xml+velocity']
296
297 def __init__(self, **options):
298 super(VelocityXmlLexer, self).__init__(XmlLexer, VelocityLexer,
299 **options)
300
301 def analyse_text(text):
302 rv = VelocityLexer.analyse_text(text) - 0.01
303 if looks_like_xml(text):
304 rv += 0.5
305 return rv
306
307
191 class DjangoLexer(RegexLexer): 308 class DjangoLexer(RegexLexer):
192 """ 309 """
193 Generic `django <http://www.djangoproject.com/documentation/templates/>`_ 310 Generic `django <http://www.djangoproject.com/documentation/templates/>`_
194 and `jinja <http://wsgiarea.pocoo.org/jinja/>`_ template lexer. 311 and `jinja <http://wsgiarea.pocoo.org/jinja/>`_ template lexer.
195 312
237 (r'(_|true|false|none|True|False|None)\b', Keyword.Pseudo), 354 (r'(_|true|false|none|True|False|None)\b', Keyword.Pseudo),
238 (r'(in|as|reversed|recursive|not|and|or|is|if|else|import|' 355 (r'(in|as|reversed|recursive|not|and|or|is|if|else|import|'
239 r'with(?:(?:out)?\s*context)?|scoped|ignore\s+missing)\b', 356 r'with(?:(?:out)?\s*context)?|scoped|ignore\s+missing)\b',
240 Keyword), 357 Keyword),
241 (r'(loop|block|super|forloop)\b', Name.Builtin), 358 (r'(loop|block|super|forloop)\b', Name.Builtin),
242 (r'[a-zA-Z][a-zA-Z0-9_]*', Name.Variable), 359 (r'[a-zA-Z][a-zA-Z0-9_-]*', Name.Variable),
243 (r'\.[a-zA-Z0-9_]+', Name.Variable), 360 (r'\.[a-zA-Z0-9_]+', Name.Variable),
244 (r':?"(\\\\|\\"|[^"])*"', String.Double), 361 (r':?"(\\\\|\\"|[^"])*"', String.Double),
245 (r":?'(\\\\|\\'|[^'])*'", String.Single), 362 (r":?'(\\\\|\\'|[^'])*'", String.Single),
246 (r'([{}()\[\]+\-*/,:~]|[><=]=?)', Operator), 363 (r'([{}()\[\]+\-*/,:~]|[><=]=?)', Operator),
247 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" 364 (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|"
385 mimetypes = ['text/css+myghty'] 502 mimetypes = ['text/css+myghty']
386 503
387 def __init__(self, **options): 504 def __init__(self, **options):
388 super(MyghtyCssLexer, self).__init__(CssLexer, MyghtyLexer, 505 super(MyghtyCssLexer, self).__init__(CssLexer, MyghtyLexer,
389 **options) 506 **options)
507
508
509 class MasonLexer(RegexLexer):
510 """
511 Generic `mason templates`_ lexer. Stolen from Myghty lexer. Code that isn't
512 Mason markup is HTML.
513
514 .. _mason templates: http://www.masonhq.com/
515
516 *New in Pygments 1.4.*
517 """
518 name = 'Mason'
519 aliases = ['mason']
520 filenames = ['*.m', '*.mhtml', '*.mc', '*.mi', 'autohandler', 'dhandler']
521 mimetypes = ['application/x-mason']
522
523 tokens = {
524 'root': [
525 (r'\s+', Text),
526 (r'(<%doc>)(.*?)(</%doc>)(?s)',
527 bygroups(Name.Tag, Comment.Multiline, Name.Tag)),
528 (r'(<%(def|method))(\s*)(.*?)(>)(.*?)(</%\2\s*>)(?s)',
529 bygroups(Name.Tag, None, Text, Name.Function, Name.Tag,
530 using(this), Name.Tag)),
531 (r'(<%(\w+))(.*?)(>)(.*?)(</%\2\s*>)(?s)',
532 bygroups(Name.Tag, None, Name.Function, Name.Tag,
533 using(PerlLexer), Name.Tag)),
534 (r'(<&[^|])(.*?)(,.*?)?(&>)(?s)',
535 bygroups(Name.Tag, Name.Function, using(PerlLexer), Name.Tag)),
536 (r'(<&\|)(.*?)(,.*?)?(&>)(?s)',
537 bygroups(Name.Tag, Name.Function, using(PerlLexer), Name.Tag)),
538 (r'</&>', Name.Tag),
539 (r'(<%!?)(.*?)(%>)(?s)',
540 bygroups(Name.Tag, using(PerlLexer), Name.Tag)),
541 (r'(?<=^)#[^\n]*(\n|\Z)', Comment),
542 (r'(?<=^)(%)([^\n]*)(\n|\Z)',
543 bygroups(Name.Tag, using(PerlLexer), Other)),
544 (r"""(?sx)
545 (.+?) # anything, followed by:
546 (?:
547 (?<=\n)(?=[%#]) | # an eval or comment line
548 (?=</?[%&]) | # a substitution or block or
549 # call start or end
550 # - don't consume
551 (\\\n) | # an escaped newline
552 \Z # end of string
553 )""", bygroups(using(HtmlLexer), Operator)),
554 ]
555 }
556
557 def analyse_text(text):
558 rv = 0.0
559 if re.search('<&', text) is not None:
560 rv = 1.0
561 return rv
390 562
391 563
392 class MakoLexer(RegexLexer): 564 class MakoLexer(RegexLexer):
393 """ 565 """
394 Generic `mako templates`_ lexer. Code that isn't Mako 566 Generic `mako templates`_ lexer. Code that isn't Mako
1374 1546
1375 class ColdfusionHtmlLexer(DelegatingLexer): 1547 class ColdfusionHtmlLexer(DelegatingLexer):
1376 """ 1548 """
1377 Coldfusion markup in html 1549 Coldfusion markup in html
1378 """ 1550 """
1379 name = 'Coldufsion HTML' 1551 name = 'Coldfusion HTML'
1380 aliases = ['cfm'] 1552 aliases = ['cfm']
1381 filenames = ['*.cfm', '*.cfml', '*.cfc'] 1553 filenames = ['*.cfm', '*.cfml', '*.cfc']
1382 mimetypes = ['application/x-coldfusion'] 1554 mimetypes = ['application/x-coldfusion']
1383 1555
1384 def __init__(self, **options): 1556 def __init__(self, **options):
1385 super(ColdfusionHtmlLexer, self).__init__(HtmlLexer, ColdfusionMarkupLexer, 1557 super(ColdfusionHtmlLexer, self).__init__(HtmlLexer, ColdfusionMarkupLexer,
1386 **options) 1558 **options)
1387 1559
1560
1561 class SspLexer(DelegatingLexer):
1562 """
1563 Lexer for Scalate Server Pages.
1564
1565 *New in Pygments 1.4.*
1566 """
1567 name = 'Scalate Server Page'
1568 aliases = ['ssp']
1569 filenames = ['*.ssp']
1570 mimetypes = ['application/x-ssp']
1571
1572 def __init__(self, **options):
1573 super(SspLexer, self).__init__(XmlLexer, JspRootLexer, **options)
1574
1575 def analyse_text(text):
1576 rv = 0.0
1577 if re.search('val \w+\s*:', text):
1578 rv += 0.6
1579 if looks_like_xml(text):
1580 rv += 0.2
1581 if '<%' in text and '%>' in text:
1582 rv += 0.1
1583 return rv

eric ide

mercurial