diff options
author | Matth?us G. Chajdas <dev@anteru.net> | 2019-05-06 19:37:28 +0200 |
---|---|---|
committer | Matth?us G. Chajdas <dev@anteru.net> | 2019-05-06 19:37:28 +0200 |
commit | c3a5a7023c0a266845c485058241ffed2eb37176 (patch) | |
tree | bd3ebe4b75f240813bcdeaf7aa928b81c7e2be8b /pygments | |
parent | 249e5feec73189cfff3a4c81d6a6c9fc821286cd (diff) | |
download | pygments-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>.
Diffstat (limited to 'pygments')
-rw-r--r-- | pygments/formatters/html.py | 20 |
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): """ |