# encoding: utf-8 # # Copyright (c) 2002-2007 Minero Aoki # 2008-2014 Minero Aoki, Kenshi Muto # # 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/textutils' require 'nkf' module ReVIEW class IDGXMLBuilder < Builder include TextUtils include HTMLUtils [:ttbold, :hint, :maru, :keytop, :labelref, :ref, :pageref, :balloon].each {|e| Compiler.definline(e) } Compiler.defsingle(:dtp, 1) Compiler.defcodeblock(:insn, 1) 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) Compiler.defblock(:reference, 0) Compiler.defblock(:term, 0) Compiler.defblock(:link, 0..1) Compiler.defblock(:practice, 0) Compiler.defblock(:expert, 0) Compiler.defblock(:rawblock, 0) def extname '.xml' end def builder_init(no_error = false) @no_error = no_error end private :builder_init def builder_init_file @warns = [] @errors = [] @section = 0 @subsection = 0 @subsubsection = 0 @subsubsubsection = 0 @sec_counter = SecCounter.new(5, @chapter) @column = 0 @noindent = nil @rootelement = "doc" @secttags = nil @tsize = nil @texblockequation = 0 @texinlineequation = 0 @output << %Q(\n) @output << %Q(<#{@rootelement} xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">) if @book.config["nolf"].present? @lf = "" else @lf = "\n" end @secttags = true unless @book.config["structuredxml"].nil? end private :builder_init_file def result s = "" unless @secttags.nil? s += "" if @subsubsubsection > 0 s += "" if @subsubsection > 0 s += "" if @subsection > 0 s += "" if @section > 0 s += "" if @chapter.number > 0 end messages() + @output.string + s + "#{@rootelement}>\n" end def warn(msg) if @no_error @warns.push [@location.filename, @location.lineno, msg] buf << "----WARNING: #{escape_html(msg)}----" << @lf else $stderr.puts "#{@location}: warning: #{msg}" end end def error(msg) if @no_error @errors.push [@location.filename, @location.lineno, msg] buf << "----ERROR: #{escape_html(msg)}----" << @lf else $stderr.puts "#{@location}: error: #{msg}" end end def messages error_messages() + warning_messages() end def error_messages return '' if @errors.empty? "
#{lines.join('').sub(/\A\t+/, "")}
) << @lf else buf << "#{lines.join}
" << @lf end else buf << %Q(#{lines.join}
) << @lf @noindent = nil end buf end def read(lines) if @book.config["deprecated-blocklines"].nil? %Q[/,"").sub(/<\/p>$/,"")}.join() %Q[
#{str}
] + @lf end end alias_method :lead, :read def column_label(id) num = @chapter.column(id).number "column-#{num}" end private :column_label def inline_column(id) if @book.config["chapterlink"] %Q(#{escape_html(@chapter.column(id).caption)}) else escape_html(@chapter.column(id).caption) end rescue KeyError error "unknown column: #{id}" nofunc_text("[UnknownColumn:#{id}]") end def inline_list(id) chapter, id = extract_chapter_id(id) if get_chap(chapter).nil? "#{I18n.t("list")}#{I18n.t("format_number_without_chapter", [chapter.list(id).number])}" else "#{I18n.t("list")}#{I18n.t("format_number", [get_chap(chapter), chapter.list(id).number])}" end end def list_header(id, caption, lang) buf = "" buf << %Q[) buf << codelines_body(lines) buf << "
) no = 1 lines.each_with_index do |line, i| unless @book.config["listinfo"].nil? buf << "" << @lf buf end def cmd(lines, caption = nil) quotedlist lines, 'cmd', caption end def quotedlist(lines, css_class, caption) buf = "" buf << %Q[" end buf << detab("" + (i + 1).to_s.rjust(2) + ": " + line) buf << "\n" buf << " " unless @book.config["listinfo"].nil? no += 1 end buf << "
] no = 1 lines.each do |line| unless @book.config["listinfo"].nil? buf << "" end buf << detab(line) buf << "\n" buf << " " unless @book.config["listinfo"].nil? no += 1 end buf << '
#{lines.join("")}" + @lf else str = lines.map{|l| l.sub(/^
/,"").sub(/<\/p>$/,"")}.join("\n")
"#{str}
" + @lf
end
end
def inline_table(id)
chapter, id = extract_chapter_id(id)
if get_chap(chapter).nil?
"#{I18n.t("table")}#{I18n.t("format_number_without_chapter", [chapter.table(id).number])}"
else
"#{I18n.t("table")}#{I18n.t("format_number", [get_chap(chapter), chapter.table(id).number])}"
end
end
def inline_img(id)
chapter, id = extract_chapter_id(id)
if get_chap(chapter).nil?
"#{I18n.t("image")}#{I18n.t("format_number_without_chapter", [chapter.image(id).number])}"
else
"#{I18n.t("image")}#{I18n.t("format_number", [get_chap(chapter), chapter.image(id).number])}"
end
end
def inline_imgref(id)
chapter, id = extract_chapter_id(id)
if chapter.image(id).caption.blank?
inline_img(id)
else
if get_chap(chapter).nil?
"#{I18n.t("image")}#{I18n.t("format_number_without_chapter", [chapter.image(id).number])}#{I18n.t('image_quote', chapter.image(id).caption)}"
else
"#{I18n.t("image")}#{I18n.t("format_number", [get_chap(chapter), chapter.image(id).number])}#{I18n.t('image_quote', chapter.image(id).caption)}"
end
end
end
def handle_metric(str)
k, v = str.split('=', 2)
return %Q|#{k}=\"#{v.sub(/\A["']/, '').sub(/["']\Z/, '')}\"|
end
def result_metric(array)
" #{array.join(' ')}"
end
def image_image(id, caption, metric=nil)
buf = ""
metrics = parse_metric("idgxml", metric)
buf << "" << @lf
buf << %Q[
" << @lf
buf << %Q[
] lines.each do |line| buf << detab(line) buf << "\n" end buf << %Q[] image_header id, caption buf << "" << @lf warn "no such image: #{id}" buf end def image_header(id, caption) if get_chap.nil? %Q[
' << @lf buf << "#{lines.join("\n")}" << @lf buf << '' << @lf buf << '
#{cell.sub("DUMMYCELLSPLITTER", "")} | ] i += 1 end end end end buf << trputs(tablewidth, rows, cellwidth, sepidx) buf << "
#{str}
", "
") + @lf else str = lines.map{|l| l.sub(/^
/,"").sub(/<\/p>$/,"")}.join("\n") "
#{str}
" + @lf end end def centering(lines) lines.join("").gsub("", "
") + @lf end def captionblock(type, lines, caption, specialstyle = nil) buf = "" buf << "<#{type}>" style = specialstyle.nil? ? "#{type}-title" : specialstyle buf << "
/,"").sub(/<\/p>$/,"")}.join("\n")
buf << "#{str}#{type}>" << @lf
end
buf
end
def note(lines, caption = nil)
captionblock("note", lines, caption)
end
def memo(lines, caption = nil)
captionblock("memo", lines, caption)
end
def tip(lines, caption = nil)
captionblock("tip", lines, caption)
end
def info(lines, caption = nil)
captionblock("info", lines, caption)
end
def planning(lines, caption = nil)
captionblock("planning", lines, caption)
end
def best(lines, caption = nil)
captionblock("best", lines, caption)
end
def important(lines, caption = nil)
captionblock("important", lines, caption)
end
def security(lines, caption = nil)
captionblock("security", lines, caption)
end
def caution(lines, caption = nil)
captionblock("caution", lines, caption)
end
def term(lines)
captionblock("term", lines, nil)
end
def link(lines, caption = nil)
captionblock("link", lines, caption)
end
def notice(lines, caption = nil)
if caption.nil?
captionblock("notice", lines, nil)
else
captionblock("notice-t", lines, caption, "notice-title")
end
end
def point(lines, caption = nil)
if caption.nil?
captionblock("point", lines, nil)
else
captionblock("point-t", lines, caption, "point-title")
end
end
def shoot(lines, caption = nil)
if caption.nil?
captionblock("shoot", lines, nil)
else
captionblock("shoot-t", lines, caption, "shoot-title")
end
end
def reference(lines)
captionblock("reference", lines, nil)
end
def practice(lines)
captionblock("practice", lines, nil)
end
def expert(lines)
captionblock("expert", lines, nil)
end
def syntaxblock(type, lines, caption)
buf = ""
if caption.nil?
buf << %Q[<#{type}>] << @lf
else
titleopentag = %Q[caption aid:pstyle="#{type}-title"]
titleclosetag = "caption"
if type == "insn"
titleopentag = %Q[floattitle type="insn"]
titleclosetag = "floattitle"
end
buf << %Q[<#{type}><#{titleopentag}>#{caption}#{titleclosetag}>] << @lf
end
no = 1
lines.each do |line|
line.chomp!
unless @book.config["listinfo"].nil?
buf << %Q[" << @lf
begin
buf << %Q[
] << @lf buf << codelines_body(lines) buf << %Q[