html.py 35 KB

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