Skip to content

Instantly share code, notes, and snippets.

@icholy
Created June 17, 2012 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icholy/2945676 to your computer and use it in GitHub Desktop.
Save icholy/2945676 to your computer and use it in GitHub Desktop.
pygments html formatter with line wrapping
# -*- coding: utf-8 -*-
"""
pygments.formatters.htmlline
~~~~~~~~~~~~~~~~~~~~~~~~
Formatter for HTML output which wraps each line in a span.
"""
__all__ = ['HtmlLineFormatter']
from pygments.formatters import HtmlFormatter
class HtmlLineFormatter(HtmlFormatter):
"""
Output as html and wrap each line in a span
"""
name = 'Html wrap lines'
aliases = ['htmlline']
def wrap(self, source, outfile):
return self._wrap_div(self._wrap_pre(self._wrap_lines(source)))
def _wrap_lines(self, source):
i = self.linenostart
for t, line in source:
if t == 1:
line = '<span id="LC%d">%s</span>' % (i, line)
i += 1
yield t, line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment