Class: ReVIEW::HTMLBuilder

Inherits:
Builder show all
Includes:
HTMLUtils, TextUtils
Defined in:
lib/review/htmlbuilder.rb

Direct Known Subclasses

EPUBBuilder

Constant Summary

Constants inherited from Builder

Builder::CAPTION_TITLES

Instance Attribute Summary

Attributes inherited from Builder

#doc_status, #location

Attributes included from Loggable

#logger

Instance Method Summary collapse

Methods included from HTMLUtils

#escape, #escape_comment, #highlight, #highlight?, #strip_html, #unescape

Methods included from TextUtils

#add_space?, #detab, #join_lines_to_paragraph, #split_paragraph

Methods inherited from Builder

#beginchild, #bind, #caption_top?, #check_nest, #check_nested_minicolumn, #check_printendnotes, #compile_inline, #detab, #embed, #endchild, #endnote, #escape, #extract_chapter_id, #firstlinenum, #get_chap, #graph, #graph_aafigure, #graph_blockdiag, #graph_gnuplot, #graph_graphviz, #graph_mermaid, #graph_plantuml, #highlight?, #image, #in_minicolumn?, #initialize, #inline_column, #inline_embed, #inline_hd, #inline_href, #inline_imgref, #inline_include, #inline_kw, #inline_ruby, #inline_w, #inline_wb, #line_num, #load_words, #minicolumn_block_name?, #over_secnolevel?, #parse_metric, #parse_table_rows, #previous_list_type, #print, #printendnotes, #puts, #raw, #system_graph, #table_row_separator_regexp, #table_rows, #target_name, #tsize

Methods included from Loggable

#app_error, #debug, #error, #error!, #warn

Constructor Details

This class inherits a constructor from ReVIEW::Builder

Instance Method Details

#best(lines, caption = nil) ⇒ Object



312
313
314
# File 'lib/review/htmlbuilder.rb', line 312

def best(lines, caption = nil)
  captionblock('best', lines, caption)
end

#bibpaper(lines, id, caption) ⇒ Object



1076
1077
1078
1079
1080
1081
# File 'lib/review/htmlbuilder.rb', line 1076

def bibpaper(lines, id, caption)
  puts %Q(<div class="bibpaper">)
  bibpaper_header(id, caption)
  bibpaper_bibpaper(id, caption, lines) unless lines.empty?
  puts '</div>'
end

#bibpaper_bibpaper(_id, _caption, lines) ⇒ Object



1090
1091
1092
# File 'lib/review/htmlbuilder.rb', line 1090

def bibpaper_bibpaper(_id, _caption, lines)
  print split_paragraph(lines).join
end

#bibpaper_header(id, caption) ⇒ Object



1083
1084
1085
1086
1087
1088
# File 'lib/review/htmlbuilder.rb', line 1083

def bibpaper_header(id, caption)
  print %Q(<a id="bib-#{normalize_id(id)}">)
  print "[#{@chapter.bibpaper(id).number}]"
  print '</a>'
  puts " #{compile_inline(caption)}"
end

#blanklineObject



893
894
895
# File 'lib/review/htmlbuilder.rb', line 893

def blankline
  puts '<p><br /></p>'
end

#box(lines, caption = nil) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/review/htmlbuilder.rb', line 344

def box(lines, caption = nil)
  captionstr = nil
  if caption.present?
    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

#bpo(lines) ⇒ Object



901
902
903
904
905
906
907
# File 'lib/review/htmlbuilder.rb', line 901

def bpo(lines)
  puts '<bpo>'
  lines.each do |line|
    puts detab(line)
  end
  puts '</bpo>'
end

#captionblock(type, lines, caption) ⇒ Object



285
286
287
288
289
290
291
292
293
294
# File 'lib/review/htmlbuilder.rb', line 285

def captionblock(type, lines, caption)
  check_nested_minicolumn
  puts %Q(<div class="#{type}">)
  if caption.present?
    puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
  end
  blocked_lines = split_paragraph(lines)
  puts blocked_lines.join("\n")
  puts '</div>'
end

#caution(lines, caption = nil) ⇒ Object



324
325
326
# File 'lib/review/htmlbuilder.rb', line 324

def caution(lines, caption = nil)
  captionblock('caution', lines, caption)
end

#centering(lines) ⇒ Object



1332
1333
1334
# File 'lib/review/htmlbuilder.rb', line 1332

def centering(lines)
  puts split_paragraph(lines).join("\n").gsub('<p>', %Q(<p class="center">))
end

#close_sectionsObject



111
112
113
# File 'lib/review/htmlbuilder.rb', line 111

def close_sections
  "</section>\n" * @section_stack.size
end

#cmd(lines, caption = nil) ⇒ Object



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/review/htmlbuilder.rb', line 579

def cmd(lines, caption = nil)
  puts %Q(<div class="cmd-code">)
  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

#column_begin(level, label, caption) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/review/htmlbuilder.rb', line 238

