Skip to content

Commit

Permalink
Merge pull request #1361 from kmuto/caption-position
Browse files Browse the repository at this point in the history
キャプションの上または下を設定するcaption_positionの実装
  • Loading branch information
kmuto authored Jun 24, 2020
2 parents cc3be1e + f2722b8 commit b433dad
Show file tree
Hide file tree
Showing 13 changed files with 1,074 additions and 93 deletions.
8 changes: 8 additions & 0 deletions doc/config.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ toc: true
# 省略した場合はnull (挿入しない)。別途unicode-eaw gemファイルが必要
# join_lines_by_lang: null

# 図・表・コードリスト・数式のキャプション位置。
# 値はtop(上)またはbottom(下)でデフォルトは以下のとおり
# caption_position:
# image: bottom
# table: top
# list: top
# equation: top

# review-toc向けのヒント情報
# (文字幅を考慮した行数計測には、別途unicode-eaw gemファイルが必要)
# ページあたりの行数文字数を用紙サイズで指定する(A5 or B5)
Expand Down
33 changes: 23 additions & 10 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2002-2019 Minero Aoki, Kenshi Muto
# Copyright (c) 2002-2020 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -142,25 +142,28 @@ def line_num

def list(lines, id, caption, lang = nil)
begin
list_header(id, caption, lang)
list_header(id, caption, lang) if caption_top?('list')
list_body(id, lines, lang)
list_header(id, caption, lang) unless caption_top?('list')
rescue KeyError
error "no such list: #{id}"
end
list_body(id, lines, lang)
end

def listnum(lines, id, caption, lang = nil)
begin
list_header(id, caption, lang)
list_header(id, caption, lang) if caption_top?('list')
listnum_body(lines, lang)
list_header(id, caption, lang) unless caption_top?('list')
rescue KeyError
error "no such list: #{id}"
end
listnum_body(lines, lang)
end

def source(lines, caption = nil, lang = nil)
source_header(caption)
source_header(caption) if caption_top?('list')
source_body(lines, lang)
source_header(caption) unless caption_top?('list')
end

def image(lines, id, caption, metric = nil)
Expand All @@ -175,15 +178,18 @@ def image(lines, id, caption, metric = nil)
def table(lines, id = nil, caption = nil)
sepidx, rows = parse_table_rows(lines)
begin
if caption.present?
if caption_top?('table') && caption.present?
table_header(id, caption)
end
table_begin(rows.first.size)
table_rows(sepidx, rows)
table_end
if !caption_top?('table') && 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 table_row_separator_regexp
Expand Down Expand Up @@ -680,5 +686,12 @@ def detab(str, num = nil)
def escape(str)
str
end

def caption_top?(type)
unless %w[top bottom].include?(@book.config['caption_position'][type])
warn("invalid caption_position/#{type} parameter. 'top' is assumed")
end
@book.config['caption_position'][type] != 'bottom'
end
end
end # module ReVIEW
6 changes: 6 additions & 0 deletions lib/review/configure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ def self.values
'lineheight' => 10 * 1.2,
'pdfcrop_pixelize_cmd' => 'pdftocairo -%t -r 90 -f %p -l %p -singlefile %i %O',
'dvipng_cmd' => 'dvipng -T tight -z 9 -p %p -l %p -o %o %i'
},
'caption_position' => {
'list' => 'top',
'image' => 'bottom',
'table' => 'top',
'equation' => 'top'
}
]
conf.maker = nil
Expand Down
72 changes: 56 additions & 16 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2008-2019 Minero Aoki, Kenshi Muto, Masayoshi Takahashi,
# Copyright (c) 2008-2020 Minero Aoki, Kenshi Muto, Masayoshi Takahashi,
# KADO Masanori
# 2002-2007 Minero Aoki
#
Expand Down Expand Up @@ -286,15 +286,25 @@ def shoot(lines, caption = nil)
end

