# encoding: utf-8 # # Copyright (c) 2002-2007 Minero Aoki # 2008-2014 Minero Aoki, Kenshi Muto, Masayoshi Takahashi, # KADO Masanori # # 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/htmllayout' require 'review/textutils' module ReVIEW class HTMLBuilder < Builder include TextUtils include HTMLUtils [:ref].each {|e| Compiler.definline(e) } Compiler.defblock(:memo, 0..1) Compiler.defblock(:tip, 0..1) Compiler.defblock(:info, 0..1) Compiler.defblock(:planning, 0..1) Compiler.defblock(:best, 0..1) Compiler.defblock(:important, 0..1) Compiler.defblock(:security, 0..1) Compiler.defblock(:caution, 0..1) Compiler.defblock(:notice, 0..1) Compiler.defblock(:point, 0..1) Compiler.defblock(:shoot, 0..1) def extname ".#{@book.config["htmlext"]}" end def builder_init(no_error = false) @no_error = no_error @noindent = nil @ol_num = nil end private :builder_init def builder_init_file @warns = [] @errors = [] @chapter.book.image_types = %w( .png .jpg .jpeg .gif .svg ) @column = 0 @sec_counter = SecCounter.new(5, @chapter) end private :builder_init_file def result layout_file = File.join(@book.basedir, "layouts", "layout.html.erb") unless File.exist?(layout_file) # backward compatibility layout_file = File.join(@book.basedir, "layouts", "layout.erb") end if File.exist?(layout_file) if ENV["REVIEW_SAFE_MODE"].to_i & 4 > 0 warn "user's layout is prohibited in safe mode. ignored." else title = strip_html(@chapter.title) toc = "" toc_level = 0 @chapter.headline_index.items.each do |i| caption = "
#{caption}
\n] end if @book.config["deprecated-blocklines"].nil? buf << lines.join("") else error "deprecated-blocklines is obsoleted." end buf << "#{caption}
\n] unless caption.nil? buf << %Q[] lines.each {|line| buf << detab(line) << "\n" } buf << "\n" buf << "
#{lines.join}
\n" else @noindent = nil %Q[#{lines.join}
\n] end end def parasep "#{I18n.t("list")}#{I18n.t("format_number_header_without_chapter", [@chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}
\n] else %Q[#{I18n.t("list")}#{I18n.t("format_number_header", [get_chap, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{caption}
\n] end end def list_body(id, lines, lang) id ||= '' buf = %Q[] body = lines.inject(''){|i, j| i + detab(j) + "\n"} lexer = lang || File.extname(id).gsub(/\./, '') buf << highlight(:body => body, :lexer => lexer, :format => 'html') buf << "\n" buf end def source(lines, caption = nil, lang = nil) buf = %Q[
#{caption}
\n] end end def source_body(id, lines, lang) id ||= '' buf = %Q[] body = lines.inject(''){|i, j| i + detab(j) + "\n"} lexer = lang || File.extname(id).gsub(/\./, '') buf << highlight(:body => body, :lexer => lexer, :format => 'html') buf << "\n" buf end def listnum(lines, id, caption, lang = nil) buf = %Q[
' lines.each_with_index do |line, i| buf << detab((i+1).to_s.rjust(2) + ": " + line) << "\n" end buf << '' << "\n" end buf end def emlist(lines, caption = nil, lang = nil) buf = %Q[
#{caption}
\n) end buf << %Q[] body = lines.inject(''){|i, j| i + detab(j) + "\n"} lexer = lang buf << highlight(:body => body, :lexer => lexer, :format => 'html') buf << "\n" buf << "
#{caption}
\n) end if highlight? body = lines.inject(''){|i, j| i + detab(j) + "\n"} lexer = lang buf << highlight(:body => body, :lexer => lexer, :format => 'html', :options => {:linenos => 'inline', :nowrap => false}) else buf << '' lines.each_with_index do |line, i| buf << detab((i+1).to_s.rjust(2) + ": " + line) << "\n" end buf << '' << "\n" end buf << '
#{caption}
\n) end buf << %Q[] body = lines.inject(''){|i, j| i + detab(j) + "\n"} lexer = 'shell-session' buf << highlight(:body => body, :lexer => lexer, :format => 'html') buf << "\n" buf << "
\n" end private :quotedlist def quote(lines) if @book.config["deprecated-blocklines"].nil? "\n] lines.each do |line| buf << detab(line) << "\n" end buf << "
#{lines.join("")}\n" else error "deprecated-blocklines is obsoleted." end end def doorquote(lines, ref) buf = "" if @book.config["deprecated-blocklines"].nil? buf << %Q[
\n] buf << "#{lines.join("")}\n" buf << %Q[\n] else error "deprecated-blocklines is obsoleted." end buf end def talk(lines) buf = "" buf << %Q[#{ref}より
\n] buf << %Q[
' buf << lines.join("\n") << "\n" buf << "\n" end buf << "
\n] lines.each do |line| buf << detab(line) << "\n" end buf << %Q[\n] buf << image_header(id, caption) buf << %Q[
\n] if get_chap.nil? buf << %Q[#{I18n.t("image")}#{I18n.t("format_number_header_without_chapter", [@chapter.image(id).number])}#{I18n.t("caption_prefix")}#{caption}\n] else buf << %Q[#{I18n.t("image")}#{I18n.t("format_number_header", [get_chap, @chapter.image(id).number])}#{I18n.t("caption_prefix")}#{caption}\n] end buf << %Q[
\n] buf 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) if id buf = %Q[#{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{caption}
\n] else %Q[#{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{caption}
\n] end end def table_begin(ncols) "#{str} | " end def td(str) "#{str} | " end def table_end "
---|
[*#{@chapter.footnote(id).number}] #{str}
[*#{@chapter.footnote(id).number}] #{str}
missing image: #{id}\n] end unless caption.empty? buf << %Q[
\n] buf << %Q[#{I18n.t("numberless_image")}#{I18n.t("caption_prefix")}#{caption}\n] buf << %Q[
\n] end buf << %Q[#{(str)}
)
else
%Q(#{(str)})
end
end
def inline_ttb(str)
if @book.htmlversion == 5
%Q(#{(str)}
)
else
%Q(#{(str)})
end
end
def inline_dtp(str)
""
end
def inline_code(str)
if @book.htmlversion == 5
%Q(#{(str)}
)
else
%Q(#{(str)})
end
end
def inline_idx(str)
%Q(#{(str)})
end
def inline_hidx(str)
%Q()
end
def inline_br(str)
%Q(#{(str)}
)
else
%Q(#{(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(#{(str)})
end
def inline_recipe(str)
%Q(「#{(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(#{(str)}) else %Q() end end def inline_raw(str) super(str) end def nofunc_text(str) escape_html(str) end def compile_href(url, label) %Q(#{label.nil? ? (url) : (label)}) end def flushright(lines) result = "" if @book.config["deprecated-blocklines"].nil? result << lines.join("").gsub("
", "
") else error "deprecated-blocklines is obsoleted." end result end def centering(lines) lines.join("").gsub("
", "
") end def image_ext "png" end def olnum(num) @ol_num = num.to_i end end end # module ReVIEW