-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 output: Line numbers misalign with code lines (since sphinx 3.1.0, theme independent) #8254
Comments
It basically appears to be 7px of top and bottom padding which is missing from the The below screenshots hint that the However it would be prettier to have the two Something like this fixes it, but of course it should be fixed elsewhere
|
This is probably somewhat related to my PR #7482, but I think it is a bit more complicated. There is a rule from td.linenos pre {
padding: 0 5px 0 5px;
} If you disable this, it looks fine in the I don't know yet how to fix this, I'll try to look into it. If you find anything new, let me know! |
FYI, starting with Sphinx 4, So we should probably concentrate on fixing the |
OK, it looks like on top of my changes I think this is a bug in Pygments ( As a work-around, you can use Pygments 2.6.1, which is the last working version:
Again, this leads to some strange horizontal spacing in the Since |
@mgeier Good to know about the table vs. inline style in Sphinx 4 and Sphinx 6. A table indeed seems like an outdated way of doing the line numbers. I'll look into the inline style/method. Is Sphinx using the raw pygments html output (i.e. pygments is generating the table with line numbers)? I might well change theme on my project, but as you also mentioned yourself the issue isn't isolated to alabaster. |
I don't know, but let's look at the source code, shall we? It looks like the HTML is generated here: sphinx/sphinx/writers/html5.py Lines 403 to 409 in 22820d8
And here for the old HTML writer: Lines 451 to 457 in 22820d8
The sphinx/sphinx/builders/html/__init__.py Line 272 in 0476e1c
Which leads us to the call to Pygments: Line 140 in 0476e1c
This is using So I guess the answer is "yes", the HTML is generated by Pygments and Sphinx just includes it as it is. But there's also CSS, which I think is actually the source of the problem. The CSS comes from sphinx/sphinx/builders/html/__init__.py Lines 732 to 733 in 0476e1c
This uses the same Line 163 in 0476e1c
I hope this helps!
That's true, but I was talking about the work-around of using |
I've created a Pygments issue: pygments/pygments#1579. |
@mgeier Cheers, it was enlightening, I didn't mean to force you to look at the source (I could have done that myself), I meant to ask you if you knew the answer off the top of your head. Also, fine work on the pygments issue. My personal interest in this is temporarily quite low, since I might change theme from alabaster and the HTML5 formatter (with the inline option/solution) might not suffer from the issue. So I might keep searching for more than a work-around, I might not, for now..... |
Just for my understanding: It works with Pygments 2.6 and 2.7 in Sphinx 3.0, but it's broken with Pygments 2.7 in 3.1 -- however Pygments 2.6 works in 3.1? |
That's a good question, but the answer isn't quite as straightforward. Most likely all combinations are broken.
Well, not quite, that would be too easy. In Sphinx 3.0 (with Pygments 2.6), the main code block doesn't extend to the right margin. It's just as wide as the contained code. That's one of the problems I wanted to fix with #7482. Switching to Pygments 2.7 (but staying with Sphinx 3.0) changes two things (AFAICT), one might say they are "broken":
The vertical alignment of line numbers seems to be fine with both Pygments 2.6 and Pygments 2.7.
Yes. To be a bit more concrete, the vertical alignment of the line number is now off (that's what this issue is all about).
Pretty much. I tried to see what's going on ... The good news: the HTML is unchanged between versions: <div class="highlight-python notranslate"><table class="highlighttable"><tbody><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">math</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">10</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
</pre></div>
</td></tr></tbody></table></div> The CSS does change indeed: In Sphinx 3.0, table.highlighttable td {
padding: 0 0.5em 0 0.5em;
}
td.linenos pre {
padding: 5px 0px;
border: 0;
background-color: transparent;
color: #aaa;
} The vertical In Sphinx 3.1, table.highlighttable tr {
display: flex;
}
table.highlighttable td.linenos {
padding-right: 0.5em;
}
table.highlighttable td {
margin: 0;
padding: 0;
}
td > :last-child {
margin-bottom: 0px;
}
td > :first-child {
margin-top: 0px;
}
div.highlight pre, table.highlighttable pre {
margin: 0;
}
td.linenos pre {
border: 0;
background-color: transparent;
color: #aaa;
} I hope I didn't miss some relevant parts. Most notably, The change from Sphinx 3.0 to 3.1 doesn't break the vertical alignment because there is no Pygments 2.6 doesn't affect the But that's exactly what Pygments 2.7 does by introducing this CSS: td.linenos pre {
color: #000000;
background-color: #f0f0f0;
padding: 0 5px 0 5px;
} This overrides most settings from In Sphinx 3.0, where the table cells are still vertically centered, this isn't really noticed (but that doesn't make it correct). I guess we could align the So, in summary, all combinations are broken, where Pygments 2.6 and Sphinx 3.1 is least broken, only affecting the |
There is a speculative fix in Pygments (see the corresponding bug there for details), if that does work for you, I'll get a 2.7.2 out quickly. Please let me know. Pygments does control the line numbers though, as they're part of the Pygments output. If I read your comment correctly, Sphinx could put the original td.linenos pre {
padding: 5px 0px;
border: 0;
background-color: transparent;
color: #aaa;
} rule back? That should fix it. In any case, I don't see Pygments stopping emitting CSS rules for linenos, as it's required for Pygments itself, but clearly we should strive to make this work for both sides. I hope the patch is sufficient. |
It should definitely not put For the other stuff, this is already there: sphinx/sphinx/themes/basic/static/basic.css_t Lines 711 to 715 in 0476e1c
Sadly, this gets overruled by the Pygments settings. I guess this is because I'm not sure what's the correct solution here ... Probably Sphinx should load |
Yep. The Sphinx rules should come last, or alternatively, use |
I've created a PR for this: #8333. |
Pygments 2.7.2 has been released. |
Describe the bug
In Sphinx 3.1.0 and later versions the line numbers of code blocks misalign with the code lines they number, as shown in the screenshots below. Version 3.0.4 aligns them properly like older versions 2.x and 1.x. Tested for both the alabaster and classic theme.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The line numbers and code lines should align as in the second screenshot (from sphinx v. 3.0.4).
Your project
A MWE is attached (
make html
with sphinx v. 3.1.0 to see the problem)lineno-misalign-bug.zip
Screenshots
Environment info
The text was updated successfully, but these errors were encountered: