Commits

gbrandl  committed 623c995

Add hl_lines option to HTML formatter. #325.

  • Participants
  • Parent commits 9db0586
  • Branches trunk

Comments (0)

Files changed (6)

   and it's documented that tokenization with this lexer may
   raise exceptions.
 
+- New option ``hl_lines`` to HTML formatter, to highlight certain
+  lines.
+
 - New option ``prestyles`` to HTML formatter.
 
 - New option *-g* to pygmentize, to allow lexer guessing based

File pygments/formatters/html.py

 
     Formatter for HTML output.
 
-    :copyright: 2006-2007 by Georg Brandl, Armin Ronacher.
+    :copyright: 2006-2008 by Georg Brandl, Armin Ronacher.
     :license: BSD, see LICENSE for more details.
 """
 import sys, os
 import StringIO
 
+try:
+    set
+except NameError:
+    from sets import Set as set
+
 from pygments.formatter import Formatter
 from pygments.token import Token, Text, STANDARD_TYPES
-from pygments.util import get_bool_opt, get_int_opt
+from pygments.util import get_bool_opt, get_int_opt, get_list_opt
 
 
 __all__ = ['HtmlFormatter']
 
     Wrapping can be disabled using the `nowrap` option.
 
+    A list of lines can be specified using the `hl_lines` option to make these
+    lines highlighted (as of Pygments 0.11).
+
     With the `full` option, a complete HTML 4 document is output, including
     the style definitions inside a ``<style>`` tag, or in a separate file if
     the `cssfile` option is given.
         CSS property (you get the default line spacing with ``line-height:
         125%``).
 
+    `hl_lines`
+        Specify a list of lines to be highlighted.  *New in Pygments 0.11.*
+
     `linenostart`
         The line number for the first line (default: ``1``).
 
         self.nobackground = get_bool_opt(options, 'nobackground', False)
         self.lineseparator = options.get('lineseparator', '\n')
         self.lineanchors = options.get('lineanchors', '')
+        self.hl_lines = set()
+        for lineno in get_list_opt(options, 'hl_lines', []):
+            try:
+                self.hl_lines.add(int(lineno))
+            except ValueError:
+                pass
 
         self._class_cache = {}
         self._create_stylesheet()
                 text_style = ' ' + self.class2style[self.ttype2class[Text]][0]
             lines.insert(0, '%s { background: %s;%s }' %
                          (prefix(''), self.style.background_color, text_style))
+        if self.style.highlight_color is not None:
+            lines.insert(0, '%s.hll { background-color: %s }' %
+                         (prefix(''), self.style.highlight_color))
         return '\n'.join(lines)
 
     def _wrap_full(self, inner, outfile):
         if line:
             yield 1, line + (lspan and '</span>') + lsep
 
+    def _highlight_lines(self, tokensource):
+        """
+        Highlighted the lines specified in the `hl_lines` option by
+        post-processing the token stream coming from `_format_lines`.
+        """
+        hls = self.hl_lines
+
+        for i, (t, value) in enumerate(tokensource):
+            if t != 1:
+                yield t, value
+            if i + 1 in hls: # i + 1 because Python indexes start at 0
+                yield 1, '<span class="hll">%s</span>' % value
+            else:
+                yield 1, value
+
     def wrap(self, source, outfile):
         """
         Wrap the ``source``, which is a generator yielding
         linewise, e.g. line number generators.
         """
         source = self._format_lines(tokensource)
+        if self.hl_lines:
+            source = self._highlight_lines(source)
         if not self.nowrap:
             if self.linenos == 2:
                 source = self._wrap_inlinelinenos(source)

File pygments/style.py

     #: overall background color (``None`` means transparent)
     background_color = '#ffffff'
 
+    #: highlight background color
+    highlight_color = '#ffffcc'
+
     #: Style definitions for individual token types.
     styles = {}

File pygments/styles/fruity.py

     """
 
     background_color = '#111111'
+    highlight_color = '#333333'
 
     styles = {
         Whitespace:         '#888888',

File pygments/styles/native.py

     """
 
     background_color = '#202020'
+    highlight_color = '#404040'
 
     styles = {
         Token:              '#d0d0d0',

File pygments/styles/vim.py

     """
 
     background_color = "#000000"
+    highlight_color = "#222222"
     default_style = "#cccccc"
 
     styles = {