Beispiel #1
0
 def _generate(self, language, content, context=None):
     lexer_name = self._lexer_alias_to_name(language)
     lexer_options = {'stripnl': False}
     lexer_options.update(self._lexer_options.get(lexer_name, {}))
     if context:
         lexer_options.update(context.get_hint('lexer_options', {}))
     lexer = get_lexer_by_name(lexer_name, **lexer_options)
     out = io.StringIO()
     # Specify `lineseparator` to workaround exception with Pygments 2.2.0:
     # "TypeError: unicode argument expected, got 'str'" with newline input
     formatter = HtmlFormatter(nowrap=True, lineseparator=u'\n')
     formatter.format(lexer.get_tokens(content), out)
     return Markup(out.getvalue())
Beispiel #2
0
def get_classes_by_start(code):
    """Returns a dictionary mapping positions in the code to html equivalent to what Pygments would
    insert prior to that position."""
    lexer = Python3Lexer()
    formatter = HtmlFormatter()

    # pygments seems to ignore initial newlines (etc.?), so to line up with all our other stuff,
    # we need to shift the starting position by that much
    start_position = len(code) - len(code.lstrip())
    parser = MyHTMLParser(start_position=start_position)

    tokens = lexer.get_tokens(code.lstrip())
    pygments_html = io.StringIO()
    formatter.format(tokens, pygments_html)
    pygments_html.seek(0)
    html = pygments_html.read()
    parser.feed(html)

    # not really sure why these are different by 1, but let's make sure we find out about it if
    # that changes
    assert parser.current_position == len(
        code) + 1, 'code is length {}, parser ended up at {}'.format(
            len(code), parser.current_position)
    return parser.start_classes
Beispiel #3
0
	def format(self, tokensource, outfile):
		new_tokensource = self._my_format(tokensource, outfile)
		HtmlFormatter.format(self, new_tokensource, outfile)