def column_begin(level, label, caption)
  puts %Q(<div class="column">)

  @column += 1
  puts if level > 1
  a_id = %Q(<a id="column-#{@column}"></a>)

  if caption.empty?
    puts a_id if label
  elsif label
    puts %Q(<h#{level} id="#{normalize_id(label)}">#{a_id}#{compile_inline(caption)}</h#{level}>)
  else
    puts %Q(<h#{level}>#{a_id}#{compile_inline(caption)}</h#{level}>)
  end
end

#column_end(_level) ⇒ Object



254
255
256
# File 'lib/review/htmlbuilder.rb', line 254

def column_end(_level)
  puts '</div>'
end

#comment(lines, comment = nil) ⇒ Object



810
811
812
813
814
815
816
817
# File 'lib/review/htmlbuilder.rb', line 810

def comment(lines, comment = nil)
  return unless @book.config['draft']

  lines ||= []
  lines.unshift(escape(comment)) unless comment.blank?
  str = lines.join('<br />')
  puts %Q(<div class="draft-comment">#{str}</div>)
end

#compile_href(url, label) ⇒ Object



1320
1321
1322
1323
1324
1325
1326
# File 'lib/review/htmlbuilder.rb', line 1320

def compile_href(url, label)
  if @book.config['externallink']
    %Q(<a href="#{escape(url)}" class="link">#{label.nil? ? escape(url) : escape(label)}</a>)
  else
    label.nil? ? escape(url) : I18n.t('external_link', [escape(label), escape(url)])
  end
end

#compile_kw(word, alt) ⇒ Object



979
980
981
982
983
984
985
986
987
# File 'lib/review/htmlbuilder.rb', line 979

def compile_kw(word, alt)
  %Q(<b class="kw">) +
    if alt
      escape(word + " (#{alt.strip})")
    else
      escape(word)
    end +
    "</b><!-- IDX:#{escape_comment(escape(word))} -->"
end

#compile_ruby(base, ruby) ⇒ Object



971
972
973
974
975
976
977
# File 'lib/review/htmlbuilder.rb', line 971

def compile_ruby(base, ruby)
  if @book.htmlversion == 5
    %Q(<ruby>#{escape(base)}<rp>#{I18n.t('ruby_prefix')}</rp><rt>#{escape(ruby)}</rt><rp>#{I18n.t('ruby_postfix')}</rp></ruby>)
  else
    %Q(<ruby><rb>#{escape(base)}</rb><rp>#{I18n.t('ruby_prefix')}</rp><rt>#{ruby}</rt><rp>#{I18n.t('ruby_postfix')}</rp></ruby>)
  end
end

#dd(lines) ⇒ Object



430
431
432
# File 'lib/review/htmlbuilder.rb', line 430

def dd(lines)
  puts "<dd>#{join_lines_to_paragraph(lines)}</dd>"
end

#dl_beginObject



422
423
424
# File 'lib/review/htmlbuilder.rb', line 422

def dl_begin
  puts '<dl>'
end

#dl_endObject



434
435
436
# File 'lib/review/htmlbuilder.rb', line 434

def dl_end
  puts '</dl>'
end

#doorquote(lines, ref) ⇒ Object



612
613
614
615
616
617
618
# File 'lib/review/htmlbuilder.rb', line 612

def doorquote(lines, ref)
  blocked_lines = split_paragraph(lines)
  puts %Q(<blockquote style="text-align:right;">)
  puts blocked_lines.join("\n")
  puts %Q(<p>#{ref}より</p>)
  puts '</blockquote>'
end

#dt(line) ⇒ Object



426
427
428
# File 'lib/review/htmlbuilder.rb', line 426

def dt(line)
  puts "<dt>#{line}</dt>"
end

#emlist(lines, caption = nil, lang = nil) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/review/htmlbuilder.rb', line 529

def emlist(lines, caption = nil, lang = nil)
  puts %Q(<div class="emlist-code">)
  if caption_top?('list') && caption.present?
    puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
  end
  class_names = ['emlist']
  class_names.push("language-#{lang}") unless lang.blank?
  class_names.push('highlight') if highlight?
  print %Q(<pre class="#{class_names.join(' ')}">)
  body = lines.inject('') { |i, j| i + detab(j) + "\n" }
  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

#emlistnum(lines, caption = nil, lang = nil) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'lib/review/htmlbuilder.rb', line 548

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

  body = lines.inject('') { |i, j| i + detab(j) + "\n" }
  lexer = lang
  first_line_number = line_num
  hs = highlight(body: body, lexer: lexer, format: 'html', linenum: true,
                 options: { linenostart: first_line_number })
  if highlight?
    puts hs
  else
    class_names = ['emlist']
    class_names.push("language-#{lang}") unless lang.blank?
    class_names.push('highlight') if highlight?
    print %Q(<pre class="#{class_names.join(' ')}">)
    hs.split("\n").each_with_index do |line, i|
      puts detab((i + first_line_number).to_s.rjust(2) + ': ' + line)
    end
    puts '</pre>'
  end

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

  puts '</div>'
end

#emtable(lines, caption = nil) ⇒ Object



806
807
808
# File 'lib/review/htmlbuilder.rb', line 806

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

#endnote_beginObject



832
833
834
# File 'lib/review/htmlbuilder.rb', line 832

def endnote_begin
  puts %Q(<div class="endnotes">)
end

#endnote_endObject



836
837
838
# File 'lib/review/htmlbuilder.rb', line 836

def endnote_end
  puts %Q(</div>)
end

#endnote_item(id) ⇒ Object



840
841
842
843
844
845
846
# File 'lib/review/htmlbuilder.rb', line 840

def endnote_item(id)
  back = ''
  if @book.config['epubmaker'] && @book.config['epubmaker']['back_footnote']
    back = %Q(<a href="#endnoteb-#{normalize_id(id)}">#{I18n.t('html_footnote_backmark')}</a>)
  end
  puts %Q(<div class="endnote" id="endnote-#{normalize_id(id)}"><p class="endnote">#{back}#{I18n.t('html_endnote_textmark', @chapter.endnote(id).number)}#{compile_inline(@chapter.endnote(id).content)}</p></div>)
end

#extnameObject



42
43
44
# File 'lib/review/htmlbuilder.rb', line 42

def extname
  ".#{@book.config['htmlext']}"
end

#flushright(lines) ⇒ Object



1328
1329
1330
# File 'lib/review/htmlbuilder.rb', line 1328

def flushright(lines)
  puts split_paragraph(lines).join("\n").gsub('<p>', %Q(<p class="flushright">))
end

#footnote(id, str) ⇒ Object



819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/review/htmlbuilder.rb', line 819

def footnote(id, str)
  if @book.config['epubversion'].to_i == 3
    back = ''
    if @book.config['epubmaker'] && @book.config['epubmaker']['back_footnote']
      back = %Q(<a href="#fnb-#{normalize_id(id)}">#{I18n.t('html_footnote_backmark')}</a>)
    end
    # XXX: back link must be located at first of p for Kindle.
    puts %Q(<div class="footnote" epub:type="footnote" id="fn-#{normalize_id(id)}"><p class="footnote">#{back}#{I18n.t('html_footnote_textmark', @chapter.footnote(id).number)}#{compile_inline(str)}</p></div>)
  else
    puts %Q(<div class="footnote" id="fn-#{normalize_id(id)}"><p class="footnote">[<a href="#fnb-#{normalize_id(id)}">*#{@chapter.footnote(id).number}</a>] #{compile_inline(str)}</p></div>)
  end
end

#handle_metric(str) ⇒ Object



682
683
684
685
686
687
688
689
# File 'lib/review/htmlbuilder.rb', line 682

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

#headline(level, label, caption) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/review/htmlbuilder.rb', line 167

def headline(level, label, caption)
  if use_section?
    print open_section(level)
  end
  prefix, anchor = headline_prefix(level)
  if prefix
    prefix = %Q(<span class="secno">#{prefix}</span>)
  end
  puts '' if level > 1
  a_id = ''
  if anchor
    a_id = %Q(<a id="h#{anchor}"></a>)
  end

  if caption.empty?
    puts a_id if label
  elsif label
    puts %Q(<h#{level} id="#{normalize_id(label)}">#{a_id}#{prefix}#{compile_inline(caption)}</h#{level}>)
  else
    puts %Q(<h#{level}>#{a_id}#{prefix}#{compile_inline(caption)}</h#{level}>)
  end
end

#hrObject



885
886
887
# File 'lib/review/htmlbuilder.rb', line 885

def hr
  puts '<hr />'
end

#image_dummy(id, caption, lines) ⇒ Object



713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/review/htmlbuilder.rb', line 713

def image_dummy(id, caption, lines)
  warn "image not bound: #{id}", location: location
  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) unless caption_top?('image')
  puts '</div>'
end

#image_extObject



1336
1337
1338
# File 'lib/review/htmlbuilder.rb', line 1336

def image_ext
  'png'
end

#image_header(id, caption) ⇒ Object



726
727
728
729
730
731
732
733
734
# File 'lib/review/htmlbuilder.rb', line 726

def image_header(id, caption)
  puts %Q(<p class="caption">)
  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 '</p>'
end

#image_image(id, caption, metric) ⇒ Object



704
705
706
707
708
709
710
711
# File 'lib/review/htmlbuilder.rb', line 704

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) unless caption_top?('image')
  puts '</div>'
end

#imgtable(lines, id, caption = nil, metric = nil) ⇒ Object



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
# File 'lib/review/htmlbuilder.rb', line 776

def imgtable(lines, id, caption = nil, metric = nil)
  unless @chapter.image_bound?(id)
    warn "image not bound: #{id}", location: location
    image_dummy(id, caption, lines)
    return
  end

  puts %Q(<div id="#{normalize_id(id)}" class="imgtable image">)
  begin
    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
    app_error "no such table: #{id}"
  end

  puts '</div>'
end

#imgtable_image(id, caption, metric) ⇒ Object



801
802
803
804
# File 'lib/review/htmlbuilder.rb', line 801

def imgtable_image(id, caption, metric)
  metrics = parse_metric('html', metric)
  puts %Q(<img src="#{@chapter.image(id).path.sub(%r{\A\./}, '')}" alt="#{escape(compile_inline(caption))}"#{metrics} />)
end

#important(lines, caption = nil) ⇒ Object



316
317
318
# File 'lib/review/htmlbuilder.rb', line 316

def important(lines, caption = nil)
  captionblock('important', lines, caption)
end

#indepimage(lines, id, caption = '', metric = nil) ⇒ Object Also known as: numberlessimage



848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'lib/review/htmlbuilder.rb', line 848

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 StandardError
    warn "image not bound: #{id}", location: location
    if lines
      puts %Q(<pre class="dummyimage">)
      lines.each do |line|
        puts detab(line)
      end
      puts '</pre>'
    end
  end

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

#info(lines, caption = nil) ⇒ Object



304
305
306
# File 'lib/review/htmlbuilder.rb', line 304

def info(lines, caption = nil)
  captionblock('info', lines, caption)
end

#inline_abbr(str) ⇒ Object



1202
1203
1204
# File 'lib/review/htmlbuilder.rb', line 1202

def inline_abbr(str)
  inline_asis(str, 'abbr')
end

#inline_acronym(str) ⇒ Object



1206
1207
1208
# File 'lib/review/htmlbuilder.rb', line 1206

def inline_acronym(str)
  inline_asis(str, 'acronym')
end

#inline_ami(str) ⇒ Object



997
998
999
# File 'lib/review/htmlbuilder.rb', line 997

def inline_ami(str)
  %Q(<span class="ami">#{escape(str)}</span>)
end

#inline_asis(str, tag) ⇒ Object



1198
1199
1200
# File 'lib/review/htmlbuilder.rb', line 1198

def inline_asis(str, tag)
  %Q(<#{tag}>#{escape(str)}</#{tag}>)
end

#inline_b(str) ⇒ Object



993
994
995
# File 'lib/review/htmlbuilder.rb', line 993

def inline_b(str)
  %Q(<b>#{escape(str)}</b>)
end

#inline_balloon(str) ⇒ Object



1308
1309
1310
# File 'lib/review/htmlbuilder.rb', line 1308

def inline_balloon(str)
  %Q(<span class="balloon">#{escape_html(str)}</span>)
end

#inline_bib(id) ⇒ Object



1094
1095
1096
1097
1098
# File 'lib/review/htmlbuilder.rb', line 1094

def inline_bib(id)
  %Q(<a href="#{@book.bib_file.gsub(/\.re\Z/, ".#{@book.config['htmlext']}")}#bib-#{normalize_id(id)}">[#{@chapter.bibpaper(id).number}]</a>)
rescue KeyError
  app_error "unknown bib: #{id}"
end

#inline_big(str) ⇒ Object



1238
1239
1240
# File 'lib/review/htmlbuilder.rb', line 1238

def inline_big(str)
  inline_asis(str, 'big')
end

#inline_bou(str) ⇒ Object



1001
1002
1003
# File 'lib/review/htmlbuilder.rb', line 1001

def inline_bou(str)
  %Q(<span class="bou">#{escape(str)}</span>)
end

#inline_br(_str) ⇒ Object



1041
1042
1043
# File 'lib/review/htmlbuilder.rb', line 1041

def inline_br(_str)
  '<br />'
end

#inline_chap(id) ⇒ Object



934
935
936
937
938
939
940
941
942
# File 'lib/review/htmlbuilder.rb', line 934

def inline_chap(id)
  if @book.config['chapterlink']
    %Q(<a href="./#{id}#{extname}">#{@book.chapter_index.number(id)}</a>)
  else
    @book.chapter_index.number(id)
  end
rescue KeyError
  app_error "unknown chapter: #{id}"
end

#inline_chapref(id) ⇒ Object



923
924
925
926
927
928
929
930
931
932
# File 'lib/review/htmlbuilder.rb', line 923

def inline_chapref(id)
  title = super
  if @book.config['chapterlink']
    %Q(<a href="./#{id}#{extname}">#{title}</a>)
  else
    title
  end
rescue KeyError
  app_error "unknown chapter: #{id}"
end

#inline_cite(str) ⇒ Object



1210
1211
1212
# File 'lib/review/htmlbuilder.rb', line 1210

def inline_cite(str)
  inline_asis(str, 'cite')
end

#inline_code(str) ⇒ Object



1025
1026
1027
1028
1029
1030
1031
# File 'lib/review/htmlbuilder.rb', line 1025

def inline_code(str)
  if @book.htmlversion == 5
    %Q(<code class="inline-code tt">#{escape(str)}</code>)
  else
    %Q(<tt class="inline-code">#{escape(str)}</tt>)
  end
end

#inline_column_chap(chapter, id) ⇒ Object



1148
1149
1150
1151
1152
1153
1154
1155
1156
# File 'lib/review/htmlbuilder.rb', line 1148

def inline_column_chap(chapter, id)
  if @book.config['chapterlink']
    %Q(<a href="#{chapter.id}#{extname}##{column_label(id, chapter)}" class="columnref">#{I18n.t('column', compile_inline(chapter.column(id).caption))}</a>)
  else
    I18n.t('column', compile_inline(chapter.column(id).caption))
  end
rescue KeyError
  app_error "unknown column: #{id}"
end

#inline_comment(str) ⇒ Object



1291
1292
1293
1294
1295
1296
1297
# File 'lib/review/htmlbuilder.rb', line 1291

def inline_comment(str)
  if @book.config['draft']
    %Q(<span class="draft-comment">#{escape(str)}</span>)
  else
    ''
  end
end

#inline_del(str) ⇒ Object



1262
1263
1264
# File 'lib/review/htmlbuilder.rb', line 1262

def inline_del(str)
  inline_asis(str, 'del')
end

#inline_dfn(str) ⇒ Object



1214
1215
1216
# File 'lib/review/htmlbuilder.rb', line 1214

def inline_dfn(str)
  inline_asis(str, 'dfn')
end

#inline_dtp(str) ⇒ Object



1021
1022
1023
# File 'lib/review/htmlbuilder.rb', line 1021

def inline_dtp(str)
  "<?dtp #{str} ?>"
end

#inline_em(str) ⇒ Object



1218
1219
1220
# File 'lib/review/htmlbuilder.rb', line 1218

def inline_em(str)
  inline_asis(str, 'em')
end

#inline_endnote(id) ⇒ Object



965
966
967
968
969
# File 'lib/review/htmlbuilder.rb', line 965

def inline_endnote(id)
  %Q(<a id="endnoteb-#{normalize_id(id)}" href="#endnote-#{normalize_id(id)}" class="noteref" epub:type="noteref">#{I18n.t('html_endnote_refmark', @chapter.endnote(id).number)}</a>)
rescue KeyError
  app_error "unknown endnote: #{id}"
end

#inline_eq(id) ⇒ Object



1188
1189
1190
1191
1192
1193
1194
1195
1196
# File 'lib/review/htmlbuilder.rb', line 1188

def inline_eq(id)
  str = super
  chapter, id = extract_chapter_id(id)
  if @book.config['chapterlink']
    %Q(<span class="eqref"><a href="./#{chapter.id}#{extname}##{normalize_id(id)}">#{str}</a></span>)
  else
    %Q(<span class="eqref">#{str}</span>)
  end
end

#inline_fn(id) ⇒ Object



955
956
957
958
959
960
961
962
963
# File 'lib/review/htmlbuilder.rb', line 955

def inline_fn(id)
  if @book.config['epubversion'].to_i == 3
    %Q(<a id="fnb-#{normalize_id(id)}" href="#fn-#{normalize_id(id)}" class="noteref" epub:type="noteref">#{I18n.t('html_footnote_refmark', @chapter.footnote(id).number)}</a>)
  else
    %Q(<a id="fnb-#{normalize_id(id)}" href="#fn-#{normalize_id(id)}" class="noteref">*#{@chapter.footnote(id).number}</a>)
  end
rescue KeyError
  app_error "unknown footnote: #{id}"
end

#inline_hd_chap(chap, id) ⇒ Object



1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
# File 'lib/review/htmlbuilder.rb', line 1100

def inline_hd_chap(chap, id)
  n = chap.headline_index.number(id)
  str = if n.present? && chap.number && over_secnolevel?(n)
          I18n.t('hd_quote', [n, compile_inline(chap.headline(id).caption)])
        else
          I18n.t('hd_quote_without_number', compile_inline(chap.headline(id).caption))
        end
  if @book.config['chapterlink']
    anchor = 'h' + n.tr('.', '-')
    %Q(<a href="#{chap.id}#{extname}##{anchor}">#{str}</a>)
  else
    str
  end
rescue KeyError
  app_error "unknown headline: #{id}"
end

#inline_hidx(str) ⇒ Object



1037
1038
1039
# File 'lib/review/htmlbuilder.rb', line 1037

def inline_hidx(str)
  %Q(<!-- IDX:#{escape_comment(escape(str))} -->)
end

#inline_i(str) ⇒ Object



989
990
991
# File 'lib/review/htmlbuilder.rb', line 989

def inline_i(str)
  %Q(<i>#{escape(str)}</i>)
end

#inline_icon(id) ⇒ Object



1278
1279
1280
1281
1282
1283
1284
1285
# File 'lib/review/htmlbuilder.rb', line 1278

def inline_icon(id)
  begin
    %Q(<img src="#{@chapter.image(id).path.sub(%r{\A\./}, '')}" alt="[#{id}]" />)
  rescue StandardError
    warn "image not bound: #{id}", location: location
    %Q(<pre>missing image: #{id}</pre>)
  end
end

#inline_idx(str) ⇒ Object



1033
1034
1035
# File 'lib/review/htmlbuilder.rb', line 1033

def inline_idx(str)
  %Q(#{escape(str)}<!-- IDX:#{escape_comment(escape(str))} -->)
end

#inline_img(id) ⇒ Object



1178
1179
1180
1181
1182
1183
1184
1185
1186
# File 'lib/review/htmlbuilder.rb', line 1178

def inline_img(id)
  str = super
  chapter, id = extract_chapter_id(id)
  if @book.config['chapterlink']
    %Q(<span class="imgref"><a href="./#{chapter.id}#{extname}##{normalize_id(id)}">#{str}</a></span>)
  else
    %Q(<span class="imgref">#{str}</span>)
  end
end

#inline_ins(str) ⇒ Object



1266
1267
1268
# File 'lib/review/htmlbuilder.rb', line 1266

def inline_ins(str)
  inline_asis(str, 'ins')
end

#inline_kbd(str) ⇒ Object



1222
1223
1224
# File 'lib/review/htmlbuilder.rb', line 1222

def inline_kbd(str)
  inline_asis(str, 'kbd')
end

#inline_labelref(idref) ⇒ Object Also known as: inline_ref



913
914
915
# File 'lib/review/htmlbuilder.rb', line 913

def inline_labelref(idref)
  %Q(<a target='#{escape(idref)}'>「#{I18n.t('label_marker')}#{escape(idref)}」</a>)
end

#inline_list(id) ⇒ Object



1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/review/htmlbuilder.rb', line 1158

def inline_list(id)
  str = super
  chapter, id = extract_chapter_id(id)
  if @book.config['chapterlink']
    %Q(<span class="listref"><a href="./#{chapter.id}#{extname}##{normalize_id(id)}">#{str}</a></span>)
  else
    %Q(<span class="listref">#{str}</span>)
  end
end

#inline_m(str) ⇒ Object



1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/review/htmlbuilder.rb', line 1045

def inline_m(str)
  if @book.config['math_format'] == 'mathml'
    begin
      require 'math_ml'
      require 'math_ml/symbol/character_reference'
    rescue LoadError
      app_error 'not found math_ml'
    end
    parser = MathML::LaTeX::Parser.new(symbol: MathML::Symbol::CharacterReference)
    %Q(<span class="equation">#{parser.parse(str, nil)}</span>)
  elsif @book.config['math_format'] == 'mathjax'
    %Q(<span class="equation">\\( #{str.gsub('<', '\lt{}').gsub('>', '\gt{}').gsub('&', '&amp;')} \\)</span>)
  elsif @book.config['math_format'] == 'imgmath'
    math_str = '$' + str + '$'
    key = Digest::SHA256.hexdigest(str)
    if @book.config.check_version('2', exception: false)
      img_path = @img_math.make_math_image(math_str, key)
      %Q(<span class="equation"><img src="#{img_path}" /></span>)
    else
      img_path = @img_math.defer_math_image(math_str, key)
      %Q(<span class="equation"><img src="#{img_path}" class="math_gen_#{key}" alt="#{escape(str)}" /></span>)
    end
  else
    %Q(<span class="equation">#{escape(str)}</span>)
  end
end

#inline_pageref(id) ⇒ Object



919
920
921
# File 'lib/review/htmlbuilder.rb', line 919

def inline_pageref(id)
  app_error "pageref op is unsupported on this builder: #{id}"
end

#inline_raw(str) ⇒ Object

rubocop:disable Lint/UselessMethodDefinition



1312
1313
1314
# File 'lib/review/htmlbuilder.rb', line 1312

def inline_raw(str) # rubocop:disable Lint/UselessMethodDefinition
  super
end

#inline_recipe(str) ⇒ Object



1274
1275
1276
# File 'lib/review/htmlbuilder.rb', line 1274

def inline_recipe(str)
  %Q(<span class="recipe">「#{escape(str)}」</span>)
end

#inline_samp(str) ⇒ Object



1226
1227
1228
# File 'lib/review/htmlbuilder.rb', line 1226

def inline_samp(str)
  inline_asis(str, 'samp')
end

#inline_sec(id) ⇒ Object



1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/review/htmlbuilder.rb', line 1117

def inline_sec(id)
  if @book.config['chapterlink']
    chap, id2 = extract_chapter_id(id)
    n = chap.headline_index.number(id2)
    anchor = 'h' + n.tr('.', '-')
    %Q(<a href="#{chap.id}#{extname}##{anchor}">#{super}</a>)
  else
    super
  end
rescue KeyError
  app_error "unknown headline: #{id}"
end

#inline_sectitle(id) ⇒ Object



1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
# File 'lib/review/htmlbuilder.rb', line 1130

def inline_sectitle(id)
  if @book.config['chapterlink']
    chap, id2 = extract_chapter_id(id)
    anchor = 'h' + chap.headline_index.number(id2).tr('.', '-')
    %Q(<a href="#{chap.id}#{extname}##{anchor}">#{super}</a>)
  else
    super
  end
rescue KeyError
  app_error "unknown headline: #{id}"
end

#inline_small(str) ⇒ Object



1242
1243
1244
# File 'lib/review/htmlbuilder.rb', line 1242

def inline_small(str)
  inline_asis(str, 'small')
end

#inline_strong(str) ⇒ Object



1230
1231
1232
# File 'lib/review/htmlbuilder.rb', line 1230

def inline_strong(str)
  inline_asis(str, 'strong')
end

#inline_sub(str) ⇒ Object



1246
1247
1248
# File 'lib/review/htmlbuilder.rb', line 1246

def inline_sub(str)
  inline_asis(str, 'sub')
end

#inline_sup(str) ⇒ Object



1250
1251
1252
# File 'lib/review/htmlbuilder.rb', line 1250

def inline_sup(str)
  inline_asis(str, 'sup')
end

#inline_table(id) ⇒ Object



1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/review/htmlbuilder.rb', line 1168

def inline_table(id)
  str = super
  chapter, id = extract_chapter_id(id)
  if @book.config['chapterlink']
    %Q(<span class="tableref"><a href="./#{chapter.id}#{extname}##{normalize_id(id)}">#{str}</a></span>)
  else
    %Q(<span class="tableref">#{str}</span>)
  end
end

#inline_tcy(str) ⇒ Object



1299
1300
1301
1302
1303
1304
1305
1306
# File 'lib/review/htmlbuilder.rb', line 1299

def inline_tcy(str)
  # 縦中横用のtcy、uprightのCSSスタイルについては電書協ガイドラインを参照
  style = 'tcy'
  if str.size == 1 && str.match(/[[:ascii:]]/)
    style = 'upright'
  end
  %Q(<span class="#{style}">#{escape(str)}</span>)
end

#inline_title(id) ⇒ Object



944
945
946
947
948
949
950
951
952
953
# File 'lib/review/htmlbuilder.rb', line 944

def inline_title(id)
  title = super
  if @book.config['chapterlink']
    %Q(<a href="./#{id}#{extname}">#{title}</a>)
  else
    title
  end
rescue KeyError
  app_error "unknown chapter: #{id}"
end

#inline_tt(str) ⇒ Object



1254
1255
1256
1257
1258
1259
1260
# File 'lib/review/htmlbuilder.rb', line 1254

def inline_tt(str)
  if @book.htmlversion == 5
    %Q(<code class="tt">#{escape(str)}</code>)
  else
    %Q(<tt>#{escape(str)}</tt>)
  end
end

#inline_ttb(str) ⇒ Object



1013
1014
1015
1016
1017
1018
1019
# File 'lib/review/htmlbuilder.rb', line 1013

def inline_ttb(str)
  if @book.htmlversion == 5
    %Q(<code class="tt"><b>#{escape(str)}</b></code>)
  else
    %Q(<tt><b>#{escape(str)}</b></tt>)
  end
end

#inline_tti(str) ⇒ Object



1005
1006
1007
1008
1009
1010
1011
# File 'lib/review/htmlbuilder.rb', line 1005

def inline_tti(str)
  if @book.htmlversion == 5
    %Q(<code class="tt"><i>#{escape(str)}</i></code>)
  else
    %Q(<tt><i>#{escape(str)}</i></tt>)
  end
end

#inline_u(str) ⇒ Object



1270
1271
1272
# File 'lib/review/htmlbuilder.rb', line 1270

def inline_u(str)
  %Q(<u>#{escape(str)}</u>)
end

#inline_uchar(str) ⇒ Object



1287
1288
1289
# File 'lib/review/htmlbuilder.rb', line 1287

def inline_uchar(str)
  %Q(&#x#{str};)
end

#inline_var(str) ⇒ Object



1234
1235
1236
# File 'lib/review/htmlbuilder.rb', line 1234

def inline_var(str)
  inline_asis(str, 'var')
end

#label(id) ⇒ Object



889
890
891
# File 'lib/review/htmlbuilder.rb', line 889

def label(id)
  puts %Q(<a id="#{normalize_id(id)}"></a>)
end

#layoutfileObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/review/htmlbuilder.rb', line 64

def layoutfile
  if @book.config.maker == 'webmaker'
    htmldir = 'web/html'
    localfilename = 'layout-web.html.erb'
  else
    htmldir = 'html'
    localfilename = 'layout.html.erb'
  end
  htmlfilename = if @book.htmlversion == 5
                   File.join(htmldir, 'layout-html5.html.erb')
                 else
                   File.join(htmldir, 'layout-xhtml1.html.erb')
                 end

  layout_file = File.join(@book.basedir, 'layouts', localfilename)
  if !File.exist?(layout_file) && File.exist?(File.join(@book.basedir, 'layouts', 'layout.erb'))
    raise ReVIEW::ConfigError, 'layout.erb is obsoleted. Please use layout.html.erb.'
  end

  if File.exist?(layout_file)
    if ENV['REVIEW_SAFE_MODE'].to_i & 4 > 0
      warn %Q(user's layout is prohibited in safe mode. ignored.), location: location
      layout_file = File.expand_path(htmlfilename, ReVIEW::Template::TEMPLATE_DIR)
    end
  else
    layout_file = File.expand_path(htmlfilename, ReVIEW::Template::TEMPLATE_DIR)
  end
  layout_file
end

#list(lines, id, caption, lang = nil) ⇒ Object



458
459
460
461
462
# File 'lib/review/htmlbuilder.rb', line 458

def list(lines, id, caption, lang = nil)
  puts %Q(<div id="#{normalize_id(id)}" class="caption-code">)
  super
  puts '</div>'
end

#list_body(_id, lines, lang) ⇒ Object



472
473
474
475
476
477
478
479
480
481
# File 'lib/review/htmlbuilder.rb', line 472

def list_body(_id, lines, lang)
  class_names = ['list']
  lexer = lang
  class_names.push("language-#{lexer}") unless lexer.blank?
  class_names.push('highlight') if highlight?
  print %Q(<pre class="#{class_names.join(' ')}">)
  body = lines.inject('') { |i, j| i + detab(j) + "\n" }
  puts highlight(body: body, lexer: lexer, format: 'html')
  puts '</pre>'
end

#list_header(id, caption, _lang) ⇒ Object



464
465
466
467
468
469
470
# File 'lib/review/htmlbuilder.rb', line 464

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

#listnum(lines, id, caption, lang = nil) ⇒ Object



503
504
505
506
507
# File 'lib/review/htmlbuilder.rb', line 503

def listnum(lines, id, caption, lang = nil)
  puts %Q(<div id="#{normalize_id(id)}" class="code">)
  super
  puts '</div>'
end

#listnum_body(lines, lang) ⇒ Object



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/review/htmlbuilder.rb', line 509

def listnum_body(lines, lang)
  body = lines.inject('') { |i, j| i + detab(j) + "\n" }
  lexer = lang
  first_line_number = line_num
  hs = highlight(body: body, lexer: lexer, format: 'html', linenum: true,
                 options: { linenostart: first_line_number })

  if highlight?
    puts hs
  else
    class_names = ['list']
    class_names.push("language-#{lang}") unless lang.blank?
    print %Q(<pre class="#{class_names.join(' ')}">)
    hs.split("\n").each_with_index do |line, i|
      puts detab((i + first_line_number).to_s.rjust(2) + ': ' + line)
    end
    puts '</pre>'
  end
end

#memo(lines, caption = nil) ⇒ Object



296
297
298
# File 'lib/review/htmlbuilder.rb', line 296

def memo(lines, caption = nil)
  captionblock('memo', lines, caption)
end

#nodisp_begin(level, label, caption) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/review/htmlbuilder.rb', line 222

def nodisp_begin(level, label, caption)
  @nonum_counter += 1
  puts '' if level > 1
  return unless caption.present?

  if label
    puts %Q(<a id="#{normalize_id(label)}" /><h#{level} id="#{normalize_id(label)}" hidden="true">#{compile_inline(caption)}</h#{level}>)
  else
    id = normalize_id("#{@chapter.name}_nonum#{@nonum_counter}")
    puts %Q(<a id="#{id}" /><h#{level} id="#{id}" hidden="true">#{compile_inline(caption)}</h#{level}>)
  end
end

#nodisp_end(level) ⇒ Object



235
236
# File 'lib/review/htmlbuilder.rb', line 235

def nodisp_end(level)
end

#nofunc_text(str) ⇒ Object



1316
1317
1318
# File 'lib/review/htmlbuilder.rb', line 1316

def nofunc_text(str)
  escape(str)
end

#noindentObject



909
910
911
# File 'lib/review/htmlbuilder.rb', line 909

def noindent
  @noindent = true
end

#nonum_begin(level, label, caption) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/review/htmlbuilder.rb', line 190

def nonum_begin(level, label, caption)
  @nonum_counter += 1
  puts if level > 1
  return unless caption.present?

  if label
    puts %Q(<h#{level} id="#{normalize_id(label)}">#{compile_inline(caption)}</h#{level}>)
  else
    id = normalize_id("#{@chapter.name}_nonum#{@nonum_counter}")
    puts %Q(<h#{level} id="#{id}">#{compile_inline(caption)}</h#{level}>)
  end
end

#nonum_end(level) ⇒ Object



203
204
# File 'lib/review/htmlbuilder.rb', line 203

def nonum_end(level)
end

#note(lines, caption = nil) ⇒ Object



367
368
369
# File 'lib/review/htmlbuilder.rb', line 367

def note(lines, caption = nil)
  captionblock('note', lines, caption)
end

#notice(lines, caption = nil) ⇒ Object



328
329
330
# File 'lib/review/htmlbuilder.rb', line 328

def notice(lines, caption = nil)
  captionblock('notice', lines, caption)
end

#notoc_begin(level, label, caption) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/review/htmlbuilder.rb', line 206

def notoc_begin(level, label, caption)
  @nonum_counter += 1
  puts if level > 1
  return unless caption.present?

  if label
    puts %Q(<h#{level} id="#{normalize_id(label)}" notoc="true">#{compile_inline(caption)}</h#{level}>)
  else
    id = normalize_id("#{@chapter.name}_nonum#{@nonum_counter}")
    puts %Q(<h#{level} id="#{id}" notoc="true">#{compile_inline(caption)}</h#{level}>)
  end
end

#notoc_end(level) ⇒ Object



219
220
# File 'lib/review/htmlbuilder.rb', line 219

def notoc_end(level)
end

#ol_beginObject



405
406
407
408
409
410
411
412
# File 'lib/review/htmlbuilder.rb', line 405

def ol_begin
  if @ol_num
    puts %Q(<ol start="#{@ol_num}">) # it's OK in HTML5, but not OK in XHTML1.1
    @ol_num = nil
  else
    puts '<ol>'
  end
end

#ol_endObject



418
419
420
# File 'lib/review/htmlbuilder.rb', line 418

def ol_end
  puts '</ol>'
end

#ol_item(lines, _num) ⇒ Object



414
415
416
# File 'lib/review/htmlbuilder.rb', line 414

def ol_item(lines, _num)
  puts "<li>#{join_lines_to_paragraph(lines)}</li>"
end

#olnum(num) ⇒ Object



1340
1341
1342
# File 'lib/review/htmlbuilder.rb', line 1340

def olnum(num)
  @ol_num = num.to_i
end

#open_section(level) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/review/htmlbuilder.rb', line 98

def open_section(level)
  result = []

  while @section_stack.size > 0 && level <= @section_stack[-1]
    result << '</section>'
    @section_stack.pop
  end
  @section_stack.push(level)
  result << %Q(<section class="level#{level}">)

  return result.join("\n")
end

#pagebreakObject



897
898
899
# File 'lib/review/htmlbuilder.rb', line 897

def pagebreak
  puts %Q(<br class="pagebreak" />)
end

#paragraph(lines) ⇒ Object



438
439
440
441
442
443
444
445
# File 'lib/review/htmlbuilder.rb', line 438

def paragraph(lines)
  if @noindent
    puts %Q(<p class="noindent">#{join_lines_to_paragraph(lines)}</p>)
    @noindent = nil
  else
    puts "<p>#{join_lines_to_paragraph(lines)}</p>"
  end
end

#parasepObject



447
448
449
# File 'lib/review/htmlbuilder.rb', line 447

def parasep
  puts '<br />'
end

#planning(lines, caption = nil) ⇒ Object



308
309
310
# File 'lib/review/htmlbuilder.rb', line 308

def planning(lines, caption = nil)
  captionblock('planning', lines, caption)
end

#point(lines, caption = nil) ⇒ Object



336
337
338
# File 'lib/review/htmlbuilder.rb', line 336

def point(lines, caption = nil)
  captionblock('point', lines, caption)
end

#post_paragraphObject



38
39
40
# File 'lib/review/htmlbuilder.rb', line 38

def post_paragraph
  '</p>'
end

#pre_paragraphObject



34
35
36
# File 'lib/review/htmlbuilder.rb', line 34

def pre_paragraph
  '<p>'
end

#quote(lines) ⇒ Object



607
608
609
610
# File 'lib/review/htmlbuilder.rb', line 607

def quote(lines)
  blocked_lines = split_paragraph(lines)
  puts %Q(<blockquote>#{blocked_lines.join("\n")}</blockquote>)
end

#read(lines) ⇒ Object Also known as: lead



451
452
453
454
# File 'lib/review/htmlbuilder.rb', line 451

def read(lines)
  blocked_lines = split_paragraph(lines)
  puts %Q(<div class="lead">\n#{blocked_lines.join("\n")}\n</div>)
end

#ref_begin(level, label, caption) ⇒ Object



267
268
269
270
# File 'lib/review/htmlbuilder.rb', line 267

def ref_begin(level, label, caption)
  print %Q(<div class="reference">)
  headline(level, label, caption)
end

#ref_end(_level) ⇒ Object



272
273
274
# File 'lib/review/htmlbuilder.rb', line 272

def ref_end(_level)
  puts '</div>'
end

#resultObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/review/htmlbuilder.rb', line 115

def result
  check_printendnotes

  # flush all `</section>`
  if use_section?
    print close_sections
  end

  # default XHTML header/footer
  @title = strip_html(compile_inline(@chapter.title))
  @body = solve_nest(@output.string)
  @language = @book.config['language']
  @stylesheets = @book.config['stylesheet']
  @next = @chapter.next_chapter
  @prev = @chapter.prev_chapter
  @next_title = @next ? compile_inline(@next.title) : ''
  @prev_title = @prev ? compile_inline(@prev.title) : ''

  if @book.config.maker == 'webmaker'
    @toc = ReVIEW::WEBTOCPrinter.book_to_string(@book)
  end

  if @book.config['math_format'] == 'mathjax'
    @javascripts.push(%Q(<script>MathJax = { tex: { inlineMath: [['\\\\(', '\\\\)']] }, svg: { fontCache: 'global' } };</script>))
    @javascripts.push(%Q(<script type="text/javascript" id="MathJax-script" async="true" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>))
  end

  ReVIEW::Template.load(layoutfile).result(binding)
end

#result_metric(array) ⇒ Object



691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/review/htmlbuilder.rb', line 691

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

#security(lines, caption = nil) ⇒ Object



320
321
322
# File 'lib/review/htmlbuilder.rb', line 320

def security(lines, caption = nil)
  captionblock('security', lines, caption)
end

#shoot(lines, caption = nil) ⇒ Object



340
341
342
# File 'lib/review/htmlbuilder.rb', line 340

def shoot(lines, caption = nil)
  captionblock('shoot', lines, caption)
end

#solve_nest(s) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/review/htmlbuilder.rb', line 145

def solve_nest(s)
  check_nest
  s.gsub("</dd>\n</dl>\n\x01→dl←\x01", '').
    gsub("\x01→/dl←\x01", "</dd>\n</dl>←END\x01").
    gsub("</li>\n</ul>\n\x01→ul←\x01", '').
    gsub("\x01→/ul←\x01", "</li>\n</ul>←END\x01").
    gsub("</li>\n</ol>\n\x01→ol←\x01", '').
    gsub("\x01→/ol←\x01", "</li>\n</ol>←END\x01").
    gsub("</dl>←END\x01\n<dl>", '').
    gsub("</ul>←END\x01\n<ul>", '').
    gsub("</ol>←END\x01\n<ol>", '').
    gsub("←END\x01", '')
end

#source(lines, caption = nil, lang = nil) ⇒ Object



483
484
485
486
487
# File 'lib/review/htmlbuilder.rb', line 483

def source(lines, caption = nil, lang = nil)
  puts %Q(<div class="source-code">)
  super
  puts '</div>'
end

#source_body(lines, lang) ⇒ Object



495
496
497
498
499
500
501
# File 'lib/review/htmlbuilder.rb', line 495

def source_body(lines, lang)
  print %Q(<pre class="source">)
  body = lines.inject('') { |i, j| i + detab(j) + "\n" }
  lexer = lang
  puts highlight(body: body, lexer: lexer, format: 'html')
  puts '</pre>'
end

#source_header(caption) ⇒ Object



489
490
491
492
493
# File 'lib/review/htmlbuilder.rb', line 489

def source_header(caption)
  if caption.present?
    puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
  end
end

#sup_begin(level, label, caption) ⇒ Object



276
277
278
279
# File 'lib/review/htmlbuilder.rb', line 276

def sup_begin(level, label, caption)
  print %Q(<div class="supplement">)
  headline(level, label, caption)
end

#sup_end(_level) ⇒ Object



281
282
283
# File 'lib/review/htmlbuilder.rb', line 281

def sup_end(_level)
  puts '</div>'
end

#table(lines, id = nil, caption = nil) ⇒ Object



736
737
738
739
740
741
742
743
744
# File 'lib/review/htmlbuilder.rb', line 736

def table(lines, id = nil, caption = nil)
  if id
    puts %Q(<div id="#{normalize_id(id)}" class="table">)
  else
    puts %Q(<div class="table">)
  end
  super
  puts '</div>'
end

#table_begin(_ncols) ⇒ Object



756
757
758
# File 'lib/review/htmlbuilder.rb', line 756

def table_begin(_ncols)
  puts '<table>'
end

#table_endObject



772
773
774
# File 'lib/review/htmlbuilder.rb', line 772

def table_end
  puts '</table>'
end

#table_header(id, caption) ⇒ Object



746
747
748
749
750
751
752
753
754
# File 'lib/review/htmlbuilder.rb', line 746

def table_header(id, caption)
  if id.nil?
    puts %Q(<p class="caption">#{compile_inline(caption)}</p>)
  elsif get_chap
    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>)
  else
    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>)
  end
end

#talk(lines) ⇒ Object



620
621
622
623
624
625
# File 'lib/review/htmlbuilder.rb', line 620

def talk(lines)
  puts %Q(<div class="talk">)
  blocked_lines = split_paragraph(lines)
  puts blocked_lines.join("\n")
  puts '</div>'
end

#td(str) ⇒ Object



768
769
770
# File 'lib/review/htmlbuilder.rb', line 768

def td(str)
  "<td>#{str}</td>"
end

#texequation(lines, id = nil, caption = '') ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
# File 'lib/review/htmlbuilder.rb', line 627

def texequation(lines, id = nil, caption = '')
  if id
    puts %Q(<div id="#{normalize_id(id)}" class="caption-equation">)
    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

#texequation_body(lines) ⇒ Object



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/review/htmlbuilder.rb', line 649

def texequation_body(lines)
  puts %Q(<div class="equation">)
  if @book.config['math_format'] == 'mathml'
    begin
      require 'math_ml'
      require 'math_ml/symbol/character_reference'
    rescue LoadError
      app_error 'not found math_ml'
    end
    p = MathML::LaTeX::Parser.new(symbol: MathML::Symbol::CharacterReference)
    print p.parse(lines.join("\n") + "\n", true)
  elsif @book.config['math_format'] == 'mathjax'
    puts "$$#{lines.join("\n").gsub('<', '\lt{}').gsub('>', '\gt{}').gsub('&', '&amp;')}$$"
  elsif @book.config['math_format'] == 'imgmath'
    fontsize = @book.config['imgmath_options']['fontsize'].to_f
    lineheight = @book.config['imgmath_options']['lineheight'].to_f
    math_str = "\\begin{equation*}\n\\fontsize{#{fontsize}}{#{lineheight}}\\selectfont\n#{lines.join("\n")}\n\\end{equation*}\n"
    key = Digest::SHA256.hexdigest(math_str)
    if @book.config.check_version('2', exception: false)
      img_path = @img_math.make_math_image(math_str, key)
      puts %Q(<img src="#{img_path}" />)
    else
      img_path = @img_math.defer_math_image(math_str, key)
      puts %Q(<img src="#{img_path}" class="math_gen_#{key}" alt="#{escape(lines.join(' '))}" />)
    end
  else
    print '<pre>'
    puts escape(lines.join("\n"))
    puts '</pre>'
  end
  puts '</div>'
end

#texequation_header(id, caption) ⇒ Object



641
642
643
644
645
646
647
# File 'lib/review/htmlbuilder.rb', line 641

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

#text(str) ⇒ Object



1072
1073
1074
# File 'lib/review/htmlbuilder.rb', line 1072

def text(str)
  str
end

#th(str) ⇒ Object



764
765
766
# File 'lib/review/htmlbuilder.rb', line 764

def th(str)
  "<th>#{str}</th>"
end

#tip(lines, caption = nil) ⇒ Object



300
301
302
# File 'lib/review/htmlbuilder.rb', line 300

def tip(lines, caption = nil)
  captionblock('tip', lines, caption)
end

#tr(rows) ⇒ Object



760
761
762
# File 'lib/review/htmlbuilder.rb', line 760

def tr(rows)
  puts "<tr>#{rows.join}</tr>"
end

#ul_beginObject



389
390
391
# File 'lib/review/htmlbuilder.rb', line 389

def ul_begin
  puts '<ul>'
end

#ul_endObject



401
402
403
# File 'lib/review/htmlbuilder.rb', line 401

def ul_end
  puts '</ul>'
end

#ul_item_begin(lines) ⇒ Object



393
394
395
# File 'lib/review/htmlbuilder.rb', line 393

def ul_item_begin(lines)
  print "<li>#{join_lines_to_paragraph(lines)}"
end

#ul_item_endObject



397
398
399
# File 'lib/review/htmlbuilder.rb', line 397

def ul_item_end
  puts '</li>'
end

#use_section?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/review/htmlbuilder.rb', line 94

def use_section?
  @use_section
end

#warning(lines, caption = nil) ⇒ Object



332
333
334
# File 'lib/review/htmlbuilder.rb', line 332

def warning(lines, caption = nil)
  captionblock('warning', lines, caption)
end

#xcolumn_begin(level, label, caption) ⇒ Object



258
259
260
261
# File 'lib/review/htmlbuilder.rb', line 258

def xcolumn_begin(level, label, caption)
  puts %Q(<div class="xcolumn">)
  headline(level, label, caption)
end

#xcolumn_end(_level) ⇒ Object



263
264
265
# File 'lib/review/htmlbuilder.rb', line 263

def xcolumn_end(_level)
  puts '</div>'
end

#xmlns_ops_prefixObject



159
160
161
162
163
164
165
# File 'lib/review/htmlbuilder.rb', line 159

def xmlns_ops_prefix
  if @book.config['epubversion'].to_i == 3
    'epub'
  else
    'ops'
  end
end