confluencenbsphinx

How to enable syntax highlighting when using nbsphinx with confluencebuilder?


When I publish the docs of a project using confluencebuilder, the part of the documentation which was generated by nbsphinx (comprising a few tutorial notebooks) does not display syntax highlighting.

I had a similar problem when publishing in ReadTheDocs (i.e. html), but there it was solved by installing IPython (and ensuring the correct lexer was available for the highlighting).

In the case of confluence, however, I am a bit lost.

Inspecting the output files I notice that codeblocks associated with Python notebook cells is set to none, e.g.

<div><ac:structured-macro ac:name="code">
<ac:parameter ac:name="language">none</ac:parameter>
<ac:parameter ac:name="linenumbers">false</ac:parameter>
<ac:parameter ac:name="title">\[33\]:</ac:parameter>
<ac:plain-text-body><![CDATA[var = 42]]></ac:plain-text-body>
</ac:structured-macro>
</div>

Is there a way to at least force nbsphinx or confluencebuilder to fall back to python and highlight the code in notebook cells?


Solution

  • The syntax highlighting can be fixed by adding the following to Sphinx's conf.py:

    def translation_override(lang):
        if lang in ['ipython', 'ipython2', 'ipython3']:
            lang = 'python'
    
        return lang
    
    confluence_lang_transform = translation_override
    

    I found the answer and the above snippet in this discussion on github.

    What is happening is that the language in the in the notebook's code blocks being processed is not being set to 'python' but to 'ipython' – which confluence does not understand. Fortunately, confluencebuilder comes with the confluence_lang_transform configuration setting, which allows one to translate one language into another, fixing the issue.