diff options
author | Matthäus G. Chajdas <Anteru@users.noreply.github.com> | 2022-04-24 14:56:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-24 14:56:11 +0200 |
commit | 992fa6c33cd61d32953f0cc00150875dce5c7bc4 (patch) | |
tree | 73c45c32d61e0fde02af0de20b2afb1d53fd5e59 /pygments/formatters | |
parent | 29392ea678456a46d741e3f4f6e32b9e58b1c2cd (diff) | |
download | pygments-git-992fa6c33cd61d32953f0cc00150875dce5c7bc4.tar.gz |
Fix #632. (#2101)
* Fix #632.
The doc string indicates that the linenos table is wrapped in <div class="highlight">,
but the actual implementation puts the <div> inside the table cell containing the code.
This seems to cause issues as explained in #632, and given it doesn't match the
documentation, this PR restores the original behavior.
* Fix sample code in comment.
* Update CHANGES.
* Refactor the wrapping logic.
Instead of calling _wrap_div() at the end of wrap(), _wrap_div()
is now called after wrap/_wrap_tablinelinenos. This yields the
desired behavior but removes the custom <div> generation code.
Diffstat (limited to 'pygments/formatters')
-rw-r--r-- | pygments/formatters/html.py | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py index e1043b67..791f749a 100644 --- a/pygments/formatters/html.py +++ b/pygments/formatters/html.py @@ -385,7 +385,7 @@ class HtmlFormatter(Formatter): class CodeHtmlFormatter(HtmlFormatter): - def wrap(self, source, outfile): + def wrap(self, source, *, include_div): return self._wrap_code(source) def _wrap_code(self, source): @@ -707,20 +707,21 @@ class HtmlFormatter(Formatter): filename_tr = "" if self.filename: filename_tr = ( - '<tr><th colspan="2" class="filename"><div class="highlight">' - '<span class="filename">' + self.filename + '</span></div>' + '<tr><th colspan="2" class="filename">' + '<span class="filename">' + self.filename + '</span>' '</th></tr>') # in case you wonder about the seemingly redundant <div> here: since the # content in the other cell also is wrapped in a div, some browsers in # some configurations seem to mess up the formatting... - yield 0, ( - '<table class="%stable">' % self.cssclass + filename_tr + + yield 0, (f'<table class="{self.cssclass}table">' + filename_tr + '<tr><td class="linenos"><div class="linenodiv"><pre>' + - ls + '</pre></div></td><td class="code">' - ) + ls + '</pre></div></td><td class="code">') + yield 0, '<div>' yield 0, dummyoutfile.getvalue() + yield 0, '</div>' yield 0, '</td></tr></table>' + def _wrap_inlinelinenos(self, inner): # need a list of lines since we need the width of a single number :( @@ -933,16 +934,20 @@ class HtmlFormatter(Formatter): else: yield 1, value - def wrap(self, source, outfile): + def wrap(self, source): """ Wrap the ``source``, which is a generator yielding individual lines, in custom generators. See docstring for `format`. Can be overridden. """ + + output = source if self.wrapcode: - return self._wrap_div(self._wrap_pre(self._wrap_code(source))) - else: - return self._wrap_div(self._wrap_pre(source)) + output = self._wrap_code(output) + + output = self._wrap_pre(output) + + return output def format_unencoded(self, tokensource, outfile): """ @@ -973,9 +978,10 @@ class HtmlFormatter(Formatter): source = self._wrap_lineanchors(source) if self.linespans: source = self._wrap_linespans(source) - source = self.wrap(source, outfile) + source = self.wrap(source) if self.linenos == 1: source = self._wrap_tablelinenos(source) + source = self._wrap_div(source) if self.full: source = self._wrap_full(source, outfile) |