开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

stone1116/jsdifflib

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (2)
master
gh-pages
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
贡献代码
同步代码
对比差异 通过 Pull Request 同步
同步更新到分支
通过 Pull Request 同步
将会在向当前分支创建一个 Pull
Request,合入后将完成同步
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Introduction

If you do find jsdifflib useful, please support my open source work via a bitcoin donation/tip to 19qCqZxAdRF4eZfyZD2GQnAWk2Mz7DZZVf. Thanks!

Overview

jsdifflib is a Javascript library that provides:

  1. a partial reimplementation of Python’s difflib module (specifically, the SequenceMatcher class)

  2. a visual diff view generator, that offers side-by-side as well as inline formatting of file data

Yes, I ripped off the formatting of the diff view from the Trac project. It’s a near-ideal presentation of diff data as far as I’m concerned. If you don’t agree, you can hack the CSS to your heart’s content.

jsdifflib does not require jQuery or any other Javascript library.

Python Interoperability

The main reason why I reimplemented Python’s difflib module in Javascript to serve as the algorithmic basis for jsdifflib was that I didn’t want to mess with the actual diff algorithm — I wanted to concentrate on getting the in-browser view right. However, because jsdifflib’s API matches Python’s difflib’s SequenceMatcher class in its entirety, it’s trivial to do the actual diffing on the server-side, using Python, and pipe the results of that diff calculation to your in-browser diff view. So, you have the choice of doing everything in Javascript on the browser, or falling back to server-side diff processing if you are diffing really large files.

Most of the time, we do the latter, simply because while jsdifflib is pretty fast all by itself, and is totally usable for diffing "normal" files (i.e. fewer than 100K lines or so), we regularly need to diff files that are 1 or 2 orders of magnitude larger than that. For that, server-side diffing is a necessity.

Demo & Examples

You can give jsdifflib a try without downloading anything. Just click the link below, put some content to be diffed in the two textboxes, and diff away.

Diffing using Javascript

Here’s the function from the demo HTML file linked to above that diffs the two pieces of text entered into the textboxes on the page:

function diffUsingJS() {
 // get the baseText and newText values from the two textboxes, and split them into lines
 var base = difflib.stringAsLines($("baseText").value);
 var newtxt = difflib.stringAsLines($("newText").value);

 // create a SequenceMatcher instance that diffs the two sets of lines
 var sm = new difflib.SequenceMatcher(base, newtxt);

 // get the opcodes from the SequenceMatcher instance
 // opcodes is a list of 3-tuples describing what changes should be made to the base text
 // in order to yield the new text
 var opcodes = sm.get_opcodes();
 var diffoutputdiv = $("diffoutput");
 while (diffoutputdiv.firstChild) diffoutputdiv.removeChild(diffoutputdiv.firstChild);
 var contextSize = $("contextSize").value;
 contextSize = contextSize ? contextSize : null;

 // build the diff view and add it to the current DOM
 diffoutputdiv.appendChild(diffview.buildView({
 baseTextLines: base,
 newTextLines: newtxt,
 opcodes: opcodes,
 // set the display titles for each resource
 baseTextName: "Base Text",
 newTextName: "New Text",
 contextSize: contextSize,
 viewType: $("inline").checked ? 1 : 0
 }));

 // scroll down to the diff view window.
 location = url + "#diff";
}

There’s not a whole lot to say about this function. The most notable aspect of it is that the diffview.buildView() function takes an object/map with specific attributes, rather than a list of arguments. Those attributes are mostly self-explanatory, but are nonetheless described in detail in code documentation in diffview.js.

Diffing using Python

This isn’t enabled in the demo link above, but I’ve included it to exemplify how one might use the opcode output from a web-based Python backend to drive jsdifflib’s diff view.

function diffUsingPython() {
 dojo.io.bind({
 url: "/diff/postYieldDiffData",
 method: "POST",
 content: {
 baseText: $("baseText").value,
 newText: $("newText").value,
 ignoreWhitespace: "Y"
 },
 load: function (type, data, evt) {
 try {
 data = eval('(' + data + ')');
 while (diffoutputdiv.firstChild) diffoutputdiv.removeChild(diffoutputdiv.firstChild);
 $("output").appendChild(diffview.buildView({
 baseTextLines: data.baseTextLines,
 newTextLines: data.newTextLines,
 opcodes: data.opcodes,
 baseTextName: data.baseTextName,
 newTextName: data.newTextName,
 contextSize: contextSize
 }));
 } catch (ex) {
 alert("An error occurred updating the diff view:\n" + ex.toString());
 }
 },
 error: function (type, evt) {
 alert('Error occurred getting diff data. Check the server logs.');
 },
 type: 'text/javascript'
 });
}
Warning

This dojo code was written in 2007, and I haven’t looked at dojo for years now. In any case, you should be able to grok what’s going on.

As you can see, I’m partial to using dojo for ajaxy stuff. All that is happening here is the base and new text is being POSTed to a Python server-side process (we like pylons, but you could just as easily use a simple Python script as a cgi). That process then needs to diff the provided text using an instance of Python’s difflib.SequenceMatcher class, and return the opcodes from that SequenceMatcher instance to the browser (in this case, using JSON serialization). In the interest of completeness, here’s the controller action from our pylons application that does this (don’t try to match up the parameters shown below with the POST parameters shown in the Javascript function above; the latter is only here as an example):

@jsonify
def diff (self, baseText, newText, baseTextName="Base Text", newTextName="New Text"):
 opcodes = SequenceMatcher(isjunk, baseText, newText).get_opcodes()
 return dict(baseTextLines=baseText, newTextLines=newText, opcodes=opcodes,
 baseTextName=baseTextName, newTextName=newTextName)

Future Directions

The top priorities would be to implement the ignoring of empty lines, and the indication of diffs at the character level with sub-highlighting (similar to what Trac’s diff view does).

I’d also like to see the difflib.SequenceMatcher reimplementation gain some more speed — it’s virtually a line-by-line translation from the Python implementation, so there’s plenty that could be done to make it more performant in Javascript. However, that would mean making the reimplementation diverge even more from the "reference" Python implementation. Given that I don’t really want to worry about the algorithm, that’s not appealing. I’d much rather use a server-side process when the in-browser diffing is a little too pokey.

Other than that, I’m open to suggestions.

Note

I’m no longer actively developing jsdifflib. It’s been sequestered (mostly out of simple neglect) to my company’s servers for too long; now that it’s on github, I’m hoping that many of the people that find it useful will submit pull requests to improve the library. I will do what I can to curate that process.

License

jsdifflib carries a BSD license. As such, it may be used in other products or services with appropriate attribution (including commercial offerings). The license is prepended to each of jsdifflib’s files.

Downloads

jsdifflib consists of three files — two Javascript files, and one CSS file. Why two Javascript files? Because I wanted to keep the reimplementation of the python difflib.SequenceMatcher class separate from the actual visual diff view generator. Feel free to combine and/or optimize them in your deployment environment.

You can download the files separately by navigating the project on github, you can clone the repo, or you can download a zipped distribution via the "Downloads" button at the top of this project page.

Release History

  • 1.1.0 (May 18, 2011): Move project to github; no changes in functionality

  • 1.0.0 (February 22, 2007): Initial release

举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/stone1116/jsdifflib.git
git@gitee.com:stone1116/jsdifflib.git
stone1116
jsdifflib
jsdifflib
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /