summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatth?us G. Chajdas <dev@anteru.net>2019-05-06 19:37:28 +0200
committerMatth?us G. Chajdas <dev@anteru.net>2019-05-06 19:37:28 +0200
commitc3a5a7023c0a266845c485058241ffed2eb37176 (patch)
treebd3ebe4b75f240813bcdeaf7aa928b81c7e2be8b
parent249e5feec73189cfff3a4c81d6a6c9fc821286cd (diff)
downloadpygments-c3a5a7023c0a266845c485058241ffed2eb37176.tar.gz
Add option to wrap the code in both <code> and <pre> (fixes #438.)
Adds a new option "wrapcode" which wraps the code using <code>.
-rw-r--r--pygments/formatters/html.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
index 7d7605eb..1ebfafcb 100644
--- a/pygments/formatters/html.py
+++ b/pygments/formatters/html.py
@@ -322,11 +322,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 +401,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:
@@ -708,6 +715,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 +827,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):
"""