Ejemplo n.º 1
0
    def PrintHighlighted(self, out):
        from pygments import lexers
        from pygments import formatters
        from pygments import highlight

        # unescape before passing to pygments, which will escape
        code = html.ToText(self.s, self.start_pos, self.end_pos)

        lexer = lexers.get_lexer_by_name(self.lang)
        formatter = formatters.HtmlFormatter()

        highlighted = highlight(code, lexer, formatter)
        out.Print(highlighted)
Ejemplo n.º 2
0
def HelpTopics(s):
    """
  Given an HTML page, yield groups (id, desc, block of text)
  """
    tag_lexer = html.TagLexer(s)

    pos = 0
    it = html.ValidTokens(s)
    while True:
        try:
            tok_id, end_pos = next(it)
        except StopIteration:
            break

        if tok_id == html.StartTag:
            tag_lexer.Reset(pos, end_pos)
            #log('%r', tag_lexer.TagString())
            #log('%r', tag_lexer.TagName())

            # Capture <h2 id="foo"> first
            if tag_lexer.TagName() == 'h2':
                h2_start_right = end_pos

                open_tag_right = end_pos
                group_topic_id = tag_lexer.GetAttr('id')
                assert group_topic_id, 'Expected id= in %r' % tag_lexer.TagString(
                )

                h2_end_left, _ = html.ReadUntilEndTag(it, tag_lexer, 'h2')

                anchor_html = s[h2_start_right:h2_end_left]
                paren_pos = anchor_html.find('(')
                assert paren_pos != -1, anchor_html

                group_name = anchor_html[:paren_pos].strip()

                # Now find the <code></code> span
                _, code_start_right = html.ReadUntilStartTag(
                    it, tag_lexer, 'code')
                css_class = tag_lexer.GetAttr('class')
                assert css_class in (
                    'language-oil-help-topics',
                    'language-osh-help-topics'), tag_lexer.TagString()

                code_end_left, _ = html.ReadUntilEndTag(it, tag_lexer, 'code')

                text = html.ToText(s, code_start_right, code_end_left)
                yield group_topic_id, group_name, text

        pos = end_pos
Ejemplo n.º 3
0
    def PrintHighlighted(self, out):
        try:
            from pygments import lexers
            from pygments import formatters
            from pygments import highlight
        except ImportError:
            log("Warning: Couldn't import pygments, so skipping syntax highlighting"
                )
            return

        # unescape before passing to pygments, which will escape
        code = html.ToText(self.s, self.start_pos, self.end_pos)

        lexer = lexers.get_lexer_by_name(self.lang)
        formatter = formatters.HtmlFormatter()

        highlighted = highlight(code, lexer, formatter)
        out.Print(highlighted)