3

I am trying to take a python script and prepare it for paper printing with syntax highlighting and line numbering. I have used the following command line instruction:

pygmentize -f html -O style=colored,linenos=1 -l python -o <file>.html <file>.py

This instruction runs without errors but no matter how I indicate the style and the lexer, the output HTML never has syntax highlighting at all. It is appropriately formatted with correct indentation and line numbers, but I cannot get it to match the coloured syntax highlighting displayed when using the pygments demo, or any colours.

I have also tried different aliases of python (such as py, python3 and py3) and different styles, but the HTML is always the same. For what it's worth, I am using PowerShell on Windows 10 to run the instruction and Google Chrome to view the output HTML (though I have also checked with Edge and don't see any difference).

Jack Parkinson
  • 681
  • 11
  • 35
  • I would also like an answer to this question. I was running this on Linux, so maybe this is a pygmentize issue? For what its worth using the python module instead of the pygmentize CLI did work (so maybe its a bug in the CLI?): pygments.highlight( open(fpath, 'r').read(), lexer=pygments.lexers.get_lexer_by_name('python'), formatter=pygments.formatters.html.HtmlFormatter( noclasses=True, style=pygments.styles.get_style_by_name('default') ) ) – Erotemic Jul 13 '20 at 15:20

1 Answers1

5

While writing my above comment, I actually found the solution. I dug into the javascript code to find the actual python call to make the python implementation work. In that notice how there is a noclasses=True as a formatter option.

I have no idea what noclasses does, but if you add it to the CLI as a formatter option then the output is colored correctly:

pygmentize -l python -f html -O style=default -O noclasses=True -o <file>.html <file>.py
Erotemic
  • 4,806
  • 4
  • 39
  • 80
  • The default (without noclasses) outputs HTML elements with solely a classname so that using CSS one can apply styling to the fragments. `noclasses` changes that behaviour to write the styles inline into the HTML element definitions. – user1556435 May 20 '22 at 10:20
  • all you really need is -O noclasses=True looks like, thanks! – colin lamarre Jun 27 '23 at 10:19