#{I18n.t('equation')}#{I18n.t('format_number_header', [get_chap, @chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
#{I18n.t('equation')}#{I18n.t('format_number_header_without_chapter', [@chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
'
end
def handle_metric(str)
if str =~ /\Ascale=([\d.]+)\Z/
return { 'class' => sprintf('width-%03dper', ($1.to_f * 100).round) }
end
k, v = str.split('=', 2)
{ k => v.sub(/\A["']/, '').sub(/["']\Z/, '') }
end
def result_metric(array)
attrs = {}
array.each do |item|
k = item.keys[0]
if attrs[k]
attrs[k] << item[k]
else
attrs[k] = [item[k]]
end
end
' ' + attrs.map { |k, v| %Q(#{k}="#{v.join(' ')}") }.join(' ')
end
def image_image(id, caption, metric)
metrics = parse_metric('html', metric)
puts %Q(
'
end
def image_dummy(id, caption, lines)
warn "image not bound: #{id}"
puts %Q(
)
if get_chap
puts %Q(#{I18n.t('image')}#{I18n.t('format_number_header', [get_chap, @chapter.image(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)})
else
puts %Q(#{I18n.t('image')}#{I18n.t('format_number_header_without_chapter', [@chapter.image(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)})
end
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?
if id
puts %Q(
)
else
puts %Q(
)
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
puts '
'
end
def table_header(id, caption)
if id.nil?
puts %Q(
#{compile_inline(caption)}
)
elsif get_chap
puts %Q(
#{I18n.t('table')}#{I18n.t('format_number_header', [get_chap, @chapter.table(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
)
else
puts %Q(
#{I18n.t('table')}#{I18n.t('format_number_header_without_chapter', [@chapter.table(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
)
end
end
def table_begin(_ncols)
puts '
'
end
def tr(rows)
puts "#{rows.join}
"
end
def th(str)
"#{str} | "
end
def td(str)
"#{str} | "
end
def table_end
puts '
'
end
def imgtable(lines, id, caption = nil, metric = nil)
unless @chapter.image(id).bound?
warn "image not bound: #{id}"
image_dummy id, caption, lines
return
end
puts %Q(
)
begin
if caption.present?
table_header id, caption
end
rescue KeyError
error "no such table: #{id}"
end
imgtable_image(id, caption, metric)
puts '
'
end
def imgtable_image(id, caption, metric)
metrics = parse_metric('html', metric)
puts %Q(
)
end
def emtable(lines, caption = nil)
table(lines, nil, caption)
end
def comment(lines, comment = nil)
return unless @book.config['draft']
lines ||= []
lines.unshift escape(comment) unless comment.blank?
str = lines.join('
')
puts %Q()
end
def footnote(id, str)
if @book.config['epubversion'].to_i == 3
back = ''
if @book.config['epubmaker'] && @book.config['epubmaker']['back_footnote']
back = %Q(
#{I18n.t('html_footnote_backmark')})
end
# XXX: back link must be located at first of p for Kindle.
puts %Q()
else
puts %Q()
end
end
def indepimage(lines, id, caption = '', metric = nil)
metrics = parse_metric('html', metric)
caption = '' unless caption.present?
puts %Q(
)
begin
puts %Q(
)
rescue
warn "image not bound: #{id}"
if lines
puts %Q(
)
lines.each do |line|
puts detab(line)
end
puts '
'
end
end
if caption.present?
puts %Q(
)
puts %Q(#{I18n.t('numberless_image')}#{I18n.t('caption_prefix')}#{compile_inline(caption)})
puts '
'
end
puts '
'
end
alias_method :numberlessimage, :indepimage
def hr
puts '
'
end
def label(id)
puts %Q(
)
end
def blankline
puts '
'
end
def pagebreak
puts %Q(
)
end
def bpo(lines)
puts '
'
lines.each do |line|
puts detab(line)
end
puts ''
end
def noindent
@noindent = true
end
def inline_labelref(idref)
%Q(
「#{I18n.t('label_marker')}#{escape(idref)}」)
end
alias_method :inline_ref, :inline_labelref
def inline_pageref(id)
error "pageref op is unsupported on this builder: #{id}"
end
def inline_chapref(id)
title = super
if @book.config['chapterlink']
%Q(
#{title})
else
title
end
rescue KeyError
error "unknown chapter: #{id}"
end
def inline_chap(id)
if @book.config['chapterlink']
%Q(
#{@book.chapter_index.number(id)})
else
@book.chapter_index.number(id)
end
rescue KeyError
error "unknown chapter: #{id}"
end
def inline_title(id)
title = super
if @book.config['chapterlink']
%Q(
#{title})
else
title
end
rescue KeyError
error "unknown chapter: #{id}"
end
def inline_fn(id)
if @book.config['epubversion'].to_i == 3
%Q(
#{I18n.t('html_footnote_refmark', @chapter.footnote(id).number)})
else
%Q(
*#{@chapter.footnote(id).number})
end
rescue KeyError
error "unknown footnote: #{id}"
end
def compile_ruby(base, ruby)
if @book.htmlversion == 5
%Q(
#{escape(base)})
else
%Q(
#{escape(base)})
end
end
def compile_kw(word, alt)
%Q(
) +
if alt
then escape(word + " (#{alt.strip})")
else escape(word)
end +
""
end
def inline_i(str)
%Q(
#{escape(str)})
end
def inline_b(str)
%Q(
#{escape(str)})
end
def inline_ami(str)
%Q(
#{escape(str)})
end
def inline_bou(str)
%Q(
#{escape(str)})
end
def inline_tti(str)
if @book.htmlversion == 5
%Q(
#{escape(str)}
)
else
%Q(
#{escape(str)})
end
end
def inline_ttb(str)
if @book.htmlversion == 5
%Q(
#{escape(str)}
)
else
%Q(
#{escape(str)})
end
end
def inline_dtp(str)
""
end
def inline_code(str)
if @book.htmlversion == 5
%Q(
#{escape(str)}
)
else
%Q(
#{escape(str)})
end
end
def inline_idx(str)
%Q(#{escape(str)})
end
def inline_hidx(str)
%Q()
end
def inline_br(_str)
'
'
end
def inline_m(str)
if @book.config['mathml']
require 'math_ml'
require 'math_ml/symbol/character_reference'
parser = MathML::LaTeX::Parser.new(symbol: MathML::Symbol::CharacterReference)
%Q(
#{parser.parse(str, nil)})
elsif @book.config['imgmath']
math_str = '$' + str + '$'
key = Digest::SHA256.hexdigest(str)
math_dir = File.join(@book.config['imagedir'], '_review_math')
Dir.mkdir(math_dir) unless Dir.exist?(math_dir)
img_path = File.join(math_dir, "_gen_#{key}.#{@book.config['imgmath_options']['format']}")
if @book.config.check_version('2', exception: false)
make_math_image(math_str, img_path)
%Q(
)
else
defer_math_image(math_str, img_path, key)
%Q(
)
end
else
%Q(
#{escape(str)})
end
end
def text(str)
str
end
def bibpaper(lines, id, caption)
puts %Q(
)
bibpaper_header id, caption
bibpaper_bibpaper id, caption, lines unless lines.empty?
puts '
'
end
def bibpaper_header(id, caption)
print %Q(
)
print "[#{@chapter.bibpaper(id).number}]"
print ''
puts " #{compile_inline(caption)}"
end
def bibpaper_bibpaper(_id, _caption, lines)
print split_paragraph(lines).join
end
def inline_bib(id)
%Q(
[#{@chapter.bibpaper(id).number}])
rescue KeyError
error "unknown bib: #{id}"
end
def inline_hd_chap(chap, id)
n = chap.headline_index.number(id)
if n.present? && chap.number && over_secnolevel?(n)
str = I18n.t('hd_quote', [n, compile_inline(chap.headline(id).caption)])
else
str = I18n.t('hd_quote_without_number', compile_inline(chap.headline(id).caption))
end
if @book.config['chapterlink']
anchor = 'h' + n.gsub('.', '-')
%Q(
#{str})
else
str
end
rescue KeyError
error "unknown headline: #{id}"
end
def column_label(id, chapter = @chapter)
num = chapter.column(id).number
"column-#{num}"
end
private :column_label
def inline_column_chap(chapter, id)
if @book.config['chapterlink']
%Q(
#{I18n.t('column', compile_inline(chapter.column(id).caption))})
else
I18n.t('column', compile_inline(chapter.column(id).caption))
end
rescue KeyError
error "unknown column: #{id}"
end
def inline_list(id)
str = super(id)
chapter, id = extract_chapter_id(id)
if @book.config['chapterlink']
%Q(
#{str})
else
%Q(
#{str})
end
end
def inline_table(id)
str = super(id)
chapter, id = extract_chapter_id(id)
if @book.config['chapterlink']
%Q(
#{str})
else
%Q(
#{str})
end
end
def inline_img(id)
str = super(id)
chapter, id = extract_chapter_id(id)
if @book.config['chapterlink']
%Q(
#{str})
else
%Q(
#{str})
end
end
def inline_eq(id)
str = super(id)
chapter, id = extract_chapter_id(id)
if @book.config['chapterlink']
%Q(
#{str})
else
%Q(
#{str})
end
end
def inline_asis(str, tag)
%Q(<#{tag}>#{escape(str)}#{tag}>)
end
def inline_abbr(str)
inline_asis(str, 'abbr')
end
def inline_acronym(str)
inline_asis(str, 'acronym')
end
def inline_cite(str)
inline_asis(str, 'cite')
end
def inline_dfn(str)
inline_asis(str, 'dfn')
end
def inline_em(str)
inline_asis(str, 'em')
end
def inline_kbd(str)
inline_asis(str, 'kbd')
end
def inline_samp(str)
inline_asis(str, 'samp')
end
def inline_strong(str)
inline_asis(str, 'strong')
end
def inline_var(str)
inline_asis(str, 'var')
end
def inline_big(str)
inline_asis(str, 'big')
end
def inline_small(str)
inline_asis(str, 'small')
end
def inline_sub(str)
inline_asis(str, 'sub')
end
def inline_sup(str)
inline_asis(str, 'sup')
end
def inline_tt(str)
if @book.htmlversion == 5
%Q(
#{escape(str)}
)
else
%Q(
#{escape(str)})
end
end
def inline_del(str)
inline_asis(str, 'del')
end
def inline_ins(str)
inline_asis(str, 'ins')
end
def inline_u(str)
%Q(
#{escape(str)})
end
def inline_recipe(str)
%Q(
「#{escape(str)}」)
end
def inline_icon(id)
begin
%Q(
)
rescue
warn "image not bound: #{id}"
%Q(
missing image: #{id}
)
end
end
def inline_uchar(str)
%Q(#{str};)
end
def inline_comment(str)
if @book.config['draft']
%Q()
else
''
end
end
def inline_tcy(str)
# 縦中横用のtcy、uprightのCSSスタイルについては電書協ガイドラインを参照
style = 'tcy'
if str.size == 1 && str.match(/[[:ascii:]]/)
style = 'upright'
end
%Q(
#{escape(str)})
end
def inline_balloon(str)
%Q(
#{escape_html(str)})
end
def inline_raw(str)
super(str)
end
def nofunc_text(str)
escape(str)
end
def compile_href(url, label)
if @book.config['externallink']
%Q(
#{label.nil? ? escape(url) : escape(label)})
else
label.nil? ? escape(url) : I18n.t('external_link', [escape(label), escape(url)])
end
end
def flushright(lines)
puts split_paragraph(lines).join("\n").gsub('
', %Q(
))
end
def centering(lines)
puts split_paragraph(lines).join("\n").gsub('
', %Q(
))
end
def image_ext
'png'
end
def olnum(num)
@ol_num = num.to_i
end
def defer_math_image(str, path, key)
# for Re:VIEW >3
File.open(File.join(File.dirname(path), '__IMGMATH_BODY__.tex'), 'a+') do |f|
f.puts str
f.puts '\\clearpage'
end
File.open(File.join(File.dirname(path), '__IMGMATH_BODY__.map'), 'a+') do |f|
f.puts key
end
end
def make_math_image(str, path, fontsize = 12)
# Re:VIEW 2 compatibility
fontsize2 = (fontsize * 1.2).round.to_i
texsrc = <<-EOB
\\documentclass[12pt]{article}
\\usepackage[utf8]{inputenc}
\\usepackage{amsmath}
\\usepackage{amsthm}
\\usepackage{amssymb}
\\usepackage{amsfonts}
\\usepackage{anyfontsize}
\\usepackage{bm}
\\pagestyle{empty}
\\begin{document}
\\fontsize{#{fontsize}}{#{fontsize2}}\\selectfont #{str}
\\end{document}
EOB
Dir.mktmpdir do |tmpdir|
tex_path = File.join(tmpdir, 'tmpmath.tex')
dvi_path = File.join(tmpdir, 'tmpmath.dvi')
File.write(tex_path, texsrc)
cmd = "latex --interaction=nonstopmode --output-directory=#{tmpdir} #{tex_path} && dvipng -T tight -z9 -o #{path} #{dvi_path}"
out, status = Open3.capture2e(cmd)
unless status.success?
error "latex compile error\n\nError log:\n" + out
end
end
end
end
end # module ReVIEW