Skip to content

Commit ad39ae4

Browse files
committed
Make compatible with lib in rails folder
1 parent ff4d973 commit ad39ae4

File tree

6 files changed

+190
-189
lines changed

6 files changed

+190
-189
lines changed

Diff for: bin/docx2html

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if $0 == __FILE__
88
end
99

1010
require 'ydocx/command'
11-
require 'ruby-prof'
11+
#require 'ruby-prof'
1212

1313
#RubyProf.start
1414
YDocx::Command.run(:to_html)

Diff for: lib/ydocx/builder.rb

+28-43
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,36 @@
88
module YDocx
99
class Builder
1010
include MarkupMethod
11-
attr_accessor :contents, :style, :title
12-
def initialize(contents)
13-
@contents = contents
14-
@style = false
15-
@title = ''
16-
init
17-
if block_given?
18-
yield self
19-
end
20-
end
21-
def init
11+
def self.build_html(node)
12+
compile(node, :html)
2213
end
23-
def build_html
24-
body = compile(@contents, :html)
25-
builder = Nokogiri::HTML::Builder.new do |doc|
26-
doc.html {
27-
doc.head {
28-
doc.meta :charset => 'utf-8'
29-
doc.title @title
30-
doc.link :rel => 'stylesheet', :type => 'text/css', :href => 'assets/style.css' if @style
31-
doc.script :src => 'assets/script.js'
32-
}
33-
doc.body { doc << body }
34-
}
35-
end
36-
builder.to_html
14+
private
15+
def self.compile(node, mode)
16+
element = node.to_markup
17+
build_tag(element[:tag], element[:content], element[:attributes], mode)
3718
end
38-
def build_xml
39-
paragraphs = compile(@contents, :xml)
40-
builder = Nokogiri::XML::Builder.new do |xml|
41-
xml.document {
42-
xml.paragraphs { xml << paragraphs }
43-
}
44-
end
45-
builder.to_xml(:indent => 0, :encoding => 'utf-8').gsub(/\n/, '')
46-
end
47-
private
48-
def compile(contents, mode)
49-
result = ''
50-
contents.to_markup.each do |element|
51-
result << build_tag(element[:tag], element[:content], element[:attributes], mode)
19+
def self.escape_whitespace(text)
20+
prev_ws = false
21+
new_text = ''
22+
text.each_char do |c|
23+
if c == "\n"
24+
new_text += "<br />"
25+
prev_ws = true
26+
elsif c =~ /[[:space:]]/
27+
if prev_ws
28+
new_text += "&nbsp;"
29+
else
30+
new_text += c
31+
end
32+
prev_ws = true
33+
else
34+
new_text += c
35+
prev_ws = false
36+
end
5237
end
53-
result
38+
new_text
5439
end
55-
def build_tag(tag, content, attributes, mode=:html)
40+
def self.build_tag(tag, content, attributes, mode=:html)
5641
if tag == :br and mode != :xml
5742
return "<br/>"
5843
elsif content.nil? or content.empty?
@@ -65,13 +50,13 @@ def build_tag(tag, content, attributes, mode=:html)
6550
if c.is_a? Hash
6651
_content << build_tag(c[:tag], c[:content], c[:attributes], mode)
6752
elsif c.is_a? String
68-
_content << (mode == :html ? c : c.chomp.to_s)
53+
_content << (mode == :html ? escape_whitespace(c) : c.chomp.to_s)
6954
end
7055
end
7156
elsif content.is_a? Hash
7257
_content = build_tag(content[:tag], content[:content], content[:attributes], mode)
7358
elsif content.is_a? String
74-
_content = content
59+
_content = (mode == :html ? escape_whitespace(content) : content)
7560
end
7661
_tag = tag.to_s
7762
_attributes = ''

Diff for: lib/ydocx/command.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
require 'ydocx'
55
require 'ydocx/differ'
6-
require 'ruby-prof'
76

87
module YDocx
98
class Command
@@ -87,6 +86,7 @@ def run_diff
8786
puts 'Parsing...'
8887
docs = files.map { |f| YDocx::Document.open(f) }
8988
f = File.new(argv[2], "w")
89+
#require 'ruby-prof'
9090
#RubyProf.start
9191
t = Time.now
9292
f.write YDocx::Differ.new.diff(*docs)

