question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow control of lexer used for syntax highlighting

See original GitHub issue

I use jupyter-sphinx to show examples using Python to execute an application-specific language:


.. jupyter-execute:

    # Parse a simple model.
    model.parse(
        """
        l l1 P=10
        s s1 l1.p1 m1.p1
        m m1 R=0.995 T=1-&m1.R
        s s2 m1.p2 m2.p1 L=10
        m m2 R=0.9999 T=1-&m2.R
        pd pd1 m2.p1.i
        pd pd2 m2.p2.o
        """
    )

This gets rendered like this:

image

I’ve got a pygments syntax highlighter that can handle highlighting our special syntax inside the string, inside Python code. I would like to change the lexer type used by Sphinx for the cell that gets compiled. This would make it render like this:

image

Currently the lexer used by jupyter-sphinx is hard-coded here. Is it possible to change this on a per-cell basis?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
philj56commented, Apr 23, 2021

Hi @akhmerov, I’m on the same team as @SeanDS. Thanks for the help! I’ve got things working locally doing it this way, but there seemed to be a few issues in jupyter-sphinx that stop it from working at first.

Firstly, the language attribute is set on the wrong node in execute.py, as it currently sets the language of the CellInputNode, rather than the child literal-block . This means this line isn’t actually doing anything (which you can confirm by commenting out that logic or setting lexer to whatever you like - the resultant div in html output always has a class of highlight-default). Instead, it should be:

source = node.children[0].children[0]

Fixing this then causes syntax highlighting to break, as the line here defines the wrong name - it should be "ipython3" (as per the definition in the ipython package)

After fixing both of these the highlighting works as intended, and for example in the html output, normal jupyter-execute blocks now have a class of highlight-ipython3 rather than the current highlight-default, and our custom highlighting works if I change the kernel.

Hopefully this makes sense and I’ve understood what’s going on. I can open a pull request if you want, but it’s only a two line change. Thanks again for the help!

1reaction
akhmerovcommented, Apr 28, 2021

I would like to include a few more things in the 0.4 milestone (#165 seems pretty important, also #169 should be useful).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lexical analyzers and syntax-highlighters in JavaScript
For maximum control, the highlighters wrap span tags around elements in the source code, and give them class names indicating to which lexical...
Read more >
Adding a custom lexer with syntax highlighting and folding to a ...
The STC handles text and uses a lexer to add syntax highlighting to this text. There are a lot of internal lexers for...
Read more >
Creating a lexer for syntax highlighting with Pygments
Pygments is basically a syntax highlighting library written in Python and developed by the pocoo team . I'd define the core functionality of ......
Read more >
Available lexers - Pygments
This allows users to highlight JSON as it is used in the wild. No validation is performed on the input JSON document. New...
Read more >
Semantic Highlight Guide | Visual Studio Code Extension API
Semantic highlighting is an addition to syntax highlighting as described in the Syntax Highlight guide. Visual Studio Code uses TextMate grammars as the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found