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

Allow control of lexer used for syntax highlighting #180

Closed
SeanDS opened this issue Apr 22, 2021 · 7 comments
Closed

Allow control of lexer used for syntax highlighting #180

SeanDS opened this issue Apr 22, 2021 · 7 comments
Labels

Comments

@SeanDS
Copy link

SeanDS commented Apr 22, 2021

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?

@akhmerov
Copy link
Member

AFAIR the lexer isn't hardcoded, the line you refer to is just providing the custom ipython lexer to sphinx.

jupyter-sphinx infers the lexer from kernel metadata. Therefore, I think, you need to register a custom ipython kernel (kernels already have a pygments lexer metadata entry) that executes your code, and then also add your lexer to sphinx.

Does that sound like a reasonable approach?

@philj56
Copy link
Contributor

philj56 commented 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!

@akhmerov
Copy link
Member

@philj56 thanks for following through! Your description sounds correct, and I'd really appreciate a PR with the fixes.

@philj56
Copy link
Contributor

philj56 commented Apr 23, 2021

No problem, it's #181

@akhmerov
Copy link
Member

@philj56 @SeanDS I think this issue may probably be closed now, is that right?

@SeanDS
Copy link
Author

SeanDS commented Apr 23, 2021

Looks like it! Thanks.

EDIT: any idea when the next release will be? Keen to use @philj56's fix!

@akhmerov
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants