Skip to content

Commit

Permalink
formatters/html: Fold newlines into spans
Browse files Browse the repository at this point in the history
Until now, the HTML formatter first ends a span,
then adds the line separator:

<div class="highlight"><pre><span></span><span class="gp gp-VirtualEnv">(venv) </span><span class="gp">user:~$ </span>ssh-keygen
<span class="go">Generating public/private rsa key pair.</span>
<span class="go">[...]</span>
</pre></div>

This is problematic because it's not possible to match the line endings
when selecting for specific classes. For example, when selecting the
"go" class and setting user-select: none, the browser will still allow
the user to select and copy the newlines between consecutive output
lines, i.e., spans of class "go".

We can improve this by folding the line separator inside the span
itself, so the output becomes:

<div class="highlight"><pre><span></span><span class="gp gp-VirtualEnv">(venv) </span><span class="gp">user:~$ </span>ssh-keygen
<span class="go">Generating public/private rsa key pair.
</span><span class="go">[...]
</span></pre></div>

This does allow the attributes for specific classes to also extend to
the newline characters.

Closes pygments#2499

Signed-off-by: Vangelis Koukis <evangelos.koukis@hpe.com>
  • Loading branch information
vkoukis committed Aug 28, 2023
1 parent 71cbc18 commit c0ef241
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion pygments/formatters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ def _format_lines(self, tokensource):
yield 1, ''.join(line)
line = []
elif part:
yield 1, ''.join((cspan, part, (cspan and '</span>'), lsep))
yield 1, ''.join((cspan, part, lsep, (cspan and '</span>')))
else:
yield 1, lsep
# for the last line
Expand Down

0 comments on commit c0ef241

Please sign in to comment.