html.py 35 KB

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