Georg Brandl avatar Georg Brandl committed 244fb6f

Added `hl_lines` and `hl_color` options to image formatter (#457).

Comments (0)

Files changed (2)

 
 - Added `line_number_start` option to image formatter (#456).
 
+- Added `hl_lines` and `hl_color` options to image formatter (#457).
+
 - Fixed the HtmlFormatter's handling of noclasses=True to not output any
   classes (#427).
 

pygments/formatters/img.py

 from commands import getstatusoutput
 
 from pygments.formatter import Formatter
-from pygments.util import get_bool_opt, get_int_opt, get_choice_opt
+from pygments.util import get_bool_opt, get_int_opt, \
+     get_list_opt, get_choice_opt
 
 # Import this carefully
 try:
         the source code area.
 
         Default: 6
+
+    `hl_lines`
+        Specify a list of lines to be highlighted.  *New in Pygments 1.2.*
+
+        Default: empty list
+
+    `hl_color`
+        Specify the color for highlighting lines.  *New in Pygments 1.2.*
+
+        Default: highlight color of the selected style
     """
 
     # Required by the pygments mapper
                                    self.line_number_pad * 2)
         else:
             self.line_number_width = 0
+        self.hl_lines = []
+        hl_lines_str = get_list_opt(options, 'hl_lines', [])
+        for line in hl_lines_str:
+            try:
+                self.hl_lines.append(int(line))
+            except ValueError:
+                pass
+        self.hl_color = options.get('hl_color',
+                                    self.style.highlight_color) or '#f90'
         self.drawables = []
 
     def get_style_defs(self, arg=''):
         )
         self._paint_line_number_bg(im)
         draw = ImageDraw.Draw(im)
+        # Highlight
+        if self.hl_lines:
+            x = self.image_pad + self.line_number_width - self.line_number_pad + 1
+            recth = self._get_line_height()
+            rectw = im.size[0] - x
+            for linenumber in self.hl_lines:
+                y = self._get_line_y(linenumber - 1)
+                draw.rectangle([(x, y), (x + rectw, y + recth)],
+                               fill=self.hl_color)
         for pos, value, font, kw in self.drawables:
             draw.text(pos, value, font=font, **kw)
         im.save(outfile, self.image_format.upper())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.