html.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. """
  2. pygments.formatters.html
  3. ~~~~~~~~~~~~~~~~~~~~~~~~
  4. Formatter for HTML output.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import functools
  9. import os
  10. import sys
  11. import os.path
  12. from io import StringIO
  13. from pygments.formatter import Formatter
  14. from pygments.token import Token, Text, STANDARD_TYPES
  15. from pygments.util import get_bool_opt, get_int_opt, get_list_opt
  16. try:
  17. import ctags
  18. except ImportError:
  19. ctags = None
  20. __all__ = ['HtmlFormatter']
  21. _escape_html_table = {
  22. ord('&'): '&',
  23. ord('<'): '&lt;',
  24. ord('>'): '&gt;',
  25. ord('"'): '&quot;',
  26. ord("'"): '&#39;',
  27. }
  28. def escape_html(text, table=_escape_html_table):
  29. """Escape &, <, > as well as single and double quotes for HTML."""
  30. return text.translate(table)
  31. def webify(color):
  32. if color.startswith('calc') or color.startswith('var'):
  33. return color
  34. else:
  35. return '#' + color
  36. def _get_ttype_class(ttype):
  37. fname = STANDARD_TYPES.get(ttype)
  38. if fname:
  39. return fname
  40. aname = ''
  41. while fname is None:
  42. aname = '-' + ttype[-1] + aname
  43. ttype = ttype.parent
  44. fname = STANDARD_TYPES.get(ttype)
  45. return fname + aname
  46. CSSFILE_TEMPLATE = '''\
  47. /*
  48. generated by Pygments <https://pygments.org/>
  49. Copyright 2006-2021 by the Pygments team.
  50. Licensed under the BSD license, see LICENSE for details.
  51. */
  52. %(styledefs)s
  53. '''
  54. DOC_HEADER = '''\
  55. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  56. "http://www.w3.org/TR/html4/strict.dtd">
  57. <!--
  58. generated by Pygments <https://pygments.org/>
  59. Copyright 2006-2021 by the Pygments team.
  60. Licensed under the BSD license, see LICENSE for details.
  61. -->
  62. <html>
  63. <head>
  64. <title>%(title)s</title>
  65. <meta http-equiv="content-type" content="text/html; charset=%(encoding)s">
  66. <style type="text/css">
  67. ''' + CSSFILE_TEMPLATE + '''
  68. </style>
  69. </head>
  70. <body>
  71. <h2>%(title)s</h2>
  72. '''
  73. DOC_HEADER_EXTERNALCSS = '''\
  74. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  75. "http://www.w3.org/TR/html4/strict.dtd">
  76. <html>
  77. <head>
  78. <title>%(title)s</title>
  79. <meta http-equiv="content-type" content="text/html; charset=%(encoding)s">
  80. <link rel="stylesheet" href="%(cssfile)s" type="text/css">
  81. </head>
  82. <body>
  83. <h2>%(title)s</h2>
  84. '''
  85. DOC_FOOTER = '''\
  86. </body>
  87. </html>
  88. '''
  89. class HtmlFormatter(Formatter):
  90. r"""
  91. Format tokens as HTML 4 ``<span>`` tags within a ``<pre>`` tag, wrapped
  92. in a ``<div>`` tag. The ``<div>``'s CSS class can be set by the `cssclass`
  93. option.
  94. If the `linenos` option is set to ``"table"``, the ``<pre>`` is
  95. additionally wrapped inside a ``<table>`` which has one row and two
  96. cells: one containing the line numbers and one containing the code.
  97. Example:
  98. .. sourcecode:: html
  99. <div class="highlight" >
  100. <table><tr>
  101. <td class="linenos" title="click to toggle"
  102. onclick="with (this.firstChild.style)
  103. { display = (display == '') ? 'none' : '' }">
  104. <pre>1
  105. 2</pre>
  106. </td>
  107. <td class="code">
  108. <pre><span class="Ke">def </span><span class="NaFu">foo</span>(bar):
  109. <span class="Ke">pass</span>
  110. </pre>
  111. </td>
  112. </tr></table></div>
  113. (whitespace added to improve clarity).
  114. Wrapping can be disabled using the `nowrap` option.
  115. A list of lines can be specified using the `hl_lines` option to make these
  116. lines highlighted (as of Pygments 0.11).
  117. With the `full` option, a complete HTML 4 document is output, including
  118. the style definitions inside a ``<style>`` tag, or in a separate file if
  119. the `cssfile` option is given.
  120. When `tagsfile` is set to the path of a ctags index file, it is used to
  121. generate hyperlinks from names to their definition. You must enable
  122. `lineanchors` and run ctags with the `-n` option for this to work. The
  123. `python-ctags` module from PyPI must be installed to use this feature;
  124. otherwise a `RuntimeError` will be raised.
  125. The `get_style_defs(arg='')` method of a `HtmlFormatter` returns a string
  126. containing CSS rules for the CSS classes used by the formatter. The
  127. argument `arg` can be used to specify additional CSS selectors that
  128. are prepended to the classes. A call `fmter.get_style_defs('td .code')`
  129. would result in the following CSS classes:
  130. .. sourcecode:: css
  131. td .code .kw { font-weight: bold; color: #00FF00 }
  132. td .code .cm { color: #999999 }
  133. ...
  134. If you have Pygments 0.6 or higher, you can also pass a list or tuple to the
  135. `get_style_defs()` method to request multiple prefixes for the tokens:
  136. .. sourcecode:: python
  137. formatter.get_style_defs(['div.syntax pre', 'pre.syntax'])
  138. The output would then look like this:
  139. .. sourcecode:: css
  140. div.syntax pre .kw,
  141. pre.syntax .kw { font-weight: bold; color: #00FF00 }
  142. div.syntax pre .cm,
  143. pre.syntax .cm { color: #999999 }
  144. ...
  145. Additional options accepted:
  146. `nowrap`
  147. If set to ``True``, don't wrap the tokens at all, not even inside a ``<pre>``
  148. tag. This disables most other options (default: ``False``).
  149. `full`
  150. Tells the formatter to output a "full" document, i.e. a complete
  151. self-contained document (default: ``False``).
  152. `title`
  153. If `full` is true, the title that should be used to caption the
  154. document (default: ``''``).
  155. `style`
  156. The style to use, can be a string or a Style subclass (default:
  157. ``'default'``). This option has no effect if the `cssfile`
  158. and `noclobber_cssfile` option are given and the file specified in
  159. `cssfile` exists.
  160. `noclasses`
  161. If set to true, token ``<span>`` tags (as well as line number elements)
  162. will not use CSS classes, but inline styles. This is not recommended
  163. for larger pieces of code since it increases output size by quite a bit
  164. (default: ``False``).
  165. `classprefix`
  166. Since the token types use relatively short class names, they may clash
  167. with some of your own class names. In this case you can use the
  168. `classprefix` option to give a string to prepend to all Pygments-generated
  169. CSS class names for token types.
  170. Note that this option also affects the output of `get_style_defs()`.
  171. `cssclass`
  172. CSS class for the wrapping ``<div>`` tag (default: ``'highlight'``).
  173. If you set this option, the default selector for `get_style_defs()`
  174. will be this class.
  175. .. versionadded:: 0.9
  176. If you select the ``'table'`` line numbers, the wrapping table will
  177. have a CSS class of this string plus ``'table'``, the default is
  178. accordingly ``'highlighttable'``.
  179. `cssstyles`
  180. Inline CSS styles for the wrapping ``<div>`` tag (default: ``''``).
  181. `prestyles`
  182. Inline CSS styles for the ``<pre>`` tag (default: ``''``).
  183. .. versionadded:: 0.11
  184. `cssfile`
  185. If the `full` option is true and this option is given, it must be the
  186. name of an external file. If the filename does not include an absolute
  187. path, the file's path will be assumed to be relative to the main output
  188. file's path, if the latter can be found. The stylesheet is then written
  189. to this file instead of the HTML file.
  190. .. versionadded:: 0.6
  191. `noclobber_cssfile`
  192. If `cssfile` is given and the specified file exists, the css file will
  193. not be overwritten. This allows the use of the `full` option in
  194. combination with a user specified css file. Default is ``False``.
  195. .. versionadded:: 1.1
  196. `linenos`
  197. If set to ``'table'``, output line numbers as a table with two cells,
  198. one containing the line numbers, the other the whole code. This is
  199. copy-and-paste-friendly, but may cause alignment problems with some
  200. browsers or fonts. If set to ``'inline'``, the line numbers will be
  201. integrated in the ``<pre>`` tag that contains the code (that setting
  202. is *new in Pygments 0.8*).
  203. For compatibility with Pygments 0.7 and earlier, every true value
  204. except ``'inline'`` means the same as ``'table'`` (in particular, that
  205. means also ``True``).
  206. The default value is ``False``, which means no line numbers at all.
  207. **Note:** with the default ("table") line number mechanism, the line
  208. numbers and code can have different line heights in Internet Explorer
  209. unless you give the enclosing ``<pre>`` tags an explicit ``line-height``
  210. CSS property (you get the default line spacing with ``line-height:
  211. 125%``).
  212. `hl_lines`
  213. Specify a list of lines to be highlighted. The line numbers are always
  214. relative to the input (i.e. the first line is line 1) and are
  215. independent of `linenostart`.
  216. .. versionadded:: 0.11
  217. `linenostart`
  218. The line number for the first line (default: ``1``).
  219. `linenostep`
  220. If set to a number n > 1, only every nth line number is printed.
  221. `linenospecial`
  222. If set to a number n > 0, every nth line number is given the CSS
  223. class ``"special"`` (default: ``0``).
  224. `nobackground`
  225. If set to ``True``, the formatter won't output the background color
  226. for the wrapping element (this automatically defaults to ``False``
  227. when there is no wrapping element [eg: no argument for the
  228. `get_syntax_defs` method given]) (default: ``False``).
  229. .. versionadded:: 0.6
  230. `lineseparator`
  231. This string is output between lines of code. It defaults to ``"\n"``,
  232. which is enough to break a line inside ``<pre>`` tags, but you can
  233. e.g. set it to ``"<br>"`` to get HTML line breaks.
  234. .. versionadded:: 0.7
  235. `lineanchors`
  236. If set to a nonempty string, e.g. ``foo``, the formatter will wrap each
  237. output line in an anchor tag with an ``id`` (and `name`) of ``foo-linenumber``.
  238. This allows easy linking to certain lines.
  239. .. versionadded:: 0.9
  240. `linespans`
  241. If set to a nonempty string, e.g. ``foo``, the formatter will wrap each
  242. output line in a span tag with an ``id`` of ``foo-linenumber``.
  243. This allows easy access to lines via javascript.
  244. .. versionadded:: 1.6
  245. `anchorlinenos`
  246. If set to `True`, will wrap line numbers in <a> tags. Used in
  247. combination with `linenos` and `lineanchors`.
  248. `tagsfile`
  249. If set to the path of a ctags file, wrap names in anchor tags that
  250. link to their definitions. `lineanchors` should be used, and the
  251. tags file should specify line numbers (see the `-n` option to ctags).
  252. .. versionadded:: 1.6
  253. `tagurlformat`
  254. A string formatting pattern used to generate links to ctags definitions.
  255. Available variables are `%(path)s`, `%(fname)s` and `%(fext)s`.
  256. Defaults to an empty string, resulting in just `#prefix-number` links.
  257. .. versionadded:: 1.6
  258. `filename`
  259. A string used to generate a filename when rendering ``<pre>`` blocks,
  260. for example if displaying source code. If `linenos` is set to
  261. ``'table'`` then the filename will be rendered in an initial row
  262. containing a single `<th>` which spans both columns.
  263. .. versionadded:: 2.1
  264. `wrapcode`
  265. Wrap the code inside ``<pre>`` blocks using ``<code>``, as recommended
  266. by the HTML5 specification.
  267. .. versionadded:: 2.4
  268. **Subclassing the HTML formatter**
  269. .. versionadded:: 0.7
  270. The HTML formatter is now built in a way that allows easy subclassing, thus
  271. customizing the output HTML code. The `format()` method calls
  272. `self._format_lines()` which returns a generator that yields tuples of ``(1,
  273. line)``, where the ``1`` indicates that the ``line`` is a line of the
  274. formatted source code.
  275. If the `nowrap` option is set, the generator is the iterated over and the
  276. resulting HTML is output.
  277. Otherwise, `format()` calls `self.wrap()`, which wraps the generator with
  278. other generators. These may add some HTML code to the one generated by
  279. `_format_lines()`, either by modifying the lines generated by the latter,
  280. then yielding them again with ``(1, line)``, and/or by yielding other HTML
  281. code before or after the lines, with ``(0, html)``. The distinction between
  282. source lines and other code makes it possible to wrap the generator multiple
  283. times.
  284. The default `wrap()` implementation adds a ``<div>`` and a ``<pre>`` tag.
  285. A custom `HtmlFormatter` subclass could look like this:
  286. .. sourcecode:: python
  287. class CodeHtmlFormatter(HtmlFormatter):
  288. def wrap(self, source, outfile):
  289. return self._wrap_code(source)
  290. def _wrap_code(self, source):
  291. yield 0, '<code>'
  292. for i, t in source:
  293. if i == 1:
  294. # it's a line of formatted code
  295. t += '<br>'
  296. yield i, t
  297. yield 0, '</code>'
  298. This results in wrapping the formatted lines with a ``<code>`` tag, where the
  299. source lines are broken using ``<br>`` tags.
  300. After calling `wrap()`, the `format()` method also adds the "line numbers"
  301. and/or "full document" wrappers if the respective options are set. Then, all
  302. HTML yielded by the wrapped generator is output.
  303. """
  304. name = 'HTML'
  305. aliases = ['html']
  306. filenames = ['*.html', '*.htm']
  307. def __init__(self, **options):
  308. Formatter.__init__(self, **options)
  309. self.title = self._decodeifneeded(self.title)
  310. self.nowrap = get_bool_opt(options, 'nowrap', False)
  311. self.noclasses = get_bool_opt(options, 'noclasses', False)
  312. self.classprefix = options.get('classprefix', '')
  313. self.cssclass = self._decodeifneeded(options.get('cssclass', 'highlight'))
  314. self.cssstyles = self._decodeifneeded(options.get('cssstyles', ''))
  315. self.prestyles = self._decodeifneeded(options.get('prestyles', ''))
  316. self.cssfile = self._decodeifneeded(options.get('cssfile', ''))
  317. self.noclobber_cssfile = get_bool_opt(options, 'noclobber_cssfile', False)
  318. self.tagsfile = self._decodeifneeded(options.get('tagsfile', ''))
  319. self.tagurlformat = self._decodeifneeded(options.get('tagurlformat', ''))
  320. self.filename = self._decodeifneeded(options.get('filename', ''))
  321. self.wrapcode = get_bool_opt(options, 'wrapcode', False)
  322. self.span_element_openers = {}
  323. if self.tagsfile:
  324. if not ctags:
  325. raise RuntimeError('The "ctags" package must to be installed '
  326. 'to be able to use the "tagsfile" feature.')
  327. self._ctags = ctags.CTags(self.tagsfile)
  328. linenos = options.get('linenos', False)
  329. if linenos == 'inline':
  330. self.linenos = 2
  331. elif linenos:
  332. # compatibility with <= 0.7
  333. self.linenos = 1
  334. else:
  335. self.linenos = 0
  336. self.linenostart = abs(get_int_opt(options, 'linenostart', 1))
  337. self.linenostep = abs(get_int_opt(options, 'linenostep', 1))
  338. self.linenospecial = abs(get_int_opt(options, 'linenospecial', 0))
  339. self.nobackground = get_bool_opt(options, 'nobackground', False)
  340. self.lineseparator = options.get('lineseparator', '\n')
  341. self.lineanchors = options.get('lineanchors', '')
  342. self.linespans = options.get('linespans', '')
  343. self.anchorlinenos = get_bool_opt(options, 'anchorlinenos', False)
  344. self.hl_lines = set()
  345. for lineno in get_list_opt(options, 'hl_lines', []):
  346. try:
  347. self.hl_lines.add(int(lineno))
  348. except ValueError:
  349. pass
  350. self._create_stylesheet()
  351. def _get_css_class(self, ttype):
  352. """Return the css class of this token type prefixed with
  353. the classprefix option."""
  354. ttypeclass = _get_ttype_class(ttype)
  355. if ttypeclass:
  356. return self.classprefix + ttypeclass
  357. return ''
  358. def _get_css_classes(self, ttype):
  359. """Return the CSS classes of this token type prefixed with the classprefix option."""
  360. cls = self._get_css_class(ttype)
  361. while ttype not in STANDARD_TYPES:
  362. ttype = ttype.parent
  363. cls = self._get_css_class(ttype) + ' ' + cls
  364. return cls or ''
  365. def _get_css_inline_styles(self, ttype):
  366. """Return the inline CSS styles for this token type."""
  367. cclass = self.ttype2class.get(ttype)
  368. while cclass is None:
  369. ttype = ttype.parent
  370. cclass = self.ttype2class.get(ttype)
  371. return cclass or ''
  372. def _create_stylesheet(self):
  373. t2c = self.ttype2class = {Token: ''}
  374. c2s = self.class2style = {}
  375. for ttype, ndef in self.style:
  376. name = self._get_css_class(ttype)
  377. style = ''
  378. if ndef['color']:
  379. style += 'color: %s; ' % webify(ndef['color'])
  380. if ndef['bold']:
  381. style += 'font-weight: bold; '
  382. if ndef['italic']:
  383. style += 'font-style: italic; '
  384. if ndef['underline']:
  385. style += 'text-decoration: underline; '
  386. if ndef['bgcolor']:
  387. style += 'background-color: %s; ' % webify(ndef['bgcolor'])
  388. if ndef['border']:
  389. style += 'border: 1px solid %s; ' % webify(ndef['border'])
  390. if style:
  391. t2c[ttype] = name
  392. # save len(ttype) to enable ordering the styles by
  393. # hierarchy (necessary for CSS cascading rules!)
  394. c2s[name] = (style[:-2], ttype, len(ttype))
  395. def get_style_defs(self, arg=None):
  396. """
  397. Return CSS style definitions for the classes produced by the current
  398. highlighting style. ``arg`` can be a string or list of selectors to
  399. insert before the token type classes.
  400. """
  401. style_lines = []
  402. style_lines.extend(self.get_linenos_style_defs())
  403. style_lines.extend(self.get_background_style_defs(arg))
  404. style_lines.extend(self.get_token_style_defs(arg))
  405. return '\n'.join(style_lines)
  406. def get_token_style_defs(self, arg=None):
  407. prefix = self.get_css_prefix(arg)
  408. styles = [
  409. (level, ttype, cls, style)
  410. for cls, (style, ttype, level) in self.class2style.items()
  411. if cls and style
  412. ]
  413. styles.sort()
  414. lines = [
  415. '%s { %s } /* %s */' % (prefix(cls), style, repr(ttype)[6:])
  416. for (level, ttype, cls, style) in styles
  417. ]
  418. return lines
  419. def get_background_style_defs(self, arg=None):
  420. prefix = self.get_css_prefix(arg)
  421. bg_color = self.style.background_color
  422. hl_color = self.style.highlight_color
  423. lines = []
  424. if arg and not self.nobackground and bg_color is not None:
  425. text_style = ''
  426. if Text in self.ttype2class:
  427. text_style = ' ' + self.class2style[self.ttype2class[Text]][0]
  428. lines.insert(
  429. 0, '%s{ background: %s;%s }' % (
  430. prefix(''), bg_color, text_style
  431. )
  432. )
  433. if hl_color is not None:
  434. lines.insert(
  435. 0, '%s { background-color: %s }' % (prefix('hll'), hl_color)
  436. )
  437. return lines
  438. def get_linenos_style_defs(self):
  439. lines = [
  440. 'pre { %s }' % self._pre_style,
  441. 'td.linenos .normal { %s }' % self._linenos_style,
  442. 'span.linenos { %s }' % self._linenos_style,
  443. 'td.linenos .special { %s }' % self._linenos_special_style,
  444. 'span.linenos.special { %s }' % self._linenos_special_style,
  445. ]
  446. return lines
  447. def get_css_prefix(self, arg):
  448. if arg is None:
  449. arg = ('cssclass' in self.options and '.'+self.cssclass or '')
  450. if isinstance(arg, str):
  451. args = [arg]
  452. else:
  453. args = list(arg)
  454. def prefix(cls):
  455. if cls:
  456. cls = '.' + cls
  457. tmp = []
  458. for arg in args:
  459. tmp.append((arg and arg + ' ' or '') + cls)
  460. return ', '.join(tmp)
  461. return prefix
  462. @property
  463. def _pre_style(self):
  464. return 'line-height: 125%;'
  465. @property
  466. def _linenos_style(self):
  467. return 'color: %s; background-color: %s; padding-left: 5px; padding-right: 5px;' % (
  468. self.style.line_number_color,
  469. self.style.line_number_background_color
  470. )
  471. @property
  472. def _linenos_special_style(self):
  473. return 'color: %s; background-color: %s; padding-left: 5px; padding-right: 5px;' % (
  474. self.style.line_number_special_color,
  475. self.style.line_number_special_background_color
  476. )
  477. def _decodeifneeded(self, value):
  478. if isinstance(value, bytes):
  479. if self.encoding:
  480. return value.decode(self.encoding)
  481. return value.decode()
  482. return value
  483. def _wrap_full(self, inner, outfile):
  484. if self.cssfile:
  485. if os.path.isabs(self.cssfile):
  486. # it's an absolute filename
  487. cssfilename = self.cssfile
  488. else:
  489. try:
  490. filename = outfile.name
  491. if not filename or filename[0] == '<':
  492. # pseudo files, e.g. name == '<fdopen>'
  493. raise AttributeError
  494. cssfilename = os.path.join(os.path.dirname(filename),
  495. self.cssfile)
  496. except AttributeError:
  497. print('Note: Cannot determine output file name, '
  498. 'using current directory as base for the CSS file name',
  499. file=sys.stderr)
  500. cssfilename = self.cssfile
  501. # write CSS file only if noclobber_cssfile isn't given as an option.
  502. try:
  503. if not os.path.exists(cssfilename) or not self.noclobber_cssfile:
  504. with open(cssfilename, "w") as cf:
  505. cf.write(CSSFILE_TEMPLATE %
  506. {'styledefs': self.get_style_defs('body')})
  507. except OSError as err:
  508. err.strerror = 'Error writing CSS file: ' + err.strerror
  509. raise
  510. yield 0, (DOC_HEADER_EXTERNALCSS %
  511. dict(title=self.title,
  512. cssfile=self.cssfile,
  513. encoding=self.encoding))
  514. else:
  515. yield 0, (DOC_HEADER %
  516. dict(title=self.title,
  517. styledefs=self.get_style_defs('body'),
  518. encoding=self.encoding))
  519. yield from inner
  520. yield 0, DOC_FOOTER
  521. def _wrap_tablelinenos(self, inner):
  522. dummyoutfile = StringIO()
  523. lncount = 0
  524. for t, line in inner:
  525. if t:
  526. lncount += 1
  527. dummyoutfile.write(line)
  528. fl = self.linenostart
  529. mw = len(str(lncount + fl - 1))
  530. sp = self.linenospecial
  531. st = self.linenostep
  532. la = self.lineanchors
  533. aln = self.anchorlinenos
  534. nocls = self.noclasses
  535. lines = []
  536. for i in range(fl, fl+lncount):
  537. print_line = i % st == 0
  538. special_line = sp and i % sp == 0
  539. if print_line:
  540. line = '%*d' % (mw, i)
  541. if aln:
  542. line = '<a href="#%s-%d">%s</a>' % (la, i, line)
  543. else:
  544. line = ' ' * mw
  545. if nocls:
  546. if special_line:
  547. style = ' style="%s"' % self._linenos_special_style
  548. else:
  549. style = ' style="%s"' % self._linenos_style
  550. else:
  551. if special_line:
  552. style = ' class="special"'
  553. else:
  554. style = ' class="normal"'
  555. if style:
  556. line = '<span%s>%s</span>' % (style, line)
  557. lines.append(line)
  558. ls = '\n'.join(lines)
  559. # If a filename was specified, we can't put it into the code table as it
  560. # would misalign the line numbers. Hence we emit a separate row for it.
  561. filename_tr = ""
  562. if self.filename:
  563. filename_tr = (
  564. '<tr><th colspan="2" class="filename"><div class="highlight">'
  565. '<span class="filename">' + self.filename + '</span></div>'
  566. '</th></tr>')
  567. # in case you wonder about the seemingly redundant <div> here: since the
  568. # content in the other cell also is wrapped in a div, some browsers in
  569. # some configurations seem to mess up the formatting...
  570. yield 0, (
  571. '<table class="%stable">' % self.cssclass + filename_tr +
  572. '<tr><td class="linenos"><div class="linenodiv"><pre>' +
  573. ls + '</pre></div></td><td class="code">'
  574. )
  575. yield 0, dummyoutfile.getvalue()
  576. yield 0, '</td></tr></table>'
  577. def _wrap_inlinelinenos(self, inner):
  578. # need a list of lines since we need the width of a single number :(
  579. inner_lines = list(inner)
  580. sp = self.linenospecial
  581. st = self.linenostep
  582. num = self.linenostart
  583. mw = len(str(len(inner_lines) + num - 1))
  584. la = self.lineanchors
  585. aln = self.anchorlinenos
  586. nocls = self.noclasses
  587. for _, inner_line in inner_lines:
  588. print_line = num % st == 0
  589. special_line = sp and num % sp == 0
  590. if print_line:
  591. line = '%*d' % (mw, num)
  592. else:
  593. line = ' ' * mw
  594. if nocls:
  595. if special_line:
  596. style = ' style="%s"' % self._linenos_special_style
  597. else:
  598. style = ' style="%s"' % self._linenos_style
  599. else:
  600. if special_line:
  601. style = ' class="linenos special"'
  602. else:
  603. style = ' class="linenos"'
  604. if style:
  605. linenos = '<span%s>%s</span>' % (style, line)
  606. else:
  607. linenos = line
  608. if aln:
  609. yield 1, ('<a href="#%s-%d">%s</a>' % (la, num, linenos) +
  610. inner_line)
  611. else:
  612. yield 1, linenos + inner_line
  613. num += 1
  614. def _wrap_lineanchors(self, inner):
  615. s = self.lineanchors
  616. # subtract 1 since we have to increment i *before* yielding
  617. i = self.linenostart - 1
  618. for t, line in inner:
  619. if t:
  620. i += 1
  621. yield 1, '<a id="%s-%d" name="%s-%d"></a>' % (s, i, s, i) + line
  622. else:
  623. yield 0, line
  624. def _wrap_linespans(self, inner):
  625. s = self.linespans
  626. i = self.linenostart - 1
  627. for t, line in inner:
  628. if t:
  629. i += 1
  630. yield 1, '<span id="%s-%d">%s</span>' % (s, i, line)
  631. else:
  632. yield 0, line
  633. def _wrap_div(self, inner):
  634. style = []
  635. if (self.noclasses and not self.nobackground and
  636. self.style.background_color is not None):
  637. style.append('background: %s' % (self.style.background_color,))
  638. if self.cssstyles:
  639. style.append(self.cssstyles)
  640. style = '; '.join(style)
  641. yield 0, ('<div' + (self.cssclass and ' class="%s"' % self.cssclass) +
  642. (style and (' style="%s"' % style)) + '>')
  643. yield from inner
  644. yield 0, '</div>\n'
  645. def _wrap_pre(self, inner):
  646. style = []
  647. if self.prestyles:
  648. style.append(self.prestyles)
  649. if self.noclasses:
  650. style.append(self._pre_style)
  651. style = '; '.join(style)
  652. if self.filename and self.linenos != 1:
  653. yield 0, ('<span class="filename">' + self.filename + '</span>')
  654. # the empty span here is to keep leading empty lines from being
  655. # ignored by HTML parsers
  656. yield 0, ('<pre' + (style and ' style="%s"' % style) + '><span></span>')
  657. yield from inner
  658. yield 0, '</pre>'
  659. def _wrap_code(self, inner):
  660. yield 0, '<code>'
  661. yield from inner
  662. yield 0, '</code>'
  663. @functools.lru_cache(maxsize=100)
  664. def _translate_parts(self, value):
  665. """HTML-escape a value and split it by newlines."""
  666. return value.translate(_escape_html_table).split('\n')
  667. def _format_lines(self, tokensource):
  668. """
  669. Just format the tokens, without any wrapping tags.
  670. Yield individual lines.
  671. """
  672. nocls = self.noclasses
  673. lsep = self.lineseparator
  674. tagsfile = self.tagsfile
  675. lspan = ''
  676. line = []
  677. for ttype, value in tokensource:
  678. try:
  679. cspan = self.span_element_openers[ttype]
  680. except KeyError:
  681. if nocls:
  682. css_style = self._get_css_inline_styles(ttype)
  683. cspan = css_style and '<span style="%s">' % self.class2style[css_style][0] or ''
  684. else:
  685. css_class = self._get_css_classes(ttype)
  686. cspan = css_class and '<span class="%s">' % css_class or ''
  687. self.span_element_openers[ttype] = cspan
  688. parts = self._translate_parts(value)
  689. if tagsfile and ttype in Token.Name:
  690. filename, linenumber = self._lookup_ctag(value)
  691. if linenumber:
  692. base, filename = os.path.split(filename)
  693. if base:
  694. base += '/'
  695. filename, extension = os.path.splitext(filename)
  696. url = self.tagurlformat % {'path': base, 'fname': filename,
  697. 'fext': extension}
  698. parts[0] = "<a href=\"%s#%s-%d\">%s" % \
  699. (url, self.lineanchors, linenumber, parts[0])
  700. parts[-1] = parts[-1] + "</a>"
  701. # for all but the last line
  702. for part in parts[:-1]:
  703. if line:
  704. if lspan != cspan:
  705. line.extend(((lspan and '</span>'), cspan, part,
  706. (cspan and '</span>'), lsep))
  707. else: # both are the same
  708. line.extend((part, (lspan and '</span>'), lsep))
  709. yield 1, ''.join(line)
  710. line = []
  711. elif part:
  712. yield 1, ''.join((cspan, part, (cspan and '</span>'), lsep))
  713. else:
  714. yield 1, lsep
  715. # for the last line
  716. if line and parts[-1]:
  717. if lspan != cspan:
  718. line.extend(((lspan and '</span>'), cspan, parts[-1]))
  719. lspan = cspan
  720. else:
  721. line.append(parts[-1])
  722. elif parts[-1]:
  723. line = [cspan, parts[-1]]
  724. lspan = cspan
  725. # else we neither have to open a new span nor set lspan
  726. if line:
  727. line.extend(((lspan and '</span>'), lsep))
  728. yield 1, ''.join(line)
  729. def _lookup_ctag(self, token):
  730. entry = ctags.TagEntry()
  731. if self._ctags.find(entry, token.encode(), 0):
  732. return entry['file'], entry['lineNumber']
  733. else:
  734. return None, None
  735. def _highlight_lines(self, tokensource):
  736. """
  737. Highlighted the lines specified in the `hl_lines` option by
  738. post-processing the token stream coming from `_format_lines`.
  739. """
  740. hls = self.hl_lines
  741. for i, (t, value) in enumerate(tokensource):
  742. if t != 1:
  743. yield t, value
  744. if i + 1 in hls: # i + 1 because Python indexes start at 0
  745. if self.noclasses:
  746. style = ''
  747. if self.style.highlight_color is not None:
  748. style = (' style="background-color: %s"' %
  749. (self.style.highlight_color,))
  750. yield 1, '<span%s>%s</span>' % (style, value)
  751. else:
  752. yield 1, '<span class="hll">%s</span>' % value
  753. else:
  754. yield 1, value
  755. def wrap(self, source, outfile):
  756. """
  757. Wrap the ``source``, which is a generator yielding
  758. individual lines, in custom generators. See docstring
  759. for `format`. Can be overridden.
  760. """
  761. if self.wrapcode:
  762. return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
  763. else:
  764. return self._wrap_div(self._wrap_pre(source))
  765. def format_unencoded(self, tokensource, outfile):
  766. """
  767. The formatting process uses several nested generators; which of
  768. them are used is determined by the user's options.
  769. Each generator should take at least one argument, ``inner``,
  770. and wrap the pieces of text generated by this.
  771. Always yield 2-tuples: (code, text). If "code" is 1, the text
  772. is part of the original tokensource being highlighted, if it's
  773. 0, the text is some piece of wrapping. This makes it possible to
  774. use several different wrappers that process the original source
  775. linewise, e.g. line number generators.
  776. """
  777. source = self._format_lines(tokensource)
  778. # As a special case, we wrap line numbers before line highlighting
  779. # so the line numbers get wrapped in the highlighting tag.
  780. if not self.nowrap and self.linenos == 2:
  781. source = self._wrap_inlinelinenos(source)
  782. if self.hl_lines:
  783. source = self._highlight_lines(source)
  784. if not self.nowrap:
  785. if self.lineanchors:
  786. source = self._wrap_lineanchors(source)
  787. if self.linespans:
  788. source = self._wrap_linespans(source)
  789. source = self.wrap(source, outfile)
  790. if self.linenos == 1:
  791. source = self._wrap_tablelinenos(source)
  792. if self.full:
  793. source = self._wrap_full(source, outfile)
  794. for t, piece in source:
  795. outfile.write(piece)