# -*- coding: utf-8 -*- # # $Id: tocprinter.rb 4309 2009-07-19 04:15:02Z aamine $ # # Copyright (c) 2002-2007 Minero Aoki # 2008-2009 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. # For details of LGPL, see the file "COPYING". # require 'review/htmlutils' require 'review/htmllayout' module ReVIEW class TOCPrinter def TOCPrinter.default_upper_level 99 # no one use 99 level nest end def initialize(print_upper, param) @print_upper = print_upper @config = param end def print?(level) level <= @print_upper end end class TextTOCPrinter < TOCPrinter def print_book(book) print_children book end private def print_children(node) return unless print?(node.level + 1) node.each_section_with_index do |section, idx| unless section.blank? print_node idx+1, section print_children section end end end def print_node(number, node) if node.chapter? vol = node.volume printf "%3s %3dKB %6dC %5dL %s (%s)\n", chapnumstr(node.number), vol.kbytes, vol.chars, vol.lines, node.label, node.chapter_id else printf "%17s %5dL %s\n", '', node.estimated_lines, " #{' ' * (node.level - 1)}#{number} #{node.label}" end end def chapnumstr(n) n ? sprintf('%2d.', n) : ' ' end def volume_columns(level, volstr) cols = ["", "", "", nil] cols[level - 1] = volstr cols[0, 3] # does not display volume of level-4 section end end class HTMLTOCPrinter < TOCPrinter include HTMLUtils def print_book(book) return unless print?(1) html = "" book.each_part do |part| html << h1(part.name) if part.name part.each_section do |chap| if chap.number name = "chap#{chap.number}" label = "第#{chap.number}章 #{chap.label}" html << h2(a_name(escape_html(name), escape_html(label))) else label = "#{chap.label}" html << h2(escape_html(label)) end return unless print?(2) if print?(3) html << chap_sections_to_s(chap) else html << chapter_to_s(chap) end end end 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 puts HTMLLayout.new( {'body' => html, 'title' => "目次"}, layout_file).result end private def chap_sections_to_s(chap) res = [] res << "