Skip to content

Commit

Permalink
Merge pull request #777 from kmuto/emtable
Browse files Browse the repository at this point in the history
//emtableの導入
  • Loading branch information
kmuto authored Jun 1, 2017
2 parents 220dafc + 8cfab8b commit 765a9bd
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 21 deletions.
4 changes: 4 additions & 0 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ def adjust_n_cols(rows)
end
private :adjust_n_cols

def emtable(lines, caption = nil)
table(lines, nil, caption)
end

#def footnote(id, str)
# @footnotes.push [id, str]
#end
Expand Down
1 change: 1 addition & 0 deletions lib/review/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def inline_defined?(name)
defblock :cmd, 0..1
defblock :table, 0..2
defblock :imgtable, 0..2
defblock :emtable, 0..1
defblock :quote, 0
defblock :image, 2..3, true
defblock :source, 0..2
Expand Down
14 changes: 11 additions & 3 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,14 @@ def table(lines, id = nil, caption = nil)
end

def table_header(id, caption)
if get_chap.nil?
puts %Q[<p class="caption">#{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}</p>]
if id.nil?
puts %Q[<p class="caption">#{compile_inline(caption)}</p>]
else
puts %Q[<p class="caption">#{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}</p>]
if get_chap.nil?
puts %Q[<p class="caption">#{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}</p>]
else
puts %Q[<p class="caption">#{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}</p>]
end
end
end

Expand Down Expand Up @@ -727,6 +731,10 @@ def imgtable_image(id, caption, metric)
puts %Q[<img src="#{@chapter.image(id).path.sub(/\A\.\//, "")}" alt="#{escape_html(compile_inline(caption))}"#{metrics} />]
end

def emtable(lines, caption = nil)
table(lines, nil, caption)
end

def comment(lines, comment = nil)
lines ||= []
lines.unshift comment unless comment.blank?
Expand Down
14 changes: 11 additions & 3 deletions lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,14 @@ def trputs(tablewidth, rows, cellwidth, sepidx)
end

def table_header(id, caption)
if get_chap.nil?
puts %Q[<caption>#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}</caption>]
if id.nil?
puts %Q[<caption>#{compile_inline(caption)}</caption>]
else
puts %Q[<caption>#{I18n.t("table")}#{I18n.t("format_number", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}</caption>]
if get_chap.nil?
puts %Q[<caption>#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}</caption>]
else
puts %Q[<caption>#{I18n.t("table")}#{I18n.t("format_number", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}</caption>]
end
end
end

Expand All @@ -589,6 +593,10 @@ def table_end
print "<?dtp tablerow last?>"
end

def emtable(lines, caption = nil)
table(lines, nil, caption)
end

def imgtable(lines, id, caption = nil, metric = nil)
if @chapter.image(id).bound?
metrics = parse_metric("idgxml", metric)
Expand Down
22 changes: 17 additions & 5 deletions lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,20 @@ def table(lines, id = nil, caption = nil)
end

def table_header(id, caption)
if caption.present?
@table_caption = true
puts '\begin{table}[h]'
puts macro('reviewtablecaption', compile_inline(caption))
if id.nil?
if caption.present?
@table_caption = true
puts '\begin{table}[h]'
puts macro('reviewtablecaption*', compile_inline(caption))
end
else
if caption.present?
@table_caption = true
puts '\begin{table}[h]'
puts macro('reviewtablecaption', compile_inline(caption))
end
puts macro('label', table_label(id))
end
puts macro('label', table_label(id))
end

def table_begin(ncols)
Expand Down Expand Up @@ -598,6 +606,10 @@ def table_end
blank
end

def emtable(lines, caption = nil)
table(lines, nil, caption)
end

def imgtable(lines, id, caption = nil, metric = nil)
if !@chapter.image(id).bound?
warn "image not bound: #{id}"
Expand Down
10 changes: 7 additions & 3 deletions lib/review/markdownbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,14 @@ def table(lines, id = nil, caption = nil)
end

def table_header(id, caption)
if get_chap.nil?
puts %Q[#{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}]
if id.nil?
puts compile_inline(caption)
else
puts %Q[#{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}]
if get_chap.nil?
puts %Q[#{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}]
else
puts %Q[#{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}]
end
end
blank
end
Expand Down
10 changes: 8 additions & 2 deletions lib/review/rstbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,10 @@ def texequation(lines)
end

def table_header(id, caption)
blank
puts ".. _#{id}:"
unless id.nil?
blank
puts ".. _#{id}:"
end
blank
puts ".. list-table:: #{compile_inline(caption)}"
puts " :header-rows: 1"
Expand Down Expand Up @@ -352,6 +354,10 @@ def table_end
blank
end

def emtable(lines, caption = nil)
table(lines, nil, caption)
end

def comment(lines, comment = nil)
puts lines.map{|line| " .. #{line}"}.join("")
end
Expand Down
50 changes: 46 additions & 4 deletions lib/review/topbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,55 @@ def texequation(lines)
blank
end

def table_header(id, caption)
def table(lines, id = nil, caption = nil)
blank
puts "◆→開始:#{@titles["table"]}←◆"
if get_chap.nil?
puts "#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}"

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)

begin
table_header id, caption unless caption.nil?
rescue KeyError
error "no such table: #{id}"
end
return if rows.empty?
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
puts "#{I18n.t("table")}#{I18n.t("format_number", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}"
rows.each do |cols|
h, *cs = *cols
tr([th(h)] + cs.map {|s| td(s) })
end
end
table_end
end

def table_header(id, caption)
if id.nil?
puts compile_inline(caption)
else
if get_chap.nil?
puts "#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}"
else
puts "#{I18n.t("table")}#{I18n.t("format_number", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix_idgxml")}#{compile_inline(caption)}"
end
end
blank
end
Expand Down
5 changes: 5 additions & 0 deletions templates/latex/layout.tex.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<%- else -%>
\usepackage[deluxe]{otf}
<%- end -%>
\usepackage{caption}
\usepackage{suffix}
\usepackage[T1]{fontenc}\usepackage{textcomp}%T1/TS1
% \usepackage{lmodern}
\usepackage[dvipdfmx]{graphicx}
Expand Down Expand Up @@ -136,6 +138,9 @@
\newcommand{\reviewtablecaption}[1]{%
\caption{#1}}

\WithSuffix\newcommand\reviewtablecaption*[1]{%
\caption*{#1}}

\newcommand{\reviewimgtablecaption}[1]{%
\caption{#1}\vspace{-3mm}}

Expand Down
5 changes: 5 additions & 0 deletions test/assets/test_template.tex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
% \usepackage{fixltx2e}[2006/09/13 v1.1m]

\usepackage[deluxe,uplatex]{otf}
\usepackage{caption}
\usepackage{suffix}
\usepackage[T1]{fontenc}\usepackage{textcomp}%T1/TS1
% \usepackage{lmodern}
\usepackage[dvipdfmx]{graphicx}
Expand Down Expand Up @@ -96,6 +98,9 @@
\newcommand{\reviewtablecaption}[1]{%
\caption{#1}}

\WithSuffix\newcommand\reviewtablecaption*[1]{%
\caption*{#1}}

\newcommand{\reviewimgtablecaption}[1]{%
\caption{#1}\vspace{-3mm}}

Expand Down
6 changes: 6 additions & 0 deletions test/test_htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,12 @@ def @chapter.table(id)
assert_equal %Q|<p><span class="tableref">表1.1</span></p>\n|, actual
end

def test_emtable
actual = compile_block("//emtable[foo]{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n//emtable{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n")
assert_equal %Q|<div class="table">\n<p class="caption">foo</p>\n<table>\n<tr><th>aaa</th><th>bbb</th></tr>\n<tr><td>ccc</td><td>ddd&lt;&gt;&amp;</td></tr>\n</table>\n</div>\n<div class="table">\n<table>\n<tr><th>aaa</th><th>bbb</th></tr>\n<tr><td>ccc</td><td>ddd&lt;&gt;&amp;</td></tr>\n</table>\n</div>\n|,
actual
end

def test_imgtable
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1, 'sample img')
Expand Down
6 changes: 6 additions & 0 deletions test/test_idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ def test_customize_mmtopt
assert_equal %Q|<table><tbody xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" aid:table="table" aid:trows="1" aid:tcols="1"><td xyh="1,1,0" aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="28.458">A</td></tbody></table>|, actual
end

def test_emtable
actual = compile_block("//emtable[foo]{\nA\n//}\n//emtable{\nA\n//}")
assert_equal %Q|<table><caption>foo</caption><tbody xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" aid:table="table" aid:trows="1" aid:tcols="1"><td xyh="1,1,0" aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="28.345">A</td></tbody></table><table><tbody xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" aid:table="table" aid:trows="1" aid:tcols="1"><td xyh="1,1,0" aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="28.345">A</td></tbody></table>|,
actual
end

def test_inline_br
actual = compile_inline("@<br>{}")
assert_equal %Q|\n|, actual
Expand Down
6 changes: 6 additions & 0 deletions test/test_latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ def test_customize_cellwidth
assert_equal %Q(\\begin{reviewtable}{|p{5mm}|cr|}\n\\hline\n\\reviewth{A} & B & C \\\\ \\hline\n\\end{reviewtable}\n), actual
end

def test_emtable
actual = compile_block("//emtable[foo]{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n//emtable{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n")
assert_equal "\\begin{table}[h]\n\\reviewtablecaption*{foo}\n\\begin{reviewtable}{|l|l|}\n\\hline\n\\reviewth{aaa} & \\reviewth{bbb} \\\\ \\hline\nccc & ddd\\textless{}\\textgreater{}\\& \\\\ \\hline\n\\end{reviewtable}\n\\end{table}\n\n\\begin{reviewtable}{|l|l|}\n\\hline\n\\reviewth{aaa} & \\reviewth{bbb} \\\\ \\hline\nccc & ddd\\textless{}\\textgreater{}\\& \\\\ \\hline\n\\end{reviewtable}\n",
actual
end

def test_imgtable
def @chapter.image(id)
item = Book::ImageIndex::Item.new("sampleimg",1, 'sample img')
Expand Down
5 changes: 5 additions & 0 deletions test/test_rstbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ def test_inline_in_table
assert_equal %Q| * - ★1☆\n - ▲2☆\n * - ★3☆\n - ▲4☆<>&\n\n|, actual
end

def test_emtable
actual = compile_block("//emtable[foo]{\nA\n//}\n//emtable{\nA\n//}")
assert_equal %Q|.. list-table:: foo\n :header-rows: 1\n\n * - A\n\n * - A\n\n|, actual
end

def test_paragraph
actual = compile_block("foo\nbar\n")
assert_equal %Q|foobar\n\n|, actual
Expand Down
22 changes: 21 additions & 1 deletion test/test_topbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_inline_comment_for_draft

def test_inline_in_table
actual = compile_block("//table{\n★1☆\t▲2☆\n------------\n★3☆\t▲4☆<>&\n//}\n")
assert_equal %Q|★★1☆☆\t★▲2☆☆\n★3☆\t▲4☆<>&\n◆→終了:表←◆\n\n|, actual
assert_equal %Q|◆→開始:表←◆\n★★1☆☆\t★▲2☆☆\n★3☆\t▲4☆<>&\n◆→終了:表←◆\n\n|, actual
end

def test_paragraph
Expand Down Expand Up @@ -210,6 +210,26 @@ def @chapter.bibpaper(id)
assert_equal %Q|[1]|, compile_inline("@<bib>{samplebib}")
end

def test_table
actual = compile_block("//table{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n")
assert_equal %Q|◆→開始:表←◆\n★aaa☆\t★bbb☆\nccc\tddd<>&\n◆→終了:表←◆\n\n|,
actual
end

def test_inline_table
def @chapter.table(id)
Book::TableIndex::Item.new("sampletable",1)
end
actual = compile_block("@<table>{sampletest}\n")
assert_equal %Q|表1.1\n|, actual
end

def test_emtable
actual = compile_block("//emtable[foo]{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n//emtable{\naaa\tbbb\n------------\nccc\tddd<>&\n//}\n")
assert_equal %Q|◆→開始:表←◆\nfoo\n\n★aaa☆\t★bbb☆\nccc\tddd<>&\n◆→終了:表←◆\n\n◆→開始:表←◆\n★aaa☆\t★bbb☆\nccc\tddd<>&\n◆→終了:表←◆\n\n|,
actual
end

def test_major_blocks
actual = compile_block("//note{\nA\n\nB\n//}\n//note[caption]{\nA\n//}")
expected = %Q(◆→開始:ノート←◆\nA\nB\n◆→終了:ノート←◆\n\n◆→開始:ノート←◆\n■caption\nA\n◆→終了:ノート←◆\n\n)
Expand Down

0 comments on commit 765a9bd

Please sign in to comment.