Skip to content

Commit

Permalink
add ReVIEW::WEBTOCPrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
takahashim committed Apr 22, 2016
1 parent f8cc1a6 commit 22b0740
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 20 deletions.
17 changes: 15 additions & 2 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'review/htmlutils'
require 'review/template'
require 'review/textutils'
require 'review/webtocprinter'

module ReVIEW

Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/review/htmlutils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 30 additions & 5 deletions lib/review/webmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -52,7 +53,7 @@ def parse_opts(args)
end

def build_path
"webroot"
@config["docroot"] || "webroot"
end

def remove_old_files(path)
Expand All @@ -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))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"]
Expand All @@ -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
<div id="cover-image" class="cover-image">
<img src="#{file}" class="max"/>
</div>
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 = ""
Expand Down
49 changes: 49 additions & 0 deletions lib/review/webtocprinter.rb
Original file line number Diff line number Diff line change
@@ -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 '<ul class="book-toc">'
@out.puts "<li><a href=\"index.html\">TOP</a></li>\n"
book.each_part do |part|
print_part(part)
end
@out.puts '</ul>'
end

def print_part(part)
if part.number
@out.puts "<li>#{h(part.title)}\n<ul>\n"
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 = chap.path.sub(/\.re/, "."+ext)
if chap_node.number && chap.on_CHAPS?
label = "#{chap.number} #{chap.title}"
else
label = chap.title
end
@out.puts "<li><a href=\"#{path}\">#{h(label)}</a></li>\n"
end

end
end
27 changes: 16 additions & 11 deletions templates/web/html/layout-html5.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@
<% @stylesheets.each do |style| %>
<link rel="stylesheet" type="text/css" href="<%= style %>" />
<% end %>
<% if @next.present? %><link rel="next" href="<%= h(@next.id.to_s+"."+@book.config['htmlext']) %>"><% end %>
<% if @prev.present? %><link rel="prev" href="<%= h(@prev.id.to_s+"."+@book.config['htmlext']) %>"><% end %>
<% end%>
<meta name="generator" content="Re:VIEW" />
<title><%= @title %></title>
</head>
<body<%= @body_ext %>>
<div id="content">
<header>
<div class="nav">
<%= @toc %>
</div>
</header>
<div class="main">
<div class="book">
<nav role="navigation" class="side-content">
<%= @toc %>
</nav>
<div class="book-body">
<%= @body %>
</div>
<footer>
<%= @next %>
<%= @prev %>
</footer>
<footer>
<% if @prev.present? %>
<a href="<%= h(@prev.id.to_s+"."+@book.config['htmlext']) %>"><%= h(@prev_title) %></a> |
<% end %>
<a href="index.html">TOP</a>
<% if @next.present? %>
| <a href="<%= h(@next.id.to_s+"."+@book.config['htmlext']) %>"><%= h(@next_title) %></a>
<% end %>
</footer>
</div>
</body>
</html>
4 changes: 2 additions & 2 deletions test/test_i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 22b0740

Please sign in to comment.