Diff for: lib/ydocx/differ.rb

+87-47
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env ruby
22
# encoding: utf-8
33

4-
require 'ydocx'
4+
require 'ydocx/builder'
55
require 'diff/lcs'
66

77
module YDocx
88
class Differ
9+
Block = Struct.new(:start_line, :type, :lines)
10+
911
def initialize
1012
@cur_change_id = 1
1113
end
@@ -137,47 +139,48 @@ def get_paragraph_diff(p1, p2, result)
137139
chunks[i].each_with_index do |chunk, j|
138140
if change_id[i][j]
139141
if change_id[i][j] >= 1
140-
group.class = (sprintf 'modify modify%d', change_id[i][j])
142+
group.css_class = (sprintf 'modify modify-%d', change_id[i][j])
141143
elsif i == 0
142-
group.class = 'delete'
144+
group.css_class = 'delete'
143145
else
144-
group.class = 'add'
146+
group.css_class = 'add'
145147
end
146148
if chunk == [Run.new("\n")]
147149
group.runs << Run.new("&crarr;\n")
148150
else
149151
group.runs += chunk
150152
end
151153
else
152-
p.groups << group unless group.runs.empty?
154+
p.runs << group unless group.runs.empty?
153155
group = RunGroup.new
154-
p.groups << RunGroup.new(chunk)
156+
p.runs += chunk
155157
end
156158
end
157-
p.groups << group unless group.runs.empty?
158-
result[i].blocks << p
159+
p.runs << group unless group.runs.empty?
160+
p.merge_runs
161+
result[i] << p
159162
end
160163
end
161164
def get_table_diff(t1, t2, result)
162165
tables = [Table.new, Table.new]
163166
get_detail_blocks(t1.rows, t2.rows).each do |rblock|
164167
if rblock[0].empty?
165-
rblock[1].map { |r| r.cells.map { |c| c.class = 'add' } }
168+
rblock[1].map { |r| r.cells.map { |c| c.css_class = 'add' } }
166169
tables[1].rows += rblock[1]
167170
elsif rblock[1].empty?
168-
rblock[0].map { |r| r.cells.map { |c| c.class = 'delete' } }
171+
rblock[0].map { |r| r.cells.map { |c| c.css_class = 'delete' } }
169172
tables[0].rows += rblock[0]
170173
else # should be 1 row in each
171174
change_id = get_diff_change_array(rblock[0].first.cells, rblock[1].first.cells)
172175
for i in 0..1
173176
rblock[i].first.cells.each_with_index do |cell, j|
174177
if change_id[i][j]
175178
if change_id[i][j] >= 1
176-
cell.class = (sprintf 'modify modify%d', change_id[i][j])
179+
cell.css_class = (sprintf 'modify modify-%d', change_id[i][j])
177180
elsif i == 0
178-
cell.class = 'delete'
181+
cell.css_class = 'delete'
179182
else
180-
cell.class = 'add'
183+
cell.css_class = 'add'
181184
end
182185
end
183186
end
@@ -186,18 +189,56 @@ def get_table_diff(t1, t2, result)
186189
tables[1].rows += rblock[1]
187190
end
188191
end
189-
result[0].blocks << tables[0]
190-
result[1].blocks << tables[1]
192+
result[0] << tables[0]
193+
result[1] << tables[1]
191194
end
192195
def diff(doc1, doc2)
193-
blocks1 = doc1.contents.blocks
194-
blocks2 = doc2.contents.blocks
196+
blocks1 = doc1.blocks
197+
blocks2 = doc2.blocks
198+
result = {:inline => [[], []], :side => [[], []]}
199+
200+
pg = [1, 1]
201+
lblocks = []
202+
rblocks = []
203+
inline_blocks = []
204+
Diff::LCS.sdiff(blocks1, blocks2).each do |change|
205+
if change.action == '='
206+
inline_blocks << [lblocks.dup, rblocks.dup] unless lblocks.empty? && rblocks.empty?
207+
inline_blocks << [[blocks1[change.old_position]], [blocks2[change.new_position]]]
208+
lblocks = []
209+
rblocks = []
210+
else
211+
lblocks << blocks1[change.old_position] unless change.old_element.nil?
212+
rblocks << blocks2[change.new_position] unless change.new_element.nil?
213+
end
214+
end
215+
inline_blocks << [lblocks.dup, rblocks.dup] unless lblocks.empty? && rblocks.empty?
216+
inline_blocks.each do |blocks|
217+
html_blocks = blocks.map do |block|
218+
block.map { |b| Builder.build_html(b) }
219+
end
220+
if blocks[0] == blocks[1]
221+
result[:inline][0] << Block.new(pg[0], '=', html_blocks[0])
222+
result[:inline][1] << Block.new(pg[1], '=', html_blocks[1])
223+
pg[0] += html_blocks[0].length
224+
pg[1] += html_blocks[1].length
225+
else
226+
unless blocks[0].empty?
227+
result[:inline][0] << Block.new(pg[0], '-', html_blocks[0])
228+
result[:inline][1] << nil
229+
pg[0] += html_blocks[0].length
230+
end
231+
unless blocks[1].empty?
232+
result[:inline][0] << nil
233+
result[:inline][1] << Block.new(pg[1], '+', html_blocks[1])
234+
pg[1] += html_blocks[1].length
235+
end
236+
end
237+
end
195238

