lib/review/webtocprinter.rb in review-4.1.0 vs lib/review/webtocprinter.rb in review-4.2.0

- old
+ new

@@ -4,51 +4,54 @@ module ReVIEW class WEBTOCPrinter < TOCPrinter include HTMLUtils def self.book_to_string(book) - io = StringIO.new - ReVIEW::WEBTOCPrinter.new(1, {}, io).print_book(book) - io.seek(0) - io.read + ReVIEW::WEBTOCPrinter.new.print_book(book) end def print_book(book) - @out.puts '<ul class="book-toc">' - @out.puts %Q(<li><a href="index.html">TOP</a></li>\n) - book.each_part do |part| - print_part(part) - end - @out.puts '</ul>' + @book = book + @indent = nil + @upper = 1 # only part and chapter + print_result(build_result_array) end - def print_part(part) - if part.number - if part.file? - ext = part.book.config['htmlext'] || 'html' - path = File.basename(part.path.sub(/\.re/, '.' + ext)) - @out.puts %Q(<li><a href="#{path}">#{h(I18n.t('part_short', part.number) + ' ' + part.title)}</a>\n<ul>\n) + def print_result(result_array) + content = <<EOT +<ul class="book-toc"> +<li><a href="index.html">TOP</a></li> +EOT + + path = '' + result_array.each do |result| + unless result[:headline] + result[:headline] = '-' + end + + if result[:name] + path = "#{result[:name]}.#{@book.config['htmlext']}" + next + end + + if result[:part] + if result[:part] == 'end' + content << "</ul></li>\n" + end + next + end + + if path.start_with?('.') + content << "<li>#{escape(result[:headline])}" else - @out.puts %Q(<li>#{h(I18n.t('part_short', part.number) + ' ' + part.title)}\n<ul>\n) + content << %Q(<li><a href="#{path}">#{escape(result[:headline])}</a>) end + if result[:level] == 0 + content << "\n<ul>" # part + else + content << "</li>\n" + end end - part.each_chapter do |chap| - print_chapter(chap) - end - if part.number - @out.puts "</ul>\n</li>\n" - end - end - - def print_chapter(chap) - chap_node = TOCParser.chapter_node(chap) - ext = chap.book.config['htmlext'] || 'html' - path = File.basename(chap.path.sub(/\.re/, '.' + ext)) - label = if chap_node.number && chap.on_chaps? - "#{I18n.t('chapter_short', chap.number)} #{chap.title}" - else - chap.title - end - @out.puts %Q(<li><a href="#{path}">#{h(label)}</a></li>\n) + content << "</ul>\n" end end end