Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# ext/pygmentplugin.py 

2# Copyright 2006-2020 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 

7from pygments import highlight 

8from pygments.formatters.html import HtmlFormatter 

9from pygments.lexer import bygroups 

10from pygments.lexer import DelegatingLexer 

11from pygments.lexer import include 

12from pygments.lexer import RegexLexer 

13from pygments.lexer import using 

14from pygments.lexers.agile import Python3Lexer 

15from pygments.lexers.agile import PythonLexer 

16from pygments.lexers.web import CssLexer 

17from pygments.lexers.web import HtmlLexer 

18from pygments.lexers.web import JavascriptLexer 

19from pygments.lexers.web import XmlLexer 

20from pygments.token import Comment 

21from pygments.token import Keyword 

22from pygments.token import Name 

23from pygments.token import Operator 

24from pygments.token import Other 

25from pygments.token import String 

26from pygments.token import Text 

27 

28from mako import compat 

29 

30 

31class MakoLexer(RegexLexer): 

32 name = "Mako" 

33 aliases = ["mako"] 

34 filenames = ["*.mao"] 

35 

36 tokens = { 

37 "root": [ 

38 ( 

39 r"(\s*)(\%)(\s*end(?:\w+))(\n|\Z)", 

40 bygroups(Text, Comment.Preproc, Keyword, Other), 

41 ), 

42 ( 

43 r"(\s*)(\%(?!%))([^\n]*)(\n|\Z)", 

44 bygroups(Text, Comment.Preproc, using(PythonLexer), Other), 

45 ), 

46 ( 

47 r"(\s*)(##[^\n]*)(\n|\Z)", 

48 bygroups(Text, Comment.Preproc, Other), 

49 ), 

50 (r"""(?s)<%doc>.*?</%doc>""", Comment.Preproc), 

51 ( 

52 r"(<%)([\w\.\:]+)", 

53 bygroups(Comment.Preproc, Name.Builtin), 

54 "tag", 

55 ), 

56 ( 

57 r"(</%)([\w\.\:]+)(>)", 

58 bygroups(Comment.Preproc, Name.Builtin, Comment.Preproc), 

59 ), 

60 (r"<%(?=([\w\.\:]+))", Comment.Preproc, "ondeftags"), 

61 ( 

62 r"(?s)(<%(?:!?))(.*?)(%>)", 

63 bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc), 

64 ), 

65 ( 

66 r"(\$\{)(.*?)(\})", 

67 bygroups(Comment.Preproc, using(PythonLexer), Comment.Preproc), 

68 ), 

69 ( 

70 r"""(?sx) 

71 (.+?) # anything, followed by: 

72 (?: 

73 (?<=\n)(?=%(?!%)|\#\#) | # an eval or comment line 

74 (?=\#\*) | # multiline comment 

75 (?=</?%) | # a python block 

76 # call start or end 

77 (?=\$\{) | # a substitution 

78 (?<=\n)(?=\s*%) | 

79 # - don't consume 

80 (\\\n) | # an escaped newline 

81 \Z # end of string 

82 ) 

83 """, 

84 bygroups(Other, Operator), 

85 ), 

86 (r"\s+", Text), 

87 ], 

88 "ondeftags": [ 

89 (r"<%", Comment.Preproc), 

90 (r"(?<=<%)(include|inherit|namespace|page)", Name.Builtin), 

91 include("tag"), 

92 ], 

93 "tag": [ 

94 (r'((?:\w+)\s*=)\s*(".*?")', bygroups(Name.Attribute, String)), 

95 (r"/?\s*>", Comment.Preproc, "#pop"), 

96 (r"\s+", Text), 

97 ], 

98 "attr": [ 

99 ('".*?"', String, "#pop"), 

100 ("'.*?'", String, "#pop"), 

101 (r"[^\s>]+", String, "#pop"), 

102 ], 

103 } 

104 

105 

106class MakoHtmlLexer(DelegatingLexer): 

107 name = "HTML+Mako" 

108 aliases = ["html+mako"] 

109 

110 def __init__(self, **options): 

111 super(MakoHtmlLexer, self).__init__(HtmlLexer, MakoLexer, **options) 

112 

113 

114class MakoXmlLexer(DelegatingLexer): 

115 name = "XML+Mako" 

116 aliases = ["xml+mako"] 

117 

118 def __init__(self, **options): 

119 super(MakoXmlLexer, self).__init__(XmlLexer, MakoLexer, **options) 

120 

121 

122class MakoJavascriptLexer(DelegatingLexer): 

123 name = "JavaScript+Mako" 

124 aliases = ["js+mako", "javascript+mako"] 

125 

126 def __init__(self, **options): 

127 super(MakoJavascriptLexer, self).__init__( 

128 JavascriptLexer, MakoLexer, **options 

129 ) 

130 

131 

132class MakoCssLexer(DelegatingLexer): 

133 name = "CSS+Mako" 

134 aliases = ["css+mako"] 

135 

136 def __init__(self, **options): 

137 super(MakoCssLexer, self).__init__(CssLexer, MakoLexer, **options) 

138 

139 

140pygments_html_formatter = HtmlFormatter( 

141 cssclass="syntax-highlighted", linenos=True 

142) 

143 

144 

145def syntax_highlight(filename="", language=None): 

146 mako_lexer = MakoLexer() 

147 if compat.py3k: 

148 python_lexer = Python3Lexer() 

149 else: 

150 python_lexer = PythonLexer() 

151 if filename.startswith("memory:") or language == "mako": 

152 return lambda string: highlight( 

153 string, mako_lexer, pygments_html_formatter 

154 ) 

155 return lambda string: highlight( 

156 string, python_lexer, pygments_html_formatter 

157 )