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

Support anchored line numbers in inline mode #1591

Merged
merged 2 commits into from
Feb 6, 2021
Merged

Support anchored line numbers in inline mode #1591

merged 2 commits into from
Feb 6, 2021

Conversation

kdeldycke
Copy link
Contributor

@kdeldycke kdeldycke commented Nov 4, 2020

In table mode, line numbers can be anchored. See how each line anchor (in the form<a name="L-1"></a>) gets their corresponding <a href="#L-1"> 1</a> link tied to their line number:

$ pygmentize -f html -O linenos=table,linespans=coderow,lineanchors=L,anchorlinenos=True,wrapcode=True ./dummy.py
<table class="highlighttable">
    <tr>
        <td class="linenos">
            <div class="linenodiv">
                <pre>
                    <a href="#L-1"> 1</a>
                    <a href="#L-2"> 2</a>
                    (...)
                </pre>
            </div>
        </td>
        <td class="code">
            <div class="highlight">
                <pre>
                    <span></span>
                    <code>
                        <span id="coderow-1">
                            <a name="L-1"></a>
                            <span class="ch">#!/usr/bin/env python</span>
                         </span>
                         <span id="coderow-2">
                             <a name="L-2"></a>
                         </span>
                         (...)
                    </code>
                </pre>
            </div>
        </td>
    </tr>
</table>

This is perfect. But does not work in inline mode, for which no link are prodduced at all:

$ pygmentize -f html -O linenos=inline,linespans=coderow,lineanchors=L,anchorlinenos=True,wrapcode=True ./dummy.py
<div class="highlight">
    <pre>
        <span></span>
        <code>
            <span id="coderow-1">
                <a name="L-1"></a>
                <span class="linenos"> 1</span>
                <span class="ch">#!/usr/bin/env python</span>
            </span>
            <span id="coderow-2">
                <a name="L-2"></a>
                <span class="linenos"> 2</span>
            </span>
            (...)
        </code>
    </pre>
</div>

This PR fixes this difference by generating proper line number link to their anchor in inline, as Pygments does in table mode:

$ pygmentize -f html -O linenos=inline,linespans=coderow,lineanchors=L,anchorlinenos=True,wrapcode=True ./dummy.py
<div class="highlight">
    <pre>
        <span></span>
        <code>
            <span id="coderow-1">
                <a name="L-1"></a>
                <a href="#L-1">
                    <span class="linenos"> 1</span>
                </a>
                <span class="ch">#!/usr/bin/env python</span>
            </span>
            <span id="coderow-2">
                <a name="L-2"></a>
                <a href="#L-2">
                    <span class="linenos"> 2</span>
                </a>
                (...)
            </code>
    </pre>
</div>

@Anteru Anteru added this to the 2.8 milestone Feb 6, 2021
@Anteru Anteru merged commit 8e62673 into pygments:master Feb 6, 2021
@Anteru Anteru self-assigned this Feb 6, 2021
@Anteru Anteru added the changelog-update Items which need to get mentioned in the changelog label Feb 6, 2021
@kdeldycke
Copy link
Contributor Author

Thanks @Anteru for the merge! 😍

@kdeldycke kdeldycke deleted the linenos-anchors-wrap branch February 6, 2021 15:49
@Anteru
Copy link
Collaborator

Anteru commented Feb 6, 2021

Thanks for the contribution!

@Anteru Anteru removed the changelog-update Items which need to get mentioned in the changelog label Feb 14, 2021
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 this pull request may close these issues.

None yet

2 participants