From 22b07401e47df69c0341562d19b2ca152fd960ad Mon Sep 17 00:00:00 2001 From: takahashim Date: Thu, 21 Apr 2016 02:18:51 +0900 Subject: [PATCH] add ReVIEW::WEBTOCPrinter --- lib/review/htmlbuilder.rb | 17 +++++++- lib/review/htmlutils.rb | 1 + lib/review/webmaker.rb | 35 ++++++++++++++--- lib/review/webtocprinter.rb | 49 ++++++++++++++++++++++++ templates/web/html/layout-html5.html.erb | 27 +++++++------ test/test_i18n.rb | 4 +- 6 files changed, 113 insertions(+), 20 deletions(-) create mode 100644 lib/review/webtocprinter.rb diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 2a0feec22..4182d4a35 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -13,6 +13,7 @@ require 'review/htmlutils' require 'review/template' require 'review/textutils' +require 'review/webtocprinter' module ReVIEW @@ -57,14 +58,20 @@ def builder_init_file @sec_counter = SecCounter.new(5, @chapter) @nonum_counter = 0 @body_ext = nil + @toc = nil end private :builder_init_file def result + if @book.config.maker == "webmaker" + htmldir = "web/html" + else + htmldir = "html" + end if @book.htmlversion == 5 - htmlfilename = "./html/layout-html5.html.erb" + htmlfilename = File.join(htmldir, "layout-html5.html.erb") else - htmlfilename = "./html/layout-xhtml1.html.erb" + htmlfilename = File.join(htmldir, "layout-xhtml1.html.erb") end layout_file = File.join(@book.basedir, "layouts", "layout.html.erb") @@ -89,6 +96,12 @@ def result @stylesheets = @book.config["stylesheet"] @next = @chapter.next_chapter @prev = @chapter.prev_chapter + @next_title = @next ? compile_inline(@next.title) : "" + @prev_title = @prev ? compile_inline(@prev.title) : "" + + if @book.config.maker == "webmaker" + @toc = ReVIEW::WEBTOCPrinter.book_to_string(@book) + end tmpl = ReVIEW::Template.load(layout_file) tmpl.result(binding) diff --git a/lib/review/htmlutils.rb b/lib/review/htmlutils.rb index 1ddccdb06..26835c889 100644 --- a/lib/review/htmlutils.rb +++ b/lib/review/htmlutils.rb @@ -24,6 +24,7 @@ def escape_html(str) end alias_method :escape, :escape_html + alias_method :h, :escape_html def unescape_html(str) # FIXME better code diff --git a/lib/review/webmaker.rb b/lib/review/webmaker.rb index 7176f27d0..dc1f610d2 100644 --- a/lib/review/webmaker.rb +++ b/lib/review/webmaker.rb @@ -13,10 +13,11 @@ require 'review' require 'review/i18n' require 'review/converter' - +require 'erb' module ReVIEW class WEBMaker + include ERB::Util attr_accessor :config, :basedir @@ -52,7 +53,7 @@ def parse_opts(args) end def build_path - "webroot" + @config["docroot"] || "webroot" end def remove_old_files(path) @@ -62,6 +63,7 @@ def remove_old_files(path) def execute(*args) @config = ReVIEW::Configure.values + @config.maker = "webmaker" cmd_config, yamlfile = parse_opts(args) @config.merge!(YAML.load_file(yamlfile)) @@ -142,9 +144,9 @@ def build_part(part, basetmpdir, htmlfile) def template_name if @config["htmlversion"].to_i == 5 - './html/layout-html5.html.erb' + 'web/html/layout-html5.html.erb' else - './html/layout-xhtml1.html.erb' + 'web/html/layout-xhtml1.html.erb' end end @@ -213,7 +215,7 @@ def copy_stylesheet(basetmpdir) end def copy_frontmatter(basetmpdir) - copy_file_with_param("cover") + build_indexpage(basetmpdir) if @config["titlepage"] if @config["titlefile"] @@ -227,6 +229,29 @@ def copy_frontmatter(basetmpdir) copy_file_with_param("originaltitlefile") end + def build_indexpage(basetmpdir) + File.open("#{basetmpdir}/index.html", "w") do |f| + if @config["coverimage"] + file = File.join("images", @config["coverimage"]) + @body = <<-EOT +
+ +
+ EOT + else + @body = "" + end + @language = @config['language'] + @stylesheets = @config["stylesheet"] + @toc = ReVIEW::WEBTOCPrinter.book_to_string(@book) + @next = @book.chapters[0] + @next_title = @next ? @next.title : "" + tmplfile = File.expand_path(template_name, ReVIEW::Template::TEMPLATE_DIR) + tmpl = ReVIEW::Template.load(tmplfile) + f.write tmpl.result(binding) + end + end + def build_titlepage(basetmpdir, htmlfile) File.open("#{basetmpdir}/#{htmlfile}", "w") do |f| @body = "" diff --git a/lib/review/webtocprinter.rb b/lib/review/webtocprinter.rb new file mode 100644 index 000000000..121d720bc --- /dev/null +++ b/lib/review/webtocprinter.rb @@ -0,0 +1,49 @@ +require 'review' +require 'review/tocprinter' + +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 + end + + def print_book(book) + @out.puts '' + end + + def print_part(part) + if part.number + @out.puts "
  • #{h(part.title)}\n\n
  • \n" + end + 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 && chap.on_CHAPS? + label = "#{chap.number} #{chap.title}" + else + label = chap.title + end + @out.puts "
  • #{h(label)}
  • \n" + end + + end +end diff --git a/templates/web/html/layout-html5.html.erb b/templates/web/html/layout-html5.html.erb index f557df6c2..d71020e77 100644 --- a/templates/web/html/layout-html5.html.erb +++ b/templates/web/html/layout-html5.html.erb @@ -7,24 +7,29 @@ <% @stylesheets.each do |style| %> <% end %> +<% if @next.present? %>"><% end %> +<% if @prev.present? %>"><% end %> <% end%> <%= @title %> > -
    -
    - -
    -
    +
    + +
    <%= @body %>
    -
    - <%= @next %> - <%= @prev %> -
    +
    diff --git a/test/test_i18n.rb b/test/test_i18n.rb index f8ed318e8..35e85e458 100644 --- a/test/test_i18n.rb +++ b/test/test_i18n.rb @@ -194,11 +194,11 @@ def test_htmlbuilder def _setup_htmlbuilder I18n.setup "en" @builder = HTMLBuilder.new() - @config = { + @config = ReVIEW::Configure[ "secnolevel" => 2, # for IDGXMLBuilder, HTMLBuilder "stylesheet" => nil, # for HTMLBuilder "ext" => ".re" - } + ] @book = Book::Base.new(".") @book.config = @config @compiler = ReVIEW::Compiler.new(@builder)