1

I'm creating an Asciidoctor document with some code blocks. I'm using pygments as syntax highlighter.

In the output, trailing empty lines in a code block are removed. Normally that's fine, but in some specific case I want to include an empty line after the code in the output.

This should be possible with pygments, since the documentation states:

Currently, all lexers support these options:

stripnl: Strip leading and trailing newlines from the input (default: True)

Is it possible to change this option (i.e. to set stripnl=False) for a code block in an Asciidoctor document? If so, how?

A work-around is acceptable if there's no clean way to achieve this. I've considered inserting invisible Unicode characters so the line is not empty, but this seems to cause problems in my IDE (AsciidocFX does not seem to like some Unicode characters) and/or in one of the output formats (HTML and PDF), resulting in garbled output.

example.adoc:

:source-highlighter: pygments
:pygments-style: manni
:pygments-linenums-mode: inline

Some code block here:

```c
void example(void)
{
    printf("hello, world\n");
}

```

When compiled using asciidoctor example.adoc -o example.html, the output is rendered (roughly) like:

Some code block here:

void example(void)
{
    printf("hello, world\n");
}

I'd like to have the code block rendered as

void example(void)
{
    printf("hello, world\n");
}
                                // including this empty line here!

NB: I added the ruby tag, because Asciidoctor and Pygments are written in ruby, and it seems that the configuration of Pygments is done using ruby files as well. I have a strong feeling that the solution requires some Ruby scripts, but I'm not familiar with Ruby myself, so this is far from trivial for me.

In case it's relevant: I'm using Windows 10, Asciidoctor 2.0.17, ruby 3.0.2p107, and pygments.rb 2.3.0.

wovano
  • 4,543
  • 5
  • 22
  • 49

2 Answers2

0

Asciidoctor and Pygments are both stripping the trailing whitespace.

When Pygments is specified as the syntax highlighter, Asciidoctor appears to stop its whitespace removal. That means that you can use a pass-through macro to add a space, provided that you use Asciidoc code blocks:

:source-highlighter: pygments
:pygments-style: manni
:pygments-linenums-mode: inline

Some code block here:

[source, c, subs="macros+"]
----
void example(void)
{
    printf("hello, world\n");
}
pass:v[ ]
----
eskwayrd
  • 3,691
  • 18
  • 23
  • Hmm, I tried this, but unfortunately there's no difference. Did you test this? If so, what asciidoctor version did you use? I'm using Asciidoctor 2.0.17 (current latest version). – wovano Oct 04 '22 at 18:19
  • I did test this, using 2.0.17. I have Pygments 2.9.0 installed, so maybe the difference is there. I'm on macOS, so my Ruby is 2.5.3p105, and that could make a difference too. – eskwayrd Oct 04 '22 at 18:40
0

The solution with pass:v[] did not work for me either using the IntelliJ Asciidoctor plugin. What worked is that I inserted a

\u200F\u200F\u200E \u200E
                  ^
                  | space here

unicode character set to the end of the last line.

Peter Verhas
  • 268
  • 1
  • 6
  • Thanks, but "_I've considered inserting invisible Unicode characters so the line is not empty, but this seems to cause problems in my IDE (AsciidocFX does not seem to like some Unicode characters) and/or in one of the output formats (HTML and PDF), resulting in garbled output._" This solution gives correct output in my HTML output, but the PDF output shows `□□□ □` (probably because the font does not contain these Unicode characters). – wovano Nov 23 '22 at 17:03
  • Good to know. In my application, it did not matter because I inserted these characters programmatically from an Asciidoctor preprocessor running inside the IntelliJ Asciidoc plugin. The preprocessor's output is saved to a file and fed into the Asciidoctor renderer. The saved file, however, does not need to contain these characters. – Peter Verhas Feb 15 '23 at 14:29