Allow control of lexer used for syntax highlighting
See original GitHub issueI 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:
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:
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:
- Created 2 years ago
- Comments:7 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 thelanguage
of theCellInputNode
, rather than the childliteral-block
. This means this line isn’t actually doing anything (which you can confirm by commenting out that logic or settinglexer
to whatever you like - the resultantdiv
in html output always has a class ofhighlight-default
). Instead, it should be: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 currenthighlight-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!
I would like to include a few more things in the 0.4 milestone (#165 seems pretty important, also #169 should be useful).