lib/review/latexbuilder.rb in review-1.0.0 vs lib/review/latexbuilder.rb in review-1.1.0

- old
+ new

@@ -36,10 +36,11 @@ #@index = indexes[:latex_index] @blank_needed = false @latex_tsize = nil @tsize = nil @table_caption = nil + @ol_num = nil end private :builder_init_file def blank @blank_needed = true @@ -66,23 +67,30 @@ HEADLINE = { 1 => 'chapter', 2 => 'section', 3 => 'subsection', - 4 => 'subsubsection' + 4 => 'subsubsection', + 5 => 'paragraph', + 6 => 'subparagraph' } def headline(level, label, caption) prefix = "" if level > ReVIEW.book.param["secnolevel"] || (@chapter.number.to_s.empty? && level > 1) prefix = "*" end blank unless @output.pos == 0 puts macro(HEADLINE[level]+prefix, compile_inline(caption)) + if prefix == "*" && level <= ReVIEW.book.param["toclevel"].to_i + puts "\\addcontentsline{toc}{#{HEADLINE[level]}}{#{compile_inline(caption)}}" + end if level == 1 puts macro('label', chapter_label) end + rescue + error "unknown level: #{level}" end def nonum_begin(level, label, caption) blank unless @output.pos == 0 puts macro(HEADLINE[level]+"*", compile_inline(caption)) @@ -91,28 +99,16 @@ def nonum_end(level) end def column_begin(level, label, caption) blank - ## puts '\vspace{2zw}' -## puts '\begin{center}' -## puts '\begin{minipage}{1.0\linewidth}' -## puts '\begin{framed}' -## puts '\setlength{\FrameSep}{1zw}' - -## nonum_begin(3, label, caption) # FIXME - puts "\\begin{reviewcolumn}\n" puts macro('reviewcolumnhead', nil, compile_inline(caption)) end def column_end(level) -## puts '\end{framed}' -## puts '\end{minipage}' -## puts '\end{center}' -## ## puts '\vspace{2zw}' puts "\\end{reviewcolumn}\n" blank end def captionblock(type, lines, caption) @@ -131,31 +127,52 @@ end puts "\\end{reviewminicolumn}\n" end + def box(lines, caption = nil) + blank + if caption + puts macro('reviewboxcaption', "#{compile_inline(caption)}") + end + puts '\begin{reviewbox}' + lines.each do |line| + puts detab(line) + end + puts '\end{reviewbox}' + blank + end + def ul_begin blank puts '\begin{itemize}' end def ul_item(lines) - puts '\item ' + lines.join + str = lines.join + str.sub!(/\A(\[)/){'\lbrack{}'} + puts '\item ' + str end def ul_end puts '\end{itemize}' blank end def ol_begin blank puts '\begin{enumerate}' + if @ol_num + puts "\\setcounter{enumi}{#{@ol_num - 1}}" + @ol_num = nil + end end def ol_item(lines, num) - puts '\item ' + lines.join + str = lines.join + str.sub!(/\A(\[)/){'\lbrack{}'} + puts '\item ' + str end def ol_end puts '\end{enumerate}' blank @@ -165,10 +182,12 @@ blank puts '\begin{description}' end def dt(str) + str.sub!(/\[/){'\lbrack{}'} + str.sub!(/\]/){'\rbrack{}'} puts '\item[' + str + '] \mbox{} \\\\' end def dd(lines) puts lines.join @@ -201,72 +220,62 @@ blank if caption puts macro('reviewemlistcaption', "#{compile_inline(caption)}") end puts '\begin{reviewemlist}' - puts '\begin{alltt}' lines.each do |line| - puts line + puts detab(line) end - puts '\end{alltt}' puts '\end{reviewemlist}' blank end def emlistnum(lines, caption = nil) blank if caption puts macro('reviewemlistcaption', "#{compile_inline(caption)}") end puts '\begin{reviewemlist}' - puts '\begin{alltt}' lines.each_with_index do |line, i| puts detab((i+1).to_s.rjust(2) + ": " + line) end - puts '\end{alltt}' puts '\end{reviewemlist}' blank end def listnum_body(lines) puts '\begin{reviewlist}' - puts '\begin{alltt}' lines.each_with_index do |line, i| puts detab((i+1).to_s.rjust(2) + ": " + line) end - puts '\end{alltt}' puts '\end{reviewlist}' blank - end + end def cmd(lines, caption = nil) blank if caption puts macro('reviewcmdcaption', "#{compile_inline(caption)}") end puts '\begin{reviewcmd}' - puts '\begin{alltt}' lines.each do |line| - puts line + puts detab(line) end - puts '\end{alltt}' puts '\end{reviewcmd}' blank end def list_header(id, caption) - puts macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])} #{compile_inline(caption)}") + puts macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}") end - def list_body(lines) + def list_body(id, lines) puts '\begin{reviewlist}' - puts '\begin{alltt}' lines.each do |line| - puts line + puts detab(line) end - puts '\end{alltt}' puts '\end{reviewlist}' puts "" end def source(lines, caption) @@ -280,15 +289,13 @@ def source_header(caption) puts macro('reviewlistcaption', compile_inline(caption)) end def source_body(lines) - puts '\begin{alltt}' lines.each do |line| - puts line + puts detab(line) end - puts '\end{alltt}' end def image_header(id, caption) end @@ -299,31 +306,29 @@ def image_image(id, caption, metric) metrics = parse_metric("latex", metric) # image is always bound here puts '\begin{reviewimage}' - if !metrics.empty? + if metrics.present? puts "\\includegraphics[#{metrics}]{#{@chapter.image(id).path}}" else puts "\\includegraphics[width=\\maxwidth]{#{@chapter.image(id).path}}" end - puts macro('label', image_label(id)) - if !caption.empty? + if caption.present? puts macro('caption', compile_inline(caption)) end + puts macro('label', image_label(id)) puts '\end{reviewimage}' end def image_dummy(id, caption, lines) puts '\begin{reviewdummyimage}' - puts '\begin{alltt}' path = @chapter.image(id).path puts "--[[path = #{path} (#{existence(id)})]]--" lines.each do |line| puts detab(line.rstrip) end - puts '\end{alltt}' puts macro('label', image_label(id)) puts compile_inline(caption) puts '\end{reviewdummyimage}' end @@ -340,33 +345,81 @@ def chapter_label() "chap:#{@chapter.id}" end private :chapter_label + def table_label(id) + "table:#{@chapter.id}:#{id}" + end + private :table_label + + def bib_label(id) + "bib:#{id}" + end + private :bib_label + def indepimage(id, caption=nil, metric=nil) metrics = parse_metric("latex", metric) puts '\begin{reviewimage}' - if !metrics.empty? + if metrics.present? puts "\\includegraphics[#{metrics}]{#{@chapter.image(id).path}}" else puts "\\includegraphics[width=\\maxwidth]{#{@chapter.image(id).path}}" end - if !caption.nil? && !caption.empty? - puts macro('caption', compile_inline(caption)) + if caption.present? + puts macro('reviewindepimagecaption', + %Q[#{I18n.t("numberless_image")}#{I18n.t("caption_prefix")}#{compile_inline(caption)}]) end puts '\end{reviewimage}' end alias :numberlessimage indepimage + 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) + + begin + table_header id, caption unless caption.nil? + rescue KeyError => err + error "no such table: #{id}" + end + return if rows.empty? + table_begin rows.first.size + if sepidx + sepidx.times do + tr rows.shift.map {|s| th(s) } + end + rows.each do |cols| + tr cols.map {|s| td(s) } + end + else + rows.each do |cols| + h, *cs = *cols + tr [th(h)] + cs.map {|s| td(s) } + end + end + table_end + end + def table_header(id, caption) - if caption && !caption.empty? + if caption.present? @table_caption = true puts '\begin{table}[h]' -## puts macro('reviewtablecaption', "表#{@chapter.number}.#{@chapter.table(id).number} #{compile_inline(caption)}") puts macro('reviewtablecaption', compile_inline(caption)) end + puts macro('label', table_label(id)) end def table_begin(ncols) if @latex_tsize puts macro('begin', 'reviewtable', @latex_tsize) @@ -386,13 +439,13 @@ end def th(s) ## use shortstack for @<br> if /\\\\/i =~ s - macro('textgt', macro('shortstack[l]', s)) + macro('reviewth', macro('shortstack[l]', s)) else - macro('textgt', s) + macro('reviewth', s) end end def td(s) ## use shortstack for @<br> @@ -423,10 +476,12 @@ def center(lines) latex_block 'center', lines end + alias centering center + def flushright(lines) latex_block 'flushright', lines end def texequation(lines) @@ -460,16 +515,23 @@ lines.each do |line| puts line end end - def comment(str) + def comment(lines, comment = nil) + lines ||= [] + lines.unshift comment unless comment.blank? if ReVIEW.book.param["draft"] + str = lines.join("") puts macro('pdfcomment', escape(str)) end end + def hr + puts '\hrule' + end + def label(id) puts macro('label', id) end def pagebreak @@ -524,16 +586,16 @@ macro('reviewlistref', "#{chapter.number}.#{chapter.list(id).number}") end def inline_table(id) chapter, id = extract_chapter_id(id) - macro('reviewtableref', "#{chapter.number}.#{chapter.table(id).number}") + macro('reviewtableref', "#{chapter.number}.#{chapter.table(id).number}", table_label(id)) end def inline_img(id) chapter, id = extract_chapter_id(id) - macro('reviewimageref', "#{chapter.number}.#{chapter.image(id).number}") + macro('reviewimageref', "#{chapter.number}.#{chapter.image(id).number}", image_label(id)) end def footnote(id, content) if ReVIEW.book.param["footnotetext"] puts macro("footnotetext[#{@chapter.footnote(id).number}]", @@ -608,28 +670,31 @@ def nofunc_text(str) escape(str) end def inline_tt(str) - str_escaped = escape(str).gsub(/\-\-/, "{-}{-}") - macro('texttt', str_escaped) + macro('texttt', escape(str)) end + def inline_del(str) + macro('reviewstrike', escape(str)) + end + def inline_tti(str) macro('texttt', macro('textit', escape(str))) end def inline_ttb(str) macro('texttt', macro('textbf', escape(str))) end def inline_bib(id) - "[#{@chapter.bibpaper(id).number}]" + macro('reviewbibref', "[#{@chapter.bibpaper(id).number}]", bib_label(id)) end def inline_hd_chap(chap, id) - "「#{chap.headline_index.number(id)} #{chap.headline(id).caption}」" + "「#{chap.headline_index.number(id)} #{compile_inline(chap.headline(id).caption)}」" end def inline_raw(str) super(str) end @@ -641,21 +706,25 @@ def inline_sup(str) macro('textsuperscript', escape(str)) end def inline_em(str) - macro('textbf', escape(str)) + macro('reviewem', escape(str)) end def inline_strong(str) - macro('textbf', escape(str)) + macro('reviewstrong', escape(str)) end def inline_u(str) macro('Underline', escape(str)) end + def inline_ami(str) + macro('reviewami', escape(str)) + end + def inline_icon(id) macro('includegraphics', @chapter.image(id).path) end def inline_uchar(str) @@ -664,21 +733,22 @@ end def inline_comment(str) if ReVIEW.book.param["draft"] macro('pdfcomment', escape(str)) + else + "" end end def bibpaper_header(id, caption) puts "[#{@chapter.bibpaper(id).number}] #{compile_inline(caption)}" + puts macro('label', bib_label(id)) end def bibpaper_bibpaper(id, caption, lines) - lines.each do |line| - puts detab(line) - end + print split_paragraph(lines).join("") end def index(str) str.sub!(/\(\)/, '') decl = '' @@ -704,11 +774,11 @@ macro('reviewkw', escape(word)) end end def compile_href(url, label) - if /\A[a-z]+:\/\// =~ url + if /\A[a-z]+:/ =~ url if label macro("href", escape_url(url), escape(label)) else macro("url", escape_url(url)) end @@ -725,9 +795,13 @@ @latex_tsize = str end def image_ext "svg" + end + + def olnum(num) + @ol_num = num.to_i end end end