Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML formatter doesn't include trailing newlines in spans of class "go", Generic.Output #2499

Open
vkoukis opened this issue Aug 28, 2023 · 0 comments · May be fixed by #2501
Open

HTML formatter doesn't include trailing newlines in spans of class "go", Generic.Output #2499

vkoukis opened this issue Aug 28, 2023 · 0 comments · May be fixed by #2501

Comments

@vkoukis
Copy link

vkoukis commented Aug 28, 2023

Lexer "console" assigns class go: Generic.Output to lines that correspond to the output of commands.
For example

$ grep Generic.Output tests/snippets/console/prompt_in_output.txt
'line1\n'     Generic.Output
'> file content, not prompt!\n' Generic.Output

However, the HTML formatter first ends a span of class go, 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 even though one can assign CSS properties to the go class, they will not have an effect on the actual line changes. For example, even when I assign user-select: none to class go, the user can still select and copy stray newlines between consecutive output lines, i.e., between consecutive spans.

Given that the lexer originally assigns Generic.Output to the whole line, including the newline character at the end, we can fix this by extending the line separator into 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 class go to also extend to the newline characters, and makes things more predictable.

Looking forward to your feedback, I can follow up with a PR that implements this behavior.

vkoukis added a commit to vkoukis/pygments that referenced this issue Aug 28, 2023
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>
@vkoukis vkoukis linked a pull request Aug 28, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant