From ae9f65b7e8fa3ae236897ef470ec257f96826839 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 2 Apr 2016 03:20:37 +0900 Subject: [PATCH] unify template engine HTMLBuilder: use ReVIEW::Template instead of HTMLLayout --- lib/review/htmlbuilder.rb | 53 ++++++++++----------------------------- lib/review/htmllayout.rb | 43 ------------------------------- lib/review/template.rb | 2 ++ 3 files changed, 15 insertions(+), 83 deletions(-) delete mode 100644 lib/review/htmllayout.rb diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index 67032979a..7d60bcb02 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -11,7 +11,7 @@ require 'review/builder' require 'review/htmlutils' -require 'review/htmllayout' +require 'review/template' require 'review/textutils' module ReVIEW @@ -58,6 +58,12 @@ def builder_init_file private :builder_init_file def result + if @book.htmlversion == 5 + htmlfilename = "./html/layout-html5.html.erb" + else + htmlfilename = "./html/layout-xhtml1.html.erb" + end + layout_file = File.join(@book.basedir, "layouts", "layout.html.erb") if !File.exist?(layout_file) && File.exist?(File.join(@book.basedir, "layouts", "layout.erb")) raise ReVIEW::ConfigError, "layout.erb is obsoleted. Please use layout.html.erb." @@ -65,39 +71,10 @@ def result if File.exist?(layout_file) if ENV["REVIEW_SAFE_MODE"].to_i & 4 > 0 warn "user's layout is prohibited in safe mode. ignored." - else - title = strip_html(compile_inline(@chapter.title)) - language = @book.config['language'] - stylesheets = @book.config["stylesheet"] - - toc = "" - toc_level = 0 - @chapter.headline_index.items.each do |i| - caption = "
  • #{strip_html(compile_inline(i.caption))}
  • \n" - if toc_level == i.number.size - # do nothing - elsif toc_level < i.number.size - toc += "\n" * (toc_level - i.number.size) - toc_level = i.number.size - toc += "" * toc_level - - return messages() + - HTMLLayout.new( - {'body' => @output.string, 'title' => title, 'toc' => toc, - 'builder' => self, - 'language' => language, - 'stylesheets' => stylesheets, - 'next' => @chapter.next_chapter, - 'prev' => @chapter.prev_chapter}, - layout_file).result + layout_file = File.expand_path(htmlfilename, ReVIEW::Template::TEMPLATE_DIR) end + else + layout_file = File.expand_path(htmlfilename, ReVIEW::Template::TEMPLATE_DIR) end # default XHTML header/footer @@ -107,14 +84,10 @@ def result @body = @output.string @language = @book.config['language'] @stylesheets = @book.config["stylesheet"] + @next = @chapter.next_chapter + @prev = @chapter.prev_chapter - if @book.htmlversion == 5 - htmlfilename = "layout-html5.html.erb" - else - htmlfilename = "layout-xhtml1.html.erb" - end - tmplfile = File.expand_path('./html/'+htmlfilename, ReVIEW::Template::TEMPLATE_DIR) - tmpl = ReVIEW::Template.load(tmplfile) + tmpl = ReVIEW::Template.load(layout_file) tmpl.result(binding) end diff --git a/lib/review/htmllayout.rb b/lib/review/htmllayout.rb deleted file mode 100644 index 290a27a1b..000000000 --- a/lib/review/htmllayout.rb +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) 2009 Narihiro Nakamura -require 'erb' - -class HTMLLayout - include ERB::Util - - def initialize(params, template) - @body = params['body'] - @title = params['title'] - @toc = params['toc'] - @next = params['next'] - @prev = params['prev'] - @builder = params['builder'] - @language = params['language'] - @stylesheets = params['stylesheets'] - @template = template - end - attr_reader :body, :title, :toc - - def next_chapter - if @next.present? - "#{h @builder.compile_inline @next.title}" - else - "" - end - end - - def prev_chapter - if @prev.present? - "#{h @builder.compile_inline @prev.title}" - else - "" - end - end - - def result - if File.exist?(@template) - return ERB.new(IO.read(@template)).result(binding) - else - return @src - end - end -end diff --git a/lib/review/template.rb b/lib/review/template.rb index 8236cc31d..f56bdd970 100644 --- a/lib/review/template.rb +++ b/lib/review/template.rb @@ -1,6 +1,8 @@ require 'erb' module ReVIEW class Template + include ERB::Util + TEMPLATE_DIR = File.join(File.dirname(__FILE__), "../../templates") def self.load(filename, mode = 1)