Use code highlighting in html generated from a plugin [publications_list]

10 views
Skip to first unread message

Adriaan Rol

unread,
Mar 25, 2020, 5:47:21 AM3/25/20
to nikola-discuss
I am using the plugin publication_list to generate a publication list for my personal website. One of the features is that it shows a foldable block of bibtex code. However, for this it does not use syntax highlighting. 

I can use manual syntax highlighting in a regular (.rst) post using the `.. code:: bibtex` snippet. (see first screenshot). The Nikola .rst compilation takes care of proper code-highlighting. However, when I use the plugin, the html is created using the `publication_list.py`. I can easily change this html to for example change the class to use the "code bibtex" class. This ensures I use the right .css but it does not use the proper highlighting (as this requires pygments and other details that Nikola normally takes care of. 

Is there a way to invoke the Nikola code highlighting from the python in the plugin so that I can use syntax highlighting in the code generated html? 

Best, 

Adriaan 


Screenshot 1. Result in the publications page: 

Screenshot_2020-03-25 10.45.41_grVrGE.png


Screenshot 2. Desired syntax highlighting: 

Screenshot_2020-03-25 10.45.50_t4QHLV.png


Chris Warrick

unread,
Mar 25, 2020, 5:56:34 AM3/25/20
to Nikola—Discuss
On Wed, 25 Mar 2020 at 10:47, Adriaan Rol <adria...@gmail.com> wrote:
>
> I am using the plugin publication_list to generate a publication list for my personal website. One of the features is that it shows a foldable block of bibtex code. However, for this it does not use syntax highlighting.
>
> I can use manual syntax highlighting in a regular (.rst) post using the `.. code:: bibtex` snippet. (see first screenshot). The Nikola .rst compilation takes care of proper code-highlighting. However, when I use the plugin, the html is created using the `publication_list.py`. I can easily change this html to for example change the class to use the "code bibtex" class. This ensures I use the right .css but it does not use the proper highlighting (as this requires pygments and other details that Nikola normally takes care of.
>
> Is there a way to invoke the Nikola code highlighting from the python in the plugin so that I can use syntax highlighting in the code generated html?

Syntax highlighting is done by Nikola in Python, not in JS, so adding
CSS classes will have no effect. You would need to modify the plugin
so that it calls either the reST thing to generate code blocks, or use
pygments + NikolaPygmentsHTML directly, and put the pygments output in
the plugin’s output files.

--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16

Adriaan Rol

unread,
Mar 25, 2020, 6:16:23 AM3/25/20
to nikola-discuss
Hi Chris, 

Thanks for the quick reply. I know that simply changing the CSS does not work and that I need to invoke either the reST thing (or the pygments directly). My question is how do I invoke the reST thing from with in the plugin code. 

Thanks again for your help, 

Adriaan 

Chris Warrick

unread,
Mar 25, 2020, 7:48:48 AM3/25/20
to Nikola—Discuss
On Wed, 25 Mar 2020 at 11:16, Adriaan Rol <adria...@gmail.com> wrote:
>
> Hi Chris,
>
> Thanks for the quick reply. I know that simply changing the CSS does not work and that I need to invoke either the reST thing (or the pygments directly). My question is how do I invoke the reST thing from with in the plugin code.

Look at nikola/plugins/compile/rest/listing.py. You might be able to
re-use our CodeBlock class if you know how to invoke reST directives
manually, or you could just use pygments directly (with
pygments.highlight(code, lexer, formatter), consult that code for what
the three arguments are).

Adriaan Rol

unread,
Mar 25, 2020, 7:57:03 AM3/25/20
to nikola-discuss
Thanks, this was exactly what I was looking for. I'll probably be able to work out the details from here. Thank you for the help. 
Reply all
Reply to author
Forward
0 new messages