pygments / pygments/pygments

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

Open
#2,499 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.2k
Forks
885
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the console lexer, the HTML formatter, and the example in tests/snippets/console/prompt_in_output.txt. Trace how Generic.Output line separators are emitted and verify the generated HTML keeps each trailing newline inside its go span, as shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.