Arash Sarshar
Arash Sarshar

Reputation: 11

Source code highlighting with pygments and html output

I am using Pygments as a source highlighter for documenting a C++ project. Both Python 2.7.x and Pygments are their latest versions. I am having trouble getting a highlighted html output for my source code when I run pygments using command line:

pygmentize  -f html -o a.html test1.cpp

The result is a colorless html output. Curiously, running the same command with Rich Text File format results in a colored rtf file:

pygmentize  -f rtf -o a.rtf test1.cpp

I tried a simpler C code and the html output was highlighted correctly. Any ideas why this is happening? The sample code I am having trouble with is here

Upvotes: 1

Views: 1827

Answers (2)

HakierGrzonzo
HakierGrzonzo

Reputation: 1

pygmentize does not output any css in that html file, it only adds classes to html elements. For example:

<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;SFML/Graphics/RenderTarget.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;SFML/Graphics/RenderTexture.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;SFML/Graphics/RenderWindow.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;SFML/System/Vector2.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;SFML/Window/ContextSettings.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;SFML/Window/Event.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;SFML/Window/Keyboard.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;SFML/Window/VideoMode.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;SFML/Window/WindowStyle.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;box2d/b2_math.h&gt;</span><span class="cp"></span>

As you can see, you can write a simple css that looks like this:

.cp {
    color: green;
}

And all preprocessor calls will be green. Repeat this for all the class values you will see (maybe use italics for comments etc.)

Upvotes: 0

MarkSchoonover
MarkSchoonover

Reputation: 198

I've noticed the same thing with C++. Since you know what language to highlight ahead of time, try this:

pygmentize -N test1.CPP

This will tell you which lexer pygmentize will use. It should be CppLexer but if not then tell pygmentize to use the CppLexer lexer:

pygmentize -f html -o a.html -l CppLexer test1.cpp

I know this was asked awhile ago and not surprising the sample code you posted is now gone therefore I can't test it.

Upvotes: 1

Related Questions