def box(lines, caption = nil)
puts %Q(<div class="syntax">)
captionstr = nil
if caption.present?
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
captionstr = %Q(<p class="caption">#{compile_inline(caption)}</p>)
end
puts %Q(<div class="syntax">)

if caption_top?('list') && caption.present?
puts captionstr
end

print %Q(<pre class="syntax">)
lines.each do |line|
puts detab(line)
end
puts '</pre>'

if !caption_top?('list') && caption.present?
puts captionstr
end
puts '</div>'
end

Expand Down Expand Up @@ -444,7 +454,7 @@ def listnum_body(lines, lang)

def emlist(lines, caption = nil, lang = nil)
puts %Q(<div class="emlist-code">)
if caption.present?
if caption_top?('list') && caption.present?
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
end
class_names = ['emlist']
Expand All @@ -455,12 +465,15 @@ def emlist(lines, caption = nil, lang = nil)
lexer = lang
puts highlight(body: body, lexer: lexer, format: 'html')
puts '</pre>'
if !caption_top?('list') && caption.present?
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
end
puts '</div>'
end

def emlistnum(lines, caption = nil, lang = nil)
puts %Q(<div class="emlistnum-code">)
if caption.present?
if caption_top?('list') && caption.present?
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
end

Expand All @@ -482,19 +495,29 @@ def emlistnum(lines, caption = nil, lang = nil)
puts '</pre>'
end

if !caption_top?('list') && caption.present?
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
end

puts '</div>'
end

def cmd(lines, caption = nil)
puts %Q(<div class="cmd-code">)
if caption.present?
if caption_top?('list') && caption.present?
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
end

print %Q(<pre class="cmd">)
body = lines.inject('') { |i, j| i + detab(j) + "\n" }
lexer = 'shell-session'
puts highlight(body: body, lexer: lexer, format: 'html')
puts '</pre>'

if !caption_top?('list') && caption.present?
puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
end

puts '</div>'
end

Expand Down Expand Up @@ -530,12 +553,13 @@ def talk(lines)
def texequation(lines, id = nil, caption = '')
if id
puts %Q(<div id="#{normalize_id(id)}" class="caption-equation">)
texequation_header(id, caption)
texequation_header(id, caption) if caption_top?('equation')
end

texequation_body(lines)

if id
texequation_header(id, caption) unless caption_top?('equation')
puts '</div>'
end
end
Expand Down Expand Up @@ -603,20 +627,22 @@ def result_metric(array)
def image_image(id, caption, metric)
metrics = parse_metric('html', metric)
puts %Q(<div id="#{normalize_id(id)}" class="image">)
image_header(id, caption) if caption_top?('image')
puts %Q(<img src="#{@chapter.image(id).path.sub(%r{\A\./}, '')}" alt="#{escape(compile_inline(caption))}"#{metrics} />)
image_header(id, caption)
image_header(id, caption) unless caption_top?('image')
puts '</div>'
end

def image_dummy(id, caption, lines)
warn "image not bound: #{id}"
puts %Q(<div id="#{normalize_id(id)}" class="image">)
image_header(id, caption) if caption_top?('image')
puts %Q(<pre class="dummyimage">)
lines.each do |line|
puts detab(line)
end
puts '</pre>'
image_header(id, caption)
image_header(id, caption) unless caption_top?('image')
puts '</div>'
end

Expand Down Expand Up @@ -679,15 +705,19 @@ def imgtable(lines, id, caption = nil, metric = nil)

puts %Q(<div id="#{normalize_id(id)}" class="imgtable image">)
begin
if caption.present?
if caption_top?('table') && caption.present?
table_header(id, caption)
end

imgtable_image(id, caption, metric)

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

imgtable_image(id, caption, metric)

puts '</div>'
end

Expand Down Expand Up @@ -724,7 +754,19 @@ def footnote(id, str)
def indepimage(lines, id, caption = '', metric = nil)
metrics = parse_metric('html', metric)
caption = '' unless caption.present?
caption_str = nil
if caption.present?
caption_str = <<-EOS
<p class="caption">
#{I18n.t('numberless_image')}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
</p>
EOS
end

puts %Q(<div id="#{normalize_id(id)}" class="image">)
if caption_top?('image') && caption.present?
puts caption_str
end
begin
puts %Q(<img src="#{@chapter.image(id).path.sub(%r{\A\./}, '')}" alt="#{escape(compile_inline(caption))}"#{metrics} />)
rescue
Expand All @@ -738,10 +780,8 @@ def indepimage(lines, id, caption = '', metric = nil)
end
end

if caption.present?
puts %Q(<p class="caption">)
puts %Q(#{I18n.t('numberless_image')}#{I18n.t('caption_prefix')}#{compile_inline(caption)})
puts '</p>'
if !caption_top?('image') && caption.present?
puts caption_str
end
puts '</div>'
end
Expand Down
Loading

0 comments on commit b433dad

Please sign in to comment.