# Copyright (c) 2008-2023 Minero Aoki, Kenshi Muto, Masayoshi Takahashi, # KADO Masanori # 2002-2007 Minero Aoki # # 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. # require 'review/builder' require 'review/htmlutils' require 'review/template' require 'review/textutils' require 'review/webtocprinter' require 'tmpdir' require 'open3' module ReVIEW class HTMLBuilder < Builder include TextUtils include HTMLUtils [:ref].each do |e| Compiler.definline(e) end Compiler.defblock(:planning, 0..1) Compiler.defblock(:best, 0..1) Compiler.defblock(:security, 0..1) Compiler.defblock(:point, 0..1) Compiler.defblock(:shoot, 0..1) def pre_paragraph '
' end def post_paragraph '
' end def extname ".#{@book.config['htmlext']}" end def builder_init_file super @noindent = nil @ol_num = nil @chapter.book.image_types = %w[.png .jpg .jpeg .gif .svg] @column = 0 @nonum_counter = 0 @first_line_num = nil @body_ext = nil @toc = nil @javascripts = [] @section_stack = [] maker = @book.config.maker || 'epubmaker' # for review-compile @use_section = @book.config[maker] && @book.config[maker]['use_section'] end private :builder_init_file 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 def use_section? @use_section end def open_section(level) result = [] while @section_stack.size > 0 && level <= @section_stack[-1] result << '' @section_stack.pop end @section_stack.push(level) result << %Q(#{compile_inline(caption)}
) end blocked_lines = split_paragraph(lines) puts blocked_lines.join("\n") puts '#{compile_inline(caption)}
) end puts %Q() lines.each do |line| puts detab(line) end puts '' if !caption_top?('list') && caption.present? puts captionstr end puts '
\#{compile_inline(caption)}
) end end def #{name}_end puts '#{join_lines_to_paragraph(lines)}
) @noindent = nil else puts "#{join_lines_to_paragraph(lines)}
" end end def parasep puts '#{I18n.t('list')}#{I18n.t('format_number_header', [get_chap, @chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
) else puts %Q(#{I18n.t('list')}#{I18n.t('format_number_header_without_chapter', [@chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
) end end 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() body = lines.inject('') { |i, j| i + detab(j) + "\n" } puts highlight(body: body, lexer: lexer, format: 'html') puts '' end def source(lines, caption = nil, lang = nil) puts %Q(
#{compile_inline(caption)}
) end end def source_body(lines, lang) print %Q() body = lines.inject('') { |i, j| i + detab(j) + "\n" } lexer = lang puts highlight(body: body, lexer: lexer, format: 'html') puts '' end def listnum(lines, id, caption, lang = nil) puts %Q(
) hs.split("\n").each_with_index do |line, i| puts detab((i + first_line_number).to_s.rjust(2) + ': ' + line) end puts '' end end def emlist(lines, caption = nil, lang = nil) puts %Q(
#{compile_inline(caption)}
) end class_names = ['emlist'] class_names.push("language-#{lang}") unless lang.blank? class_names.push('highlight') if highlight? print %Q() body = lines.inject('') { |i, j| i + detab(j) + "\n" } lexer = lang puts highlight(body: body, lexer: lexer, format: 'html') puts '' if !caption_top?('list') && caption.present? puts %Q(
#{compile_inline(caption)}
) end puts '#{compile_inline(caption)}
) 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() hs.split("\n").each_with_index do |line, i| puts detab((i + first_line_number).to_s.rjust(2) + ': ' + line) end puts '' end if !caption_top?('list') && caption.present? puts %Q(
#{compile_inline(caption)}
) end puts '#{compile_inline(caption)}
) end print %Q() body = lines.inject('') { |i, j| i + detab(j) + "\n" } lexer = 'shell-session' puts highlight(body: body, lexer: lexer, format: 'html') puts '' if !caption_top?('list') && caption.present? puts %Q(
#{compile_inline(caption)}
) end puts '' end private :quotedlist def quote(lines) blocked_lines = split_paragraph(lines) puts %Q() lines.each do |line| puts detab(line) end puts '
#{blocked_lines.join("\n")}) end def doorquote(lines, ref) blocked_lines = split_paragraph(lines) puts %Q(
) puts blocked_lines.join("\n") puts %Q(' end def talk(lines) puts %Q(#{ref}より
) puts '
#{I18n.t('equation')}#{I18n.t('format_number_header', [get_chap, @chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
) else puts %Q(#{I18n.t('equation')}#{I18n.t('format_number_header_without_chapter', [@chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
) end end def texequation_body(lines) puts %Q(' puts escape(lines.join("\n")) puts '' end puts '
) lines.each do |line| puts detab(line) end puts '' image_header(id, caption) unless caption_top?('image') puts '
) 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) if id 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 '#{str} | " end def td(str) "#{str} | " end def table_end puts '
---|
#{back}#{I18n.t('html_footnote_textmark', @chapter.footnote(id).number)}#{compile_inline(str)}
[*#{@chapter.footnote(id).number}] #{compile_inline(str)}
#{back}#{I18n.t('html_endnote_textmark', @chapter.endnote(id).number)}#{compile_inline(@chapter.endnote(id).content)}
#{I18n.t('numberless_image')}#{I18n.t('caption_prefix')}#{compile_inline(caption)}
EOS end puts %Q() lines.each do |line| puts detab(line) end puts '' end end if !caption_top?('image') && caption.present? puts caption_str end puts '
#{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)
'#{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(missing image: #{id}) end end def inline_uchar(str) %Q(#{str};) end def inline_comment(str) if @book.config['draft'] %Q(#{escape(str)}) 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) # rubocop:disable Lint/UselessMethodDefinition 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 end end # module ReVIEW