lib/review/latexbuilder.rb in review-1.4.0 vs lib/review/latexbuilder.rb in review-1.5.0

- old
+ new

@@ -11,11 +11,10 @@ # require 'review/builder' require 'review/latexutils' require 'review/textutils' -require 'review/sec_counter' module ReVIEW class LATEXBuilder < Builder @@ -73,29 +72,24 @@ 4 => 'subsubsection', 5 => 'paragraph', 6 => 'subparagraph' } - def headline_prefix(level) - @sec_counter.inc(level) - anchor = @sec_counter.anchor(level) - prefix = @sec_counter.prefix(level, @book.config["secnolevel"]) - [prefix, anchor] - end - private :headline_prefix - - def headline(level, label, caption) _, anchor = headline_prefix(level) + headline_name = HEADLINE[level] + if @chapter.kind_of? ReVIEW::Book::Part + headline_name = "part" + end prefix = "" if level > @book.config["secnolevel"] || (@chapter.number.to_s.empty? && level > 1) prefix = "*" end blank unless @output.pos == 0 - puts macro(HEADLINE[level]+prefix, compile_inline(caption)) + puts macro(headline_name+prefix, compile_inline(caption)) if prefix == "*" && level <= @book.config["toclevel"].to_i - puts "\\addcontentsline{toc}{#{HEADLINE[level]}}{#{compile_inline(caption)}}" + puts "\\addcontentsline{toc}{#{headline_name}}{#{compile_inline(caption)}}" end if level == 1 puts macro('label', chapter_label) else puts macro('label', sec_label(anchor)) @@ -234,88 +228,116 @@ latex_block 'quotation', lines end alias_method :lead, :read - def emlist(lines, caption = nil) + def highlight_listings? + @book.config["highlight"] && @book.config["highlight"]["latex"] == "listings" + end + private :highlight_listings? + + def emlist(lines, caption = nil, lang = nil) blank - if caption - puts macro('reviewemlistcaption', "#{compile_inline(caption)}") + if highlight_listings? + common_code_block_lst(lines, 'reviewemlistlst', 'title', caption, lang) + else + common_code_block(lines, 'reviewemlist', caption, lang) do |line, idx| + detab(line) + "\n" + end end - puts '\begin{reviewemlist}' - lines.each do |line| - puts detab(line) - end - puts '\end{reviewemlist}' - blank end - def emlistnum(lines, caption = nil) + def emlistnum(lines, caption = nil, lang = nil) blank - if caption - puts macro('reviewemlistcaption', "#{compile_inline(caption)}") + if highlight_listings? + common_code_block_lst(lines, 'reviewemlistnumlst', 'title', caption, lang) + else + common_code_block(lines, 'reviewemlist', caption, lang) do |line, idx| + detab((idx+1).to_s.rjust(2)+": " + line) + "\n" + end end - puts '\begin{reviewemlist}' - lines.each_with_index do |line, i| - puts detab((i+1).to_s.rjust(2) + ": " + line) + end + + ## override Builder#list + def list(lines, id, caption, lang = nil) + if highlight_listings? + common_code_block_lst(lines, 'reviewlistlst', 'caption', caption, lang) + else + begin + puts macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}") + rescue KeyError + error "no such list: #{id}" + end + common_code_block(lines, 'reviewlist', nil, lang) do |line, idx| + detab(line) + "\n" + end end - puts '\end{reviewemlist}' - blank end - def listnum_body(lines) - puts '\begin{reviewlist}' - lines.each_with_index do |line, i| - puts detab((i+1).to_s.rjust(2) + ": " + line) + ## override Builder#listnum + def listnum(lines, id, caption, lang = nil) + if highlight_listings? + common_code_block_lst(lines, 'reviewlistnumlst', 'caption', caption, lang) + else + begin + puts macro('reviewlistcaption', "#{I18n.t("list")}#{I18n.t("format_number_header", [@chapter.number, @chapter.list(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}") + rescue KeyError + error "no such list: #{id}" + end + common_code_block(lines, 'reviewlist', caption, lang) do |line, idx| + detab((idx+1).to_s.rjust(2)+": " + line) + "\n" + end end - puts '\end{reviewlist}' - blank + end + def cmd(lines, caption = nil, lang = nil) + if highlight_listings? + common_code_block_lst(lines, 'reviewcmdlst', 'title', caption, lang) + else + blank + common_code_block(lines, 'reviewcmd', caption, lang) do |line, idx| + detab(line) + "\n" + end + end end - def cmd(lines, caption = nil) - blank + def common_code_block(lines, command, caption, lang) if caption - puts macro('reviewcmdcaption', "#{compile_inline(caption)}") + puts macro(command + 'caption', "#{compile_inline(caption)}") end - puts '\begin{reviewcmd}' - lines.each do |line| - puts detab(line) + body = "" + lines.each_with_index do |line, idx| + body.concat(yield(line, idx)) end - puts '\end{reviewcmd}' + puts macro('begin' ,command) + print body + puts macro('end' ,command) blank end - def list_header(id, 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(id, lines) - puts '\begin{reviewlist}' - lines.each do |line| - puts detab(line) + def common_code_block_lst(lines, command, title, caption, lang) + caption_str = compile_inline((caption || "")) + if title == "title" && caption_str == "" + caption_str = "\\relax" ## dummy charactor to remove lstname + print "\\vspace{-1.5em}" end - puts '\end{reviewlist}' - puts "" + lexer = lang || "" + body = lines.inject(''){|i, j| i + detab(unescape_latex(j)) + "\n"} + puts "\\begin{"+command+"}["+title+"={"+caption_str+"},language={"+ lexer+"}]" + print body + puts "\\end{"+ command + "}" + blank end def source(lines, caption) puts '\begin{reviewlist}' - source_header caption - source_body lines - puts '\end{reviewlist}' - puts "" - end - - def source_header(caption) puts macro('reviewlistcaption', compile_inline(caption)) - end - - def source_body(lines) lines.each do |line| puts detab(line) end + puts '\end{reviewlist}' + puts "" end def image_header(id, caption) end @@ -371,12 +393,13 @@ def sec_label(sec_anchor) "sec:#{sec_anchor}" end private :sec_label - def table_label(id) - "table:#{@chapter.id}:#{id}" + def table_label(id, chapter=nil) + chapter ||= @chapter + "table:#{chapter.id}:#{id}" end private :table_label def bib_label(id) "bib:#{id}" @@ -591,13 +614,13 @@ nofunc_text("[UnknownChapter:#{id}]") end def inline_chap(id) if @book.config["chapterlink"] - "\\hyperref[chap:#{id}]{#{@chapter.env.chapter_index.number(id)}}" + "\\hyperref[chap:#{id}]{#{@book.chapter_index.number(id)}}" else - @chapter.env.chapter_index.number(id) + @book.chapter_index.number(id) end rescue KeyError error "unknown chapter: #{id}" nofunc_text("[UnknownChapter:#{id}]") end @@ -621,10 +644,10 @@ 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}", table_label(id)) + macro('reviewtableref', "#{chapter.number}.#{chapter.table(id).number}", table_label(id, chapter)) end def inline_img(id) chapter, id = extract_chapter_id(id) macro('reviewimageref', "#{chapter.number}.#{chapter.image(id).number}", image_label(id, chapter))