pygmentplugin.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # ext/pygmentplugin.py
  2. # Copyright (C) 2006-2016 the Mako authors and contributors <see AUTHORS file>
  3. #
  4. # This module is part of Mako and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. from pygments.lexers.web import \
  7. HtmlLexer, XmlLexer, JavascriptLexer, CssLexer
  8. from pygments.lexers.agile import PythonLexer, Python3Lexer
  9. from pygments.lexer import DelegatingLexer, RegexLexer, bygroups, \
  10. include, using
  11. from pygments.token import \
  12. Text, Comment, Operator, Keyword, Name, String, Other
  13. from pygments.formatters.html import HtmlFormatter
  14. from pygments import highlight
  15. from mako import compat
  16. class MakoLexer(RegexLexer):
  17. name = 'Mako'
  18. aliases = ['mako']
  19. filenames = ['*.mao']
  20. tokens = {
  21. 'root': [
  22. (r'(\s*)(\%)(\s*end(?:\w+))(\n|\Z)',
  23. bygroups(Text, Comment.Preproc, Keyword, Other)),
  24. (r'(\s*)(\%(?!%))([^\n]*)(\n|\Z)',
  25. bygroups(Text, Comment.Preproc, using(PythonLexer), Other)),
  26. (r'(\s*)(##[^\n]*)(\n|\Z)',
  27. bygroups(Text, Comment.Preproc, Other)),
  28. (r'''(?s)<%doc>.*?</%doc>''', Comment.Preproc),
  29. (r'(<%)([\w\.\:]+)',
  30. bygroups(Comment.Preproc, Name.Builtin), 'tag'),
  31. (r'(</%)([\w\.\:]+)(>)',
  32. bygroups(Comment.Preproc, Name.Builtin, Comment.Preproc)),
  33. (r'<%(?=([\w\.\:]+))', Comment.Preproc, 'ondeftags'),
  34. (r'(<%(?:!?))(.*?)(%>)(?s)',
  35. bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc)),
  36. (r'(\$\{)(.*?)(\})',
  37. bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc)),
  38. (r'''(?sx)
  39. (.+?) # anything, followed by:
  40. (?:
  41. (?<=\n)(?=%(?!%)|\#\#) | # an eval or comment line
  42. (?=\#\*) | # multiline comment
  43. (?=</?%) | # a python block
  44. # call start or end
  45. (?=\$\{) | # a substitution
  46. (?<=\n)(?=\s*%) |
  47. # - don't consume
  48. (\\\n) | # an escaped newline
  49. \Z # end of string
  50. )
  51. ''', bygroups(Other, Operator)),
  52. (r'\s+', Text),
  53. ],
  54. 'ondeftags': [
  55. (r'<%', Comment.Preproc),
  56. (r'(?<=<%)(include|inherit|namespace|page)', Name.Builtin),
  57. include('tag'),
  58. ],
  59. 'tag': [
  60. (r'((?:\w+)\s*=)\s*(".*?")',
  61. bygroups(Name.Attribute, String)),
  62. (r'/?\s*>', Comment.Preproc, '#pop'),
  63. (r'\s+', Text),
  64. ],
  65. 'attr': [
  66. ('".*?"', String, '#pop'),
  67. ("'.*?'", String, '#pop'),
  68. (r'[^\s>]+', String, '#pop'),
  69. ],
  70. }
  71. class MakoHtmlLexer(DelegatingLexer):
  72. name = 'HTML+Mako'
  73. aliases = ['html+mako']
  74. def __init__(self, **options):
  75. super(MakoHtmlLexer, self).__init__(HtmlLexer, MakoLexer,
  76. **options)
  77. class MakoXmlLexer(DelegatingLexer):
  78. name = 'XML+Mako'
  79. aliases = ['xml+mako']
  80. def __init__(self, **options):
  81. super(MakoXmlLexer, self).__init__(XmlLexer, MakoLexer,
  82. **options)
  83. class MakoJavascriptLexer(DelegatingLexer):
  84. name = 'JavaScript+Mako'
  85. aliases = ['js+mako', 'javascript+mako']
  86. def __init__(self, **options):
  87. super(MakoJavascriptLexer, self).__init__(JavascriptLexer,
  88. MakoLexer, **options)
  89. class MakoCssLexer(DelegatingLexer):
  90. name = 'CSS+Mako'
  91. aliases = ['css+mako']
  92. def __init__(self, **options):
  93. super(MakoCssLexer, self).__init__(CssLexer, MakoLexer,
  94. **options)
  95. pygments_html_formatter = HtmlFormatter(cssclass='syntax-highlighted',
  96. linenos=True)
  97. def syntax_highlight(filename='', language=None):
  98. mako_lexer = MakoLexer()
  99. if compat.py3k:
  100. python_lexer = Python3Lexer()
  101. else:
  102. python_lexer = PythonLexer()
  103. if filename.startswith('memory:') or language == 'mako':
  104. return lambda string: highlight(string, mako_lexer,
  105. pygments_html_formatter)
  106. return lambda string: highlight(string, python_lexer,
  107. pygments_html_formatter)