php listings with inline lexer

5 views
Skip to first unread message

giorgio ruffa

unread,
Nov 3, 2016, 2:29:39 PM11/3/16
to nikola-discuss
Hi everyone,
first of all: thanks for the great tool!

I am having a little issue:
I am including some php listings in a post using the "..listing: file.php php" restructured directive.
I want to keep only one file in the listings folder and create multiple code blocks in the generated HTML, hence I am using the start-after/end-before combo in the following way:

.. listing:: file.php php
  :start-after: //LISTING_AFTER1
  :end-before: //LISTING_BEFORE1

some restructured text talking about the first block 

.. listing:: file.php php
  :start-after: //LISTING_AFTER2
  :end-before: //LISTING_BEFORE2

some other restructured text about the second block... and so on.

It works great with all the other languages, but I'm having a little problem with php:
In the generated HTML the syntax highlight for each block is turned off.
This happens because the included php code does not contains the "<?php ... ?>"  tag.

I took a look at nikola source code, in particular at the file nikola/plugins/task/listings.py , which, I suppose, is delegated to the generation of the correct Highlight, and I saw that the pigments library is used.
The pygments' php lexer has a startinline argument (disabled by default) that prevent highlighting if the code does not start with the "<?php" tag.

Do you know if is possible to pass this argument trough any configuration or as an option of the ".. listing::" directive?


Thanks everyone in advance,
regards.

Giorgio

Roberto Alsina

unread,
Nov 3, 2016, 2:36:44 PM11/3/16
to nikola-discuss
Well, it would need to be added. Is that argument only for the php lexer?

--
You received this message because you are subscribed to the Google Groups "nikola-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nikola-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Warrick

unread,
Nov 3, 2016, 2:39:38 PM11/3/16
to Nikola—Discuss
On 3 November 2016 at 19:29, giorgio ruffa <gior...@gmail.com> wrote:
> Hi everyone,
> first of all: thanks for the great tool!

You’re welcome!

> I took a look at nikola source code, in particular at the file
> nikola/plugins/task/listings.py , which, I suppose, is delegated to the
> generation of the correct Highlight, and I saw that the pigments library is
> used.
> The pygments' php lexer has a startinline argument (disabled by default)
> that prevent highlighting if the code does not start with the "<?php" tag.
>
> Do you know if is possible to pass this argument trough any configuration or
> as an option of the ".. listing::" directive?

As Roberto said, this needs support from Nikola itself. How does this
setting affect PHP+HTML files?

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

giorgio ruffa

unread,
Nov 3, 2016, 2:59:20 PM11/3/16
to nikola-discuss, ral...@kde.org
Hi Roberto,

looking to the pygments list of lexers the PHP lexer is the only one with the startinline option, it is also one of the few with some extra options.

It seems reasonable to enable it by default other than having a dedicated option for that, but I guess you'll have to check for the file extension before (kind of like what is already done for "*.ipynb" files).
As an alternative the Lexer class has a "name" field you can use to discern the language after  the Lexer is retrieved with get_lexer_for_filename or guess_lexer (line 128/131).

Thank you,
Giorgio
Reply all
Reply to author
Forward
0 new messages