Skip to content

Commit

Permalink
Merge pull request #1356 from kmuto/refactoring-table
Browse files Browse the repository at this point in the history
各ビルダのtableメソッドのリファクタリング
  • Loading branch information
kmuto authored Aug 20, 2019
2 parents eaf3f38 + a1b7f79 commit 0a1dbfd
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 195 deletions.
34 changes: 20 additions & 14 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2002-2018 Minero Aoki, Kenshi Muto
# Copyright (c) 2002-2019 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -157,28 +157,35 @@ def image(lines, id, caption, metric = nil)
end

def table(lines, id = nil, caption = nil)
rows = []
sepidx, rows = parse_table_rows(lines)
begin
if caption.present?
table_header(id, caption)
end
rescue KeyError
error "no such table: #{id}"
end
table_begin(rows.first.size)
table_rows(sepidx, rows)
table_end
end

def parse_table_rows(lines)
sepidx = nil
rows = []
lines.each_with_index do |line, idx|
if /\A[\=\-]{12}/ =~ line
# just ignore
# error "too many table separator" if sepidx
if /\A[\=\-]{12}/ =~ line || /\A[\=\{\-\}]{12}/ =~ line
sepidx ||= idx
next
end
rows.push(line.strip.split(/\t+/).map { |s| s.sub(/\A\./, '') })
end
rows = adjust_n_cols(rows)
error 'no rows in the table' if rows.empty?
[sepidx, rows]
end

begin
if caption.present?
table_header(id, caption)
end
rescue KeyError
error "no such table: #{id}"
end
table_begin(rows.first.size)
def table_rows(sepidx, rows)
if sepidx
sepidx.times do
tr(rows.shift.map { |s| th(s) })
Expand All @@ -192,7 +199,6 @@ def table(lines, id = nil, caption = nil)
tr([th(h)] + cs.map { |s| td(s) })
end
end
table_end
end

def adjust_n_cols(rows)
Expand Down
37 changes: 1 addition & 36 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -642,47 +642,12 @@ def image_header(id, caption)
end

def table(lines, id = nil, caption = nil)
rows = []
sepidx = nil
lines.each_with_index do |line, idx|
if /\A[\=\-]{12}/ =~ line
# just ignore
# error "too many table separator" if sepidx
sepidx ||= idx
next
end
rows.push(line.strip.split(/\t+/).map { |s| s.sub(/\A\./, '') })
end
rows = adjust_n_cols(rows)
error 'no rows in the table' if rows.empty?

if id
puts %Q(<div id="#{normalize_id(id)}" class="table">)
else
puts %Q(<div class="table">)
end
begin
if caption.present?
table_header(id, caption)
end
rescue KeyError
error "no such table: #{id}"
end
table_begin(rows.first.size)
if sepidx
sepidx.times do
tr(rows.shift.map { |s| th(s) })
end
rows.each do |cols|
tr(cols.map { |s| td(s) })
end
else
rows.each do |cols|
h, *cs = *cols
tr([th(h)] + cs.map { |s| td(s) })
end
end
table_end
super(lines, id, caption)
puts '</div>'
end

Expand Down
69 changes: 39 additions & 30 deletions lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,63 +449,75 @@ def texequation(lines, id = nil, caption = '')
end

def table(lines, id = nil, caption = nil)
tablewidth = @book.config['tableopt'] ? @book.config['tableopt'].split(',')[0].to_f / @book.config['pt_to_mm_unit'].to_f : nil
col = 0
@tablewidth = nil
if @book.config['tableopt']
@tablewidth = @book.config['tableopt'].split(',')[0].to_f / @book.config['pt_to_mm_unit'].to_f
end
@col = 0

rows = []
sepidx, rows = parse_table_rows(lines)
puts '<table>'

begin
table_header(id, caption) if caption.present?
rescue KeyError
error "no such table: #{id}"
end

