summaryrefslogtreecommitdiff
path: root/pygments/formatters/html.py
diff options
context:
space:
mode:
Diffstat (limited to 'pygments/formatters/html.py')
-rw-r--r--pygments/formatters/html.py44
1 files changed, 37 insertions, 7 deletions
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
index 7d7605eb..d65c09ce 100644
--- a/pygments/formatters/html.py
+++ b/pygments/formatters/html.py
@@ -5,7 +5,7 @@
Formatter for HTML output.
- :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
+ :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
@@ -41,6 +41,11 @@ def escape_html(text, table=_escape_html_table):
"""Escape &, <, > as well as single and double quotes for HTML."""
return text.translate(table)
+def webify(color):
+ if color.startswith('calc') or color.startswith('var'):
+ return color
+ else:
+ return '#' + color
def _get_ttype_class(ttype):
fname = STANDARD_TYPES.get(ttype)
@@ -55,6 +60,11 @@ def _get_ttype_class(ttype):
CSSFILE_TEMPLATE = '''\
+/*
+generated by Pygments <http://pygments.org>
+Copyright 2006-2019 by the Pygments team.
+Licensed under the BSD license, see LICENSE for details.
+*/
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
pre { line-height: 125%%; }
@@ -64,7 +74,11 @@ pre { line-height: 125%%; }
DOC_HEADER = '''\
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
-
+<!--
+generated by Pygments <http://pygments.org>
+Copyright 2006-2019 by the Pygments team.
+Licensed under the BSD license, see LICENSE for details.
+-->
<html>
<head>
<title>%(title)s</title>
@@ -322,11 +336,17 @@ class HtmlFormatter(Formatter):
.. versionadded:: 1.6
`filename`
- A string used to generate a filename when rendering <pre> blocks,
+ A string used to generate a filename when rendering ``<pre>`` blocks,
for example if displaying source code.
.. versionadded:: 2.1
+ `wrapcode`
+ Wrap the code inside ``<pre>`` blocks using ``<code>``, as recommended
+ by the HTML5 specification.
+
+ .. versionadded:: 2.4
+
**Subclassing the HTML formatter**
@@ -395,6 +415,7 @@ class HtmlFormatter(Formatter):
self.tagsfile = self._decodeifneeded(options.get('tagsfile', ''))
self.tagurlformat = self._decodeifneeded(options.get('tagurlformat', ''))
self.filename = self._decodeifneeded(options.get('filename', ''))
+ self.wrapcode = get_bool_opt(options, 'wrapcode', False)
if self.tagsfile:
if not ctags:
@@ -451,7 +472,7 @@ class HtmlFormatter(Formatter):
name = self._get_css_class(ttype)
style = ''
if ndef['color']:
- style += 'color: #%s; ' % ndef['color']
+ style += 'color: %s; ' % webify(ndef['color'])
if ndef['bold']:
style += 'font-weight: bold; '
if ndef['italic']:
@@ -459,9 +480,9 @@ class HtmlFormatter(Formatter):
if ndef['underline']:
style += 'text-decoration: underline; '
if ndef['bgcolor']:
- style += 'background-color: #%s; ' % ndef['bgcolor']
+ style += 'background-color: %s; ' % webify(ndef['bgcolor'])
if ndef['border']:
- style += 'border: 1px solid #%s; ' % ndef['border']
+ style += 'border: 1px solid %s; ' % webify(ndef['border'])
if style:
t2c[ttype] = name
# save len(ttype) to enable ordering the styles by
@@ -708,6 +729,12 @@ class HtmlFormatter(Formatter):
yield tup
yield 0, '</pre>'
+ def _wrap_code(self, inner):
+ yield 0, '<code>'
+ for tup in inner:
+ yield tup
+ yield 0, '</code>'
+
def _format_lines(self, tokensource):
"""
Just format the tokens, without any wrapping tags.
@@ -814,7 +841,10 @@ class HtmlFormatter(Formatter):
individual lines, in custom generators. See docstring
for `format`. Can be overridden.
"""
- return self._wrap_div(self._wrap_pre(source))
+ if self.wrapcode:
+ return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
+ else:
+ return self._wrap_div(self._wrap_pre(source))
def format_unencoded(self, tokensource, outfile):
"""