# Copyright (c) 2008-2022 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(
) return result.join("\n") end def close_sections "
\n" * @section_stack.size end def result check_printendnotes # flush all `` 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()) @javascripts.push(%Q()) end ReVIEW::Template.load(layoutfile).result(binding) end def solve_nest(s) check_nest s.gsub("\n\n\x01→dl←\x01", ''). gsub("\x01→/dl←\x01", "\n←END\x01"). gsub("\n\n\x01→ul←\x01", ''). gsub("\x01→/ul←\x01", "\n←END\x01"). gsub("\n\n\x01→ol←\x01", ''). gsub("\x01→/ol←\x01", "\n←END\x01"). gsub("←END\x01\n
", ''). gsub("←END\x01\n