if @tablewidth.nil?
print '<tbody>'
else
print %Q(<tbody xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" aid:table="table" aid:trows="#{rows.length}" aid:tcols="#{@col}">)
end
table_rows(sepidx, rows)
puts '</tbody></table>'
@tsize = nil
end

def parse_table_rows(lines)
sepidx = nil
rows = []
lines.each_with_index do |line, idx|
if /\A[\=\-]{12}/ =~ line
sepidx ||= idx
next
end
if tablewidth
if @tablewidth
rows.push(line.gsub(/\t\.\t/, "\tDUMMYCELLSPLITTER\t").gsub(/\t\.\.\t/, "\t.\t").gsub(/\t\.\Z/, "\tDUMMYCELLSPLITTER").gsub(/\t\.\.\Z/, "\t.").gsub(/\A\./, ''))
else
rows.push(line.gsub(/\t\.\t/, "\t\t").gsub(/\t\.\.\t/, "\t.\t").gsub(/\t\.\Z/, "\t").gsub(/\t\.\.\Z/, "\t.").gsub(/\A\./, ''))
end
col2 = rows[rows.length - 1].split(/\t/).length
col = col2 if col2 > col
@col = col2 if col2 > @col
end
error 'no rows in the table' if rows.empty?
[sepidx, rows]
end

puts '<table>'

def table_rows(sepidx, rows)
cellwidth = []
if tablewidth
if @tablewidth
if @tsize.nil?
col.times { |n| cellwidth[n] = tablewidth / col }
@col.times { |n| cellwidth[n] = @tablewidth / @col }
else
cellwidth = @tsize.split(/\s*,\s*/)
totallength = 0
cellwidth.size.times do |n|
cellwidth[n] = cellwidth[n].to_f / @book.config['pt_to_mm_unit'].to_f
totallength += cellwidth[n]
warn "total length exceeds limit for table: #{id}" if totallength > tablewidth
warn "total length exceeds limit for table: #{id}" if totallength > @tablewidth
end
if cellwidth.size < col
cw = (tablewidth - totallength) / (col - cellwidth.size)
if cellwidth.size < @col
cw = (@tablewidth - totallength) / (@col - cellwidth.size)
warn "auto cell sizing exceeds limit for table: #{id}" if cw <= 0
(cellwidth.size..(col - 1)).each { |i| cellwidth[i] = cw }
(cellwidth.size..(@col - 1)).each { |i| cellwidth[i] = cw }
end
end
end

begin
table_header(id, caption) if caption.present?
rescue KeyError
error "no such table: #{id}"
end

if tablewidth.nil?
print '<tbody>'
else
print %Q(<tbody xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" aid:table="table" aid:trows="#{rows.length}" aid:tcols="#{col}">)
end

if sepidx
sepidx.times do |y|
if tablewidth.nil?
if @tablewidth.nil?
puts %Q(<tr type="header">#{rows.shift}</tr>)
else
i = 0
Expand All @@ -516,9 +528,7 @@ def table(lines, id = nil, caption = nil)
end
end
end
trputs(tablewidth, rows, cellwidth, sepidx)
puts '</tbody></table>'
@tsize = nil
trputs(@tablewidth, rows, cellwidth, sepidx)
end

def trputs(tablewidth, rows, cellwidth, sepidx)
Expand Down Expand Up @@ -564,7 +574,6 @@ def td(str)
end

def table_end
print '<?dtp tablerow last?>'
end

def emtable(lines, caption = nil)
Expand Down
23 changes: 1 addition & 22 deletions lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,27 +606,7 @@ def indepimage(lines, id, caption = nil, metric = nil)

alias_method :numberlessimage, :indepimage

def table(lines, id = nil, caption = nil)
rows = []
sepidx = nil
lines.each_with_index do |line, idx|
if /\A[\=\{\-\}]{12}/ =~ line
# just ignore
# error "too many table separator" if sepidx
sepidx ||= idx
next
end
rows.push(line.strip.split(/\t+/).map { |s| s.sub(/\A\./, '') })
end
rows = adjust_n_cols(rows)
error 'no rows in the table' if rows.empty?

begin
table_header(id, caption) if caption.present?
rescue KeyError
error "no such table: #{id}"
end
table_begin(rows.first.size)
def table_rows(sepidx, rows)
if sepidx
sepidx.times do
cno = -1
Expand All @@ -653,7 +633,6 @@ def table(lines, id = nil, caption = nil)
end)
end
end
table_end
end

def table_header(id, caption)
Expand Down
25 changes: 3 additions & 22 deletions lib/review/markdownbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) 2013-2019 KADO Masanori, Masayoshi Takahashi, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
# the GNU LGPL, Lesser General Public License version 2.1.
Expand Down Expand Up @@ -244,27 +246,7 @@ def cmd(lines)
puts '```'
end

def table(lines, id = nil, caption = nil)
rows = []
sepidx = nil
lines.each_with_index do |line, idx|
if /\A[\=\-]{12}/ =~ line
# just ignore
# error "too many table separator" if sepidx
sepidx ||= idx
next
end
rows.push(line.strip.split(/\t+/).map { |s| s.sub(/\A\./, '') })
end
rows = adjust_n_cols(rows)
error 'no rows in the table' if rows.empty?

begin
table_header(id, caption) unless caption.nil?
rescue KeyError
error "no such table: #{id}"
end
table_begin(rows.first.size)
def table_rows(sepidx, rows)
if sepidx
sepidx.times do
tr(rows.shift.map { |s| th(s) })
Expand All @@ -279,7 +261,6 @@ def table(lines, id = nil, caption = nil)
tr([th(h)] + cs.map { |s| td(s) })
end
end
table_end
end

def table_header(id, caption)
Expand Down
40 changes: 4 additions & 36 deletions lib/review/plaintextbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,43 +220,11 @@ def texequation(lines, id = nil, caption = '')
blank
end

def table(lines, id = nil, caption = nil)
rows = []
sepidx = nil
lines.each_with_index do |line, idx|
if /\A[\=\-]{12}/ =~ line
# just ignore
# error "too many table separator" if sepidx
sepidx ||= idx
next
end
rows.push(line.strip.split(/\t+/).map { |s| s.sub(/\A\./, '') })
end
rows = adjust_n_cols(rows)
error 'no rows in the table' if rows.empty?

blank

begin
table_header(id, caption) if caption.present?
rescue KeyError
error "no such table: #{id}"
end
table_begin(rows.first.size)
if sepidx
sepidx.times do
tr(rows.shift.map { |s| th(s) })
end
rows.each do |cols|
tr(cols.map { |s| td(s) })
end
else
rows.each do |cols|
h, *cs = *cols
tr([th(h)] + cs.map { |s| td(s) })
end
def table(lines, id = nil, caption = nil, noblank = nil)
unless noblank
blank
end
table_end
super(lines, id, caption)
end

def table_header(id, caption)
Expand Down
Loading

0 comments on commit 0a1dbfd

Please sign in to comment.