PyGTK中的Pygments
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyGTK中的Pygments相关的知识,希望对你有一定的参考价值。
How to use pygments in PyGTK textview
#!/usr/bin/env python # -*- coding: utf-8 -*- """ GTKPygments ~~~~~~~~~~~ proof of concept pygments to gtk widget renderer :copyright: 2007 by Armin Ronacher. :license: GNU GPL. """ import pygtk pygtk.require('2.0') import gtk import pango from pygments.lexers import PythonLexer from pygments.styles.colorful import ColorfulStyle STYLE = ColorfulStyle f = file(__file__) try: SOURCE = f.read() finally: f.close() class GTKPygments(gtk.Window): def __init__(self): super(GTKPygments, self).__init__() self.set_title('GTK Pygments') win = gtk.ScrolledWindow() self.add(win) self.textview = gtk.TextView() win.add(self.textview) buf = gtk.TextBuffer() styles = {} for token, value in PythonLexer().get_tokens(SOURCE): while not STYLE.styles_token(token) and token.parent: token = token.parent if token not in styles: styles[token] = buf.create_tag() start = buf.get_end_iter() buf.insert_with_tags(start, value.encode('utf-8'), styles[token]) for token, tag in styles.iteritems(): style = STYLE.style_for_token(token) if style['bgcolor']: tag.set_property('background', '#' + style['bgcolor']) if style['color']: tag.set_property('foreground', '#' + style['color']) if style['bold']: tag.set_property('weight', pango.WEIGHT_BOLD) if style['italic']: tag.set_property('style', pango.STYLE_ITALIC) if style['underline']: tag.set_property('underline', pango.UNDERLINE_SINGLE) self.connect('delete-event', lambda *a: gtk.main_quit()) self.textview.set_buffer(buf) self.textview.set_editable(False) self.textview.modify_font(pango.FontDescription('monospace')) self.resize(800, 500) self.show_all() def run(self): gtk.main() if __name__ == '__main__': GTKPygments().run()
以上是关于PyGTK中的Pygments的主要内容,如果未能解决你的问题,请参考以下文章
Pygments 代码中的行号在 Windows 上的 xampp 中突出显示
使用 Pygments 在 Jekyll 中突出显示围栏代码块