196-
puts 'Extracting text...'
197239
text1 = blocks1.map { |b| b.get_chunks.map(&method(:get_text)) }
198240
text2 = blocks2.map { |b| b.get_chunks.map(&method(:get_text)) }
199241

200-
puts 'Computing paragraph diffs...'
201242
lblocks = []
202243
rblocks = []
203244
diff_blocks = []
@@ -214,45 +255,44 @@ def diff(doc1, doc2)
214255
end
215256
diff_blocks << [lblocks.dup, rblocks.dup] unless lblocks.empty? && rblocks.empty?
216257

217-
puts 'Computing block diffs...'
218-
table = Table.new
258+
pg = [1, 1]
219259
diff_blocks.each do |dblock|
220260
get_detail_blocks(dblock[0], dblock[1]).each do |block|
221-
row = Row.new
222-
row.cells = [Cell.new, Cell.new]
261+
type = ['=', '=']
262+
blocks = [[], []]
223263
if block[0].empty?
224-
row.cells[1].class = 'add'
225-
row.cells[1].blocks = block[1]
264+
blocks[1] = block[1]
265+
type[1] = '+'
226266
elsif block[1].empty?
227-
row.cells[0].class = 'delete'
228-
row.cells[0].blocks = block[0]
229-
elsif block[0] != block[1] # should only be 1 block in each
230-
row.cells[0].class = row.cells[1].class = 'modify'
267+
blocks[0] = block[0]
268+
type[0] = '-'
269+
elsif block[0] != block[1]
270+
# Each block should only contain 1 element
271+
type = ['!', '!']
231272
if block[0].first.is_a? Paragraph
232-
get_paragraph_diff(block[0].first, block[1].first, row.cells)
273+
get_paragraph_diff(block[0].first, block[1].first, blocks)
233274
else
234-
get_table_diff(block[0].first, block[1].first, row.cells)
275+
get_table_diff(block[0].first, block[1].first, blocks)
235276
end
236277
else
237-
row.cells[0].blocks = block[0]
238-
row.cells[1].blocks = block[1]
278+
blocks[0] = block[0]
279+
blocks[1] = block[1]
280+
end
281+
blocks = blocks.map do |block|
282+
block.map { |b| Builder.build_html(b) }
283+
end
284+
for i in 0..1
285+
if blocks[i].empty?
286+
result[:side][i] << nil
287+
else
288+
result[:side][i] << Block.new(pg[i], type[i], blocks[i])
289+
pg[i] += blocks[i].length
290+
end
239291
end
240-
table.rows << row
241-
end
242-
end
243-
244-
[doc1, doc2].each do |doc|
245-
if !doc.images.empty?
246-
doc.create_files
247292
end
248293
end
249294

250-
html_doc = ParsedDocument.new
251-
html_doc.blocks << table
252-
builder = Builder.new(html_doc)
253-
builder.title = 'Diff Results'
254-
builder.style = true
255-
builder.build_html
295+
result
256296
end
257297
end
258-
end
298+
end

0 commit comments

Comments
 (0)