Beispiel #1
0
    def _highlight_lines(self, tokensource):
        """
        Highlighted the lines specified in the `hl_lines` option by
        post-processing the token stream coming from `_format_lines`.
        """
        if not isinstance(self.hl_lines, dict):
            HtmlFormatter._highlight_lines(tokensource)
        else:
            hl_lines = {}
            for css, lines in self.hl_lines.items():
                hl_lines.update(dict([(line, css) for line in lines]))

            hls = hl_lines.keys()
            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
                    css = hl_lines[i + 1]
                    if css:
                        yield 1, '<span class="%s">%s</span>' % (css, value)
                    elif self.noclasses:
                        style = ''
                        if self.style.highlight_color is not None:
                            style = (' style="background-color: %s"' %
                                     (self.style.highlight_color,))
                        yield 1, '<span%s>%s</span>' % (style, value)
                    else:
                        yield 1, '<span class="hll">%s</span>' % value
                else:
                    yield 1, value