editeur 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ =Editeur
2
+
3
+ editeur is a simple diff tool for English. You can invoke it as
4
+
5
+ editeur [-s <separator>] file1 file2
6
+
7
+ ...where the default separator is ". " So, basically, it makes it easy to diff
8
+ two documents sentence by sentence
9
+
10
+ ==LICENCE
11
+
12
+ Public Domain.
13
+
14
+ ==Caveats
15
+
16
+ Still very rough. Not tested much at all.
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+
4
+ require 'rubygems' unless defined?(gem)
5
+ here = File.dirname(__FILE__)
6
+ $LOAD_PATH << File.expand_path(File.join(here, '..', 'lib'))
7
+ require 'editeur'
@@ -0,0 +1,22 @@
1
+ require 'editeur/differ'
2
+ require 'editeur/reader'
3
+ require 'editeur/printer'
4
+ require 'optparse'
5
+
6
+ opts = {
7
+ :separator => '. '
8
+ }
9
+
10
+ op = OptionParser.new
11
+
12
+ op.on('-s SEP', 'change the separator') { |s|
13
+ opts[:separator] = s
14
+ }
15
+
16
+ op.parse!(ARGV)
17
+
18
+ f1, f2 = ARGV.slice 0, 2
19
+ reader1 = Editeur::Reader.new f1, opts[:separator]
20
+ reader2 = Editeur::Reader.new f2, opts[:separator]
21
+ differ = Editeur::Differ.new reader1, reader2
22
+ Editeur::Printer.print differ.diff
@@ -0,0 +1,65 @@
1
+ module Editeur
2
+
3
+ require 'diff-lcs'
4
+ class Differ
5
+ def initialize(right_reader, left_reader)
6
+ @right_reader = right_reader
7
+ @left_reader = left_reader
8
+ @left_lines = []
9
+ @right_lines = []
10
+ end
11
+
12
+ def subtract! xs, ys
13
+ acc = []
14
+ if ys.empty?
15
+ ys = [nil]
16
+ end
17
+
18
+ for y in ys
19
+ x = xs.shift
20
+ tmp_acc = []
21
+ while x
22
+ if x != y
23
+ tmp_acc << x
24
+ else
25
+ break
26
+ end
27
+ x = xs.shift
28
+ end
29
+ acc << tmp_acc
30
+ end
31
+ acc
32
+ end
33
+
34
+ def diff
35
+ for r in @right_reader
36
+ @right_lines << r
37
+ end
38
+ for l in @left_reader
39
+ @left_lines << l
40
+ end
41
+
42
+ lcs = Diff::LCS.LCS @left_lines, @right_lines
43
+
44
+ diff_left = subtract! @left_lines, lcs
45
+ diff_right = subtract! @right_lines, lcs
46
+
47
+ emit(diff_left, diff_right)
48
+ end
49
+
50
+ def emit(lefts, rights)
51
+ output = []
52
+ for left, right in [lefts, rights].transpose
53
+ if not left.empty?
54
+ output += left.map { |line| '- ' + line }
55
+ end
56
+ if not right.empty?
57
+ output += right.map { |line| '+ ' + line }
58
+ end
59
+ end
60
+
61
+ output
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,34 @@
1
+ module Editeur
2
+
3
+ class UnknownFormatError < StandardError
4
+ end
5
+
6
+ module TTYColor
7
+ RED = "\033[1;31m"
8
+ GREEN = "\033[1;32m"
9
+ NORMAL = "\033[0;39m"
10
+ end
11
+
12
+ class Printer
13
+ include TTYColor
14
+ ColorMap = {
15
+ '+ ' => GREEN,
16
+ '- ' => RED
17
+ }
18
+ def self.print(lines, enable_color = true)
19
+ lines.each { |line|
20
+ if not enable_color
21
+ puts line
22
+ else
23
+ color = ColorMap[line[0..1]]
24
+ if not color
25
+ raise UnknownFormatError
26
+ end
27
+
28
+ puts [color, line, NORMAL].join
29
+ end
30
+ }
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,54 @@
1
+ module Editeur
2
+
3
+ class Reader
4
+ attr :lines
5
+ def initialize(path, sep="\n")
6
+ if not File.exists? path
7
+ return nil
8
+ end
9
+
10
+ @handle = File.new(path)
11
+ @sep = sep
12
+ @buf = ''
13
+ @buf_size = 1024
14
+ @lines = []
15
+ end
16
+
17
+ def read
18
+ if @lines.length > 1
19
+ return @lines.shift
20
+ end
21
+
22
+ head = @lines.shift
23
+ buf = @handle.read @buf_size
24
+
25
+ if (not head) and (not buf)
26
+ return nil
27
+ end
28
+
29
+ head ||= ''
30
+ if buf
31
+ buf.gsub!("\n", ' ')
32
+ chunks = buf.split @sep
33
+ head += (chunks.shift)
34
+ for chunk in chunks
35
+ @lines << chunk
36
+ end
37
+ end
38
+
39
+ head
40
+ end
41
+
42
+ def each
43
+ while true
44
+ line = read
45
+ if not line
46
+ break
47
+ end
48
+ yield line
49
+ end
50
+ nil
51
+ end
52
+ end
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: editeur
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - kiyoto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: diff-lcs
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.3
30
+ description: described
31
+ email:
32
+ - me@ktamura.com
33
+ executables:
34
+ - editeur
35
+ extensions: []
36
+ extra_rdoc_files:
37
+ - README.rdoc
38
+ files:
39
+ - bin/editeur
40
+ - lib/editeur.rb
41
+ - lib/editeur/differ.rb
42
+ - lib/editeur/printer.rb
43
+ - lib/editeur/reader.rb
44
+ - README.rdoc
45
+ homepage: http://ktamuta.com
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.24
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: sentence by sentence diff-ing
69
+ test_files: []