PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/gluon/highlight.py

https://github.com/goldenboy/skiheilw2p
Python | 334 lines | 296 code | 14 blank | 24 comment | 21 complexity | 6eed68ddc35784a9dedb2ec4524c0859 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. This file is part of the web2py Web Framework
  5. Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
  6. License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
  7. """
  8. import re
  9. import cgi
  10. __all__ = ['highlight']
  11. class Highlighter(object):
  12. """
  13. Do syntax highlighting.
  14. """
  15. def __init__(
  16. self,
  17. mode,
  18. link=None,
  19. styles={},
  20. ):
  21. """
  22. Initialise highlighter:
  23. mode = language (PYTHON, WEB2PY,C, CPP, HTML, HTML_PLAIN)
  24. """
  25. mode = mode.upper()
  26. if link and link[-1] != '/':
  27. link = link + '/'
  28. self.link = link
  29. self.styles = styles
  30. self.output = []
  31. self.span_style = None
  32. if mode == 'WEB2PY':
  33. (mode, self.suppress_tokens) = ('PYTHON', [])
  34. elif mode == 'PYTHON':
  35. self.suppress_tokens = ['GOTOHTML']
  36. elif mode == 'CPP':
  37. (mode, self.suppress_tokens) = ('C', [])
  38. elif mode == 'C':
  39. self.suppress_tokens = ['CPPKEYWORD']
  40. elif mode == 'HTML_PLAIN':
  41. (mode, self.suppress_tokens) = ('HTML', ['GOTOPYTHON'])
  42. elif mode == 'HTML':
  43. self.suppress_tokens = []
  44. else:
  45. raise SyntaxError, 'Unknown mode: %s' % mode
  46. self.mode = mode
  47. def c_tokenizer(
  48. self,
  49. token,
  50. match,
  51. style,
  52. ):
  53. """
  54. Callback for C specific highlighting.
  55. """
  56. value = cgi.escape(match.group())
  57. self.change_style(token, style)
  58. self.output.append(value)
  59. def python_tokenizer(
  60. self,
  61. token,
  62. match,
  63. style,
  64. ):
  65. """
  66. Callback for python specific highlighting.
  67. """
  68. value = cgi.escape(match.group())
  69. if token == 'MULTILINESTRING':
  70. self.change_style(token, style)
  71. self.output.append(value)
  72. self.strMultilineString = match.group(1)
  73. return 'PYTHONMultilineString'
  74. elif token == 'ENDMULTILINESTRING':
  75. if match.group(1) == self.strMultilineString:
  76. self.output.append(value)
  77. self.strMultilineString = ''
  78. return 'PYTHON'
  79. if style and style[:5] == 'link:':
  80. self.change_style(None, None)
  81. (url, style) = style[5:].split(';', 1)
  82. if url == 'None' or url == '':
  83. self.output.append('<span style="%s">%s</span>'
  84. % (style, value))
  85. else:
  86. self.output.append('<a href="%s%s" style="%s">%s</a>'
  87. % (url, value, style, value))
  88. else:
  89. self.change_style(token, style)
  90. self.output.append(value)
  91. if token == 'GOTOHTML':
  92. return 'HTML'
  93. return None
  94. def html_tokenizer(
  95. self,
  96. token,
  97. match,
  98. style,
  99. ):
  100. """
  101. Callback for HTML specific highlighting.
  102. """
  103. value = cgi.escape(match.group())
  104. self.change_style(token, style)
  105. self.output.append(value)
  106. if token == 'GOTOPYTHON':
  107. return 'PYTHON'
  108. return None
  109. all_styles = {
  110. 'C': (c_tokenizer, (
  111. ('COMMENT', re.compile(r'//.*\r?\n'),
  112. 'color: green; font-style: italic'),
  113. ('MULTILINECOMMENT', re.compile(r'/\*.*?\*/', re.DOTALL),
  114. 'color: green; font-style: italic'),
  115. ('PREPROCESSOR', re.compile(r'\s*#.*?[^\\]\s*\n',
  116. re.DOTALL), 'color: magenta; font-style: italic'),
  117. ('PUNC', re.compile(r'[-+*!&|^~/%\=<>\[\]{}(),.:]'),
  118. 'font-weight: bold'),
  119. ('NUMBER',
  120. re.compile(r'0x[0-9a-fA-F]+|[+-]?\d+(\.\d+)?([eE][+-]\d+)?|\d+'),
  121. 'color: red'),
  122. ('KEYWORD', re.compile(r'(sizeof|int|long|short|char|void|'
  123. + r'signed|unsigned|float|double|'
  124. + r'goto|break|return|continue|asm|'
  125. + r'case|default|if|else|switch|while|for|do|'
  126. + r'struct|union|enum|typedef|'
  127. + r'static|register|auto|volatile|extern|const)(?![a-zA-Z0-9_])'),
  128. 'color:#185369; font-weight: bold'),
  129. ('CPPKEYWORD',
  130. re.compile(r'(class|private|protected|public|template|new|delete|'
  131. + r'this|friend|using|inline|export|bool|throw|try|catch|'
  132. + r'operator|typeid|virtual)(?![a-zA-Z0-9_])'),
  133. 'color: blue; font-weight: bold'),
  134. ('STRING', re.compile(r'r?u?\'(.*?)(?<!\\)\'|"(.*?)(?<!\\)"'),
  135. 'color: #FF9966'),
  136. ('IDENTIFIER', re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*'),
  137. None),
  138. ('WHITESPACE', re.compile(r'[ \r\n]+'), 'Keep'),
  139. )),
  140. 'PYTHON': (python_tokenizer, (
  141. ('GOTOHTML', re.compile(r'\}\}'), 'color: red'),
  142. ('PUNC', re.compile(r'[-+*!|&^~/%\=<>\[\]{}(),.:]'),
  143. 'font-weight: bold'),
  144. ('NUMBER',
  145. re.compile(r'0x[0-9a-fA-F]+|[+-]?\d+(\.\d+)?([eE][+-]\d+)?|\d+'
  146. ), 'color: red'),
  147. ('KEYWORD',
  148. re.compile(r'(def|class|break|continue|del|exec|finally|pass|'
  149. + r'print|raise|return|try|except|global|assert|lambda|'
  150. + r'yield|for|while|if|elif|else|and|in|is|not|or|import|'
  151. + r'from|True|False)(?![a-zA-Z0-9_])'),
  152. 'color:#185369; font-weight: bold'),
  153. ('WEB2PY',
  154. re.compile(r'(request|response|session|cache|redirect|local_import|HTTP|TR|XML|URL|BEAUTIFY|A|BODY|BR|B|CENTER|CODE|DIV|EM|EMBED|FIELDSET|LEGEND|FORM|H1|H2|H3|H4|H5|H6|IFRAME|HEAD|HR|HTML|I|IMG|INPUT|LABEL|LI|LINK|MARKMIN|MENU|META|OBJECT|OL|ON|OPTION|P|PRE|SCRIPT|SELECT|SPAN|STYLE|TABLE|THEAD|TBODY|TFOOT|TAG|TD|TEXTAREA|TH|TITLE|TT|T|UL|XHTML|IS_SLUG|IS_STRONG|IS_LOWER|IS_UPPER|IS_ALPHANUMERIC|IS_DATETIME|IS_DATETIME_IN_RANGE|IS_DATE|IS_DATE_IN_RANGE|IS_DECIMAL_IN_RANGE|IS_EMAIL|IS_EXPR|IS_FLOAT_IN_RANGE|IS_IMAGE|IS_INT_IN_RANGE|IS_IN_SET|IS_IPV4|IS_LIST_OF|IS_LENGTH|IS_MATCH|IS_EQUAL_TO|IS_EMPTY_OR|IS_NULL_OR|IS_NOT_EMPTY|IS_TIME|IS_UPLOAD_FILENAME|IS_URL|CLEANUP|CRYPT|IS_IN_DB|IS_NOT_IN_DB|DAL|Field|SQLFORM|SQLTABLE|xmlescape|embed64)(?![a-zA-Z0-9_])'
  155. ), 'link:%(link)s;text-decoration:None;color:#FF5C1F;'),
  156. ('MAGIC', re.compile(r'self|None'),
  157. 'color:#185369; font-weight: bold'),
  158. ('MULTILINESTRING', re.compile(r'r?u?(\'\'\'|""")'),
  159. 'color: #FF9966'),
  160. ('STRING', re.compile(r'r?u?\'(.*?)(?<!\\)\'|"(.*?)(?<!\\)"'
  161. ), 'color: #FF9966'),
  162. ('IDENTIFIER', re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*'),
  163. None),
  164. ('COMMENT', re.compile(r'\#.*\r?\n'),
  165. 'color: green; font-style: italic'),
  166. ('WHITESPACE', re.compile(r'[ \r\n]+'), 'Keep'),
  167. )),
  168. 'PYTHONMultilineString': (python_tokenizer,
  169. (('ENDMULTILINESTRING',
  170. re.compile(r'.*?("""|\'\'\')',
  171. re.DOTALL), 'color: darkred'), )),
  172. 'HTML': (html_tokenizer, (
  173. ('GOTOPYTHON', re.compile(r'\{\{'), 'color: red'),
  174. ('COMMENT', re.compile(r'<!--[^>]*-->|<!>'),
  175. 'color: green; font-style: italic'),
  176. ('XMLCRAP', re.compile(r'<![^>]*>'),
  177. 'color: blue; font-style: italic'),
  178. ('SCRIPT', re.compile(r'<script .*?</script>', re.IGNORECASE
  179. + re.DOTALL), 'color: black'),
  180. ('TAG', re.compile(r'</?\s*[a-zA-Z0-9]+'),
  181. 'color: darkred; font-weight: bold'),
  182. ('ENDTAG', re.compile(r'/?>'),
  183. 'color: darkred; font-weight: bold'),
  184. )),
  185. }
  186. def highlight(self, data):
  187. """
  188. Syntax highlight some python code.
  189. Returns html version of code.
  190. """
  191. i = 0
  192. mode = self.mode
  193. while i < len(data):
  194. for (token, o_re, style) in Highlighter.all_styles[mode][1]:
  195. if not token in self.suppress_tokens:
  196. match = o_re.match(data, i)
  197. if match:
  198. if style:
  199. new_mode = \
  200. Highlighter.all_styles[mode][0](self,
  201. token, match, style
  202. % dict(link=self.link))
  203. else:
  204. new_mode = \
  205. Highlighter.all_styles[mode][0](self,
  206. token, match, style)
  207. if new_mode != None:
  208. mode = new_mode
  209. i += max(1, len(match.group()))
  210. break
  211. else:
  212. self.change_style(None, None)
  213. self.output.append(data[i])
  214. i += 1
  215. self.change_style(None, None)
  216. return ''.join(self.output).expandtabs(4)
  217. def change_style(self, token, style):
  218. """
  219. Generate output to change from existing style to another style only.
  220. """
  221. if token in self.styles:
  222. style = self.styles[token]
  223. if self.span_style != style:
  224. if style != 'Keep':
  225. if self.span_style != None:
  226. self.output.append('</span>')
  227. if style != None:
  228. self.output.append('<span style="%s">' % style)
  229. self.span_style = style
  230. def highlight(
  231. code,
  232. language,
  233. link='/examples/globals/vars/',
  234. counter=1,
  235. styles={},
  236. highlight_line=None,
  237. attributes={},
  238. ):
  239. if not 'CODE' in styles:
  240. code_style = """
  241. font-size: 11px;
  242. font-family: Bitstream Vera Sans Mono,monospace;
  243. background-color: transparent;
  244. margin: 0;
  245. padding: 5px;
  246. border: none;
  247. overflow: auto;
  248. white-space: pre !important;\n"""
  249. else:
  250. code_style = styles['CODE']
  251. if not 'LINENUMBERS' in styles:
  252. linenumbers_style = """
  253. font-size: 11px;
  254. font-family: Bitstream Vera Sans Mono,monospace;
  255. background-color: transparent;
  256. margin: 0;
  257. padding: 5px;
  258. border: none;
  259. color: #A0A0A0;\n"""
  260. else:
  261. linenumbers_style = styles['LINENUMBERS']
  262. if not 'LINEHIGHLIGHT' in styles:
  263. linehighlight_style = "background-color: #EBDDE2;"
  264. else:
  265. linehighlight_style = styles['LINEHIGHLIGHT']
  266. if language and language.upper() in ['PYTHON', 'C', 'CPP', 'HTML',
  267. 'WEB2PY']:
  268. code = Highlighter(language, link, styles).highlight(code)
  269. else:
  270. code = cgi.escape(code)
  271. lines = code.split('\n')
  272. if counter is None:
  273. linenumbers = [''] * len(lines)
  274. elif isinstance(counter, str):
  275. linenumbers = [cgi.escape(counter)] * len(lines)
  276. else:
  277. linenumbers = [str(i + counter) + '.' for i in
  278. xrange(len(lines))]
  279. if highlight_line:
  280. if counter and not isinstance(counter, str):
  281. lineno = highlight_line - counter
  282. else:
  283. lineno = highlight_line
  284. if lineno<len(lines):
  285. lines[lineno] = '<div style="%s">%s</div>' % (linehighlight_style, lines[lineno])
  286. linenumbers[lineno] = '<div style="%s">%s</div>' % (linehighlight_style, linenumbers[lineno])
  287. code = '<br/>'.join(lines)
  288. numbers = '<br/>'.join(linenumbers)
  289. items = attributes.items()
  290. fa = ' '.join([key[1:].lower() for (key, value) in items if key[:1]
  291. == '_' and value == None] + ['%s="%s"'
  292. % (key[1:].lower(), str(value).replace('"', "'"))
  293. for (key, value) in attributes.items() if key[:1]
  294. == '_' and value])
  295. if fa:
  296. fa = ' ' + fa
  297. return '<table%s><tr valign="top"><td style="width:40px; text-align: right;"><pre style="%s">%s</pre></td><td><pre style="%s">%s</pre></td></tr></table>'\
  298. % (fa, linenumbers_style, numbers, code_style, code)
  299. if __name__ == '__main__':
  300. import sys
  301. argfp = open(sys.argv[1])
  302. data = argfp.read()
  303. argfp.close()
  304. print '<html><body>' + highlight(data, sys.argv[2])\
  305. + '</body></html>'