简体   繁体   English

使用Python渲染统一差异

[英]Render Unified Diff with Python

I have a string which contains svn unified diff. 我有一个包含svn统一差异的字符串。 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. 我的PyGTK应用程序需要向用户显示此差异,并且我想像其他差异工具一样呈现它,或者至少将其着色。

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. 我在http://kafka.fr.free.fr/diff2html/上找东西,但是我更喜欢使用一些类似的库或其他东西,因此用户不需要安装第三方应用程序。

I want use this for git and mercurial diffs later as well. 我也想稍后将其用于git和水银差异。

You could use difflib to generate diffs, and pygtkscintilla for syntax-highlighting, line-numbering, code-folding, etc. 你可以使用difflib产生差异列表,并pygtkscintilla的语法高亮,线路编号,代码折叠等。

If you only want syntax-highlighting (as opposed to all the editor features offered by pygtkscintilla), then you could also look at pygments . 如果您只希望语法高亮(与pygtkscintilla提供的所有编辑器功能相反),那么您也可以查看pygments

The difflib.HtmlDiff class provides facilities for doing this. difflib.HtmlDiff类提供了执行此操作的工具。 However, instead of starting with a unified diff file, HtmlDiff wants you to pass the complete "before" and "after" files. 但是,不是从统一的diff文件开始, HtmlDiff希望您传递完整的“ before”和“ after”文件。 These files are easy to get with svn/git/mercurial commands without using the "diff" functionality of those VCS. 使用svn / git / mercurial命令可以轻松获得这些文件,而无需使用这些VCS的“ diff”功能。

GtkSourceView是pygtk的TextView的替代品,它可以语法突出显示差异文件,包括统一差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM