1

I'm currently writing an application that uses Pygments to perform syntax highlighting. The problem I'm having is any code I process with Pygments has the leading and trailing whitespace in the file removed, and a single line break added to the end. Is there a way to make Pygments preserve the whitespace?

wovano
  • 4,543
  • 5
  • 22
  • 49
cpaul37
  • 23
  • 3

1 Answers1

3

I think you want to make your own lexer, e.g.

lexer = lexers.get_lexer_by_name("python", stripnl=False)

and explicitly pass it to pygment.highlight. See the lexers' reference here.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395