diff --git a/bin/review-index b/bin/review-index index d959c9ad0..014d12b8c 100755 --- a/bin/review-index +++ b/bin/review-index @@ -41,11 +41,7 @@ def _main opts = OptionParser.new opts.version = ReVIEW::VERSION opts.on('-a', '--all', 'print all chapters.') { - begin - source = book - rescue ReVIEW::Error => err - error_exit err.message - end + source = book } opts.on('-p', '--part N', 'list only part N.') {|n| source = book.part(Integer(n)) or @@ -71,9 +67,6 @@ def _main opts.on('--html', 'output in HTML (deprecated)') { printer_class = ReVIEW::HTMLTOCPrinter } - opts.on('--idg', 'output in InDesign XML') { - printer_class = ReVIEW::IDGTOCPrinter - } opts.on('--help', 'print this message and quit.') { puts opts.help exit 0 @@ -95,7 +88,12 @@ def _main end begin - printer_class.new(upper, param).print_book source + printer = printer_class.new(upper, param) + if source.kind_of?(ReVIEW::Book::Part) + printer.print_part source + else + printer.print_book source + end rescue ReVIEW::Error, Errno::ENOENT => err raise if $DEBUG error_exit err.message diff --git a/lib/review/book/chapter.rb b/lib/review/book/chapter.rb index 977739f7a..9762976c6 100644 --- a/lib/review/book/chapter.rb +++ b/lib/review/book/chapter.rb @@ -34,6 +34,7 @@ def initialize(book, number, name, path, io = nil) @indepimage_index = nil @headline_index = nil @column_index = nil + @volume = nil end def inspect diff --git a/lib/review/book/index.rb b/lib/review/book/index.rb index 23c839451..27ca30147 100644 --- a/lib/review/book/index.rb +++ b/lib/review/book/index.rb @@ -380,7 +380,7 @@ def ColumnIndex.parse(src, *args) seq = 1 src.each do |line| if m = COLUMN_PATTERN.match(line) - level = m[1] ## not use it yet + _level = m[1] ## not use it yet id = m[2] caption = m[3].strip if !id || id == "" diff --git a/lib/review/book/part.rb b/lib/review/book/part.rb index 7da89feea..b721dc718 100644 --- a/lib/review/book/part.rb +++ b/lib/review/book/part.rb @@ -21,6 +21,7 @@ def initialize(book, number, chapters, name="") @chapters = chapters @path = name @name = name ? File.basename(name, '.re') : nil + @volume = nil end attr_reader :number diff --git a/lib/review/extentions/string.rb b/lib/review/extentions/string.rb index b5ef853ac..fad6df49f 100644 --- a/lib/review/extentions/string.rb +++ b/lib/review/extentions/string.rb @@ -1,4 +1,5 @@ -if defined?(Encoding) && Encoding.respond_to?("default_external") +if defined?(Encoding) && Encoding.respond_to?("default_external") && + Encoding.default_external != Encoding::UTF_8 Encoding.default_external = "UTF-8" end diff --git a/lib/review/preprocessor.rb b/lib/review/preprocessor.rb index f70f237a5..127e5028d 100644 --- a/lib/review/preprocessor.rb +++ b/lib/review/preprocessor.rb @@ -304,7 +304,7 @@ def check_ruby_syntax(rbfile) end def spawn - pid, status = *Process.waitpid2(fork { yield }) + _pid, status = *Process.waitpid2(fork { yield }) ## pid not used status end diff --git a/lib/review/tocparser.rb b/lib/review/tocparser.rb index 131e41785..3c03fb834 100644 --- a/lib/review/tocparser.rb +++ b/lib/review/tocparser.rb @@ -27,6 +27,14 @@ def TOCParser.parse(chap) end end + def TOCParser.chapter_node(chap) + toc = TOCParser.parse(chap) + unless toc.size == 1 + $stderr.puts "warning: chapter #{toc.join} contains more than 1 chapter" + end + toc.first + end + def parse(f, chap) roots = [] ## list of chapters node_stack = [] @@ -285,55 +293,5 @@ def yield_section end end - - - module TOCRoot - def level - 0 - end - - def chapter? - false - end - - def each_section_with_index - idx = -1 - each_section do |node| - yield node, (idx += 1) - end - end - - def each_section(&block) - each_chapter do |chap| - yield chap.toc - end - end - - def section_size - chapters.size - end - - def estimated_lines - chapters.inject(0) {|sum, chap| sum + chap.toc.estimated_lines } - end - end - - class Book::Base # reopen - include TOCRoot - end - - class Book::Part - include TOCRoot - end - - class Book::Chapter # reopen - def toc - @toc ||= TOCParser.parse(self) - unless @toc.size == 1 - $stderr.puts "warning: chapter #{@toc.join} contains more than 1 chapter" - end - @toc.first - end - end - end + diff --git a/lib/review/tocprinter.rb b/lib/review/tocprinter.rb index 6306605ba..1ff2b3a86 100644 --- a/lib/review/tocprinter.rb +++ b/lib/review/tocprinter.rb @@ -11,8 +11,7 @@ # For details of LGPL, see the file "COPYING". # -require 'review/htmlutils' -require 'review/htmllayout' +require 'review' module ReVIEW @@ -27,6 +26,24 @@ def initialize(print_upper, param) @config = param end + def print_book(book) + book.each_part do |part| + print_part(part) + end + end + + def print_part(part) + part.each_chapter do |chap| + print_chapter(chap) + end + end + + def print_chapter(chap) + chap_node = TOCParser.chapter_node(chap) + print_node 1, chap_node + print_children chap_node + end + def print?(level) level <= @print_upper end @@ -34,9 +51,6 @@ def print?(level) class TextTOCPrinter < TOCPrinter - def print_book(book) - print_children book - end private @@ -82,36 +96,34 @@ class HTMLTOCPrinter < TOCPrinter include HTMLUtils def print_book(book) - return unless print?(1) - html = "" + puts '' + end + + def print_part(part) + puts li(part.name) if part.name + super + end + + def print_chapter(chap) + chap_node = TOCParser.chapter_node(chap) + ext = chap.book.config["htmlext"] || ".html" + path = chap.path.sub(/\.re/, ext) + if chap_node.number + name = "chap#{chap_node.number}" + label = "#{chap.number} #{chap.title}" + else + label = chap.title end - if File.exist?(layout_file) - puts HTMLLayout.new( - {'body' => html, 'title' => "目次"}, layout_file).result + puts li(a_name(path, escape_html(label))) + return unless print?(2) + if print?(3) + puts chap_sections_to_s(chap_node) else - puts html + puts chapter_to_s(chap_node) end end @@ -130,30 +142,19 @@ def chap_sections_to_s(chap) def chapter_to_s(chap) res = [] chap.each_section do |sec| - res << h3(escape_html(sec.label)) + res << li(escape_html(sec.label)) next unless print?(4) - next if sec.section_size == 0 - res << "" end return res.join("\n") end - def h1(label) - "

#{label}

" - end - - def h2(label) - "

#{label}

" - end - - def h3(label) - "

#{label}

" - end - def li(content) "
  • #{content}
  • " end @@ -164,46 +165,4 @@ def a_name(name, label) end - class IDGTOCPrinter < TOCPrinter - def print_book(book) - puts %Q() - puts %Q(1 パート1) # FIXME: 部タイトルを取るには? & 部ごとに結果を分けるには? - puts %Q() - end - - private - - def print_children(node) - return unless print?(node.level + 1) - node.each_section_with_index do |sec, idx| - print_node idx+1, sec - print_children sec - end - end - - LABEL_LEN = 54 - - def print_node(seq, node) - if node.chapter? - vol = node.volume - printf "
  • %s
  • \n", - "#{chapnumstr(node.number)}#{node.label}" - else - printf "
  • %-#{LABEL_LEN}s\n", - " #{' ' * (node.level - 1)}#{seq} #{node.label}
  • " - end - end - - def chapnumstr(n) - n ? sprintf('第%d章 ', n) : '' - end - - def volume_columns(level, volstr) - cols = ["", "", "", nil] - cols[level - 1] = volstr - cols[0, 3] - end - end end