使用 Python 渲染统一差异
我有一个包含 svn Unified diff 的字符串。我的 PyGTK 应用程序需要向用户显示此差异,并且我想像其他差异工具一样渲染它,或者至少将其着色。
您有什么建议吗,外部工具、库、自定义实现......?我正在查看 http://kafka.fr.free.fr/diff2html/ 但我更喜欢使用一些库或类似的东西,因此用户不需要安装第三方应用程序。
我想稍后将其用于 git 和 Mercurial diffs。
I have a string which contains svn unified diff. My PyGTK app need to show this diff to user, and I want to render it like other diff tools do, or at least have it colorized.
Do you have something to suggest, external tool, library, custom implementation...? I was loking at http://kafka.fr.free.fr/diff2html/ but I prefer to use some library or sth like that, so users don't need to install third party apps.
I want use this for git and mercurial diffs later as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 difflib 生成差异,并 pygtkscintilla 用于语法高亮、行编号、代码折叠等。
如果您只想要语法高亮(而不是pygtkscintilla 提供的所有编辑器功能),那么您还可以查看 pygments。
You could use difflib to generate diffs, and pygtkscintilla for syntax-highlighting, line-numbering, code-folding, etc.
If you only want syntax-highlighting (as opposed to all the editor features offered by pygtkscintilla), then you could also look at pygments.
difflib.HtmlDiff
类提供了执行此操作的工具。但是,HtmlDiff
希望您传递完整的“之前”和“之后”文件,而不是从统一的 diff 文件开始。使用 svn/git/mercurial 命令可以轻松获取这些文件,而无需使用这些 VCS 的“diff”功能。The
difflib.HtmlDiff
class provides facilities for doing this. However, instead of starting with a unified diff file,HtmlDiff
wants you to pass the complete "before" and "after" files. These files are easy to get with svn/git/mercurial commands without using the "diff" functionality of those VCS.GtkSourceView 是 pygtk 的 TextView 的替代品,它可以语法突出显示差异文件,包括统一差异。
GtkSourceView is a drop in replacement for pygtk's TextView that can syntax-highlight diff files, including unified diffs.