GithubHelp home page GithubHelp logo

yixf-self / tdiff Goto Github PK

View Code? Open in Web Editor NEW

This project forked from postmodern/tdiff

0.0 0.0 0.0 39 KB

Calculates the differences between two tree-like structures.

License: MIT License

Ruby 100.00%

tdiff's Introduction

TDiff

Description

Calculates the differences between two tree-like structures. Similar to Rubys built-in TSort module.

Features

  • Provides the {TDiff} mixin.
  • Provides the {TDiff::Unordered} mixin for unordered diffing.
  • Allows custom node equality and traversal logic by overriding the {TDiff#tdiff_equal} and {TDiff#tdiff_each_child} methods.
  • Implements the Longest Common Subsequence (LCS) algorithm.

Examples

Diff two HTML documents:

require 'nokogiri'
require 'tdiff'

class Nokogiri::XML::Node

  include TDiff

  def tdiff_equal(node)
    if (self.text? && node.text?)
      self.text == node.text
    elsif (self.respond_to?(:root) && node.respond_to?(:root))
      self.root.tdiff_equal(node.root)
    elsif (self.respond_to?(:name) && node.respond_to?(:name))
      self.name == node.name
    else
      false
    end
  end

  def tdiff_each_child(node,&block)
    node.children.each(&block)
  end

end

doc1 = Nokogiri::HTML('<div><p>one</p> <p>three</p></div>')
doc2 = Nokogiri::HTML('<div><p>one</p> <p>two</p> <p>three</p></div>')

doc1.at('div').tdiff(doc2.at('div')) do |change,node|
  puts "#{change} #{node.to_html}".ljust(30) + node.parent.path
end

Output

+ <p>one</p>                  /html/body/div
+                             /html/body/div
  <p>one</p>                  /html/body/div
                              /html/body/div
  <p>three</p>                /html/body/div
- one                         /html/body/div/p[1]
+ two                         /html/body/div/p[2]
  three                       /html/body/div/p[2]

Requirements

Install

$ gem install tdiff

Copyright

See {file:LICENSE.txt} for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.