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 eb0e8eb commit 56a8af4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 19 deletions.
8 changes: 5 additions & 3 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,6 +58,7 @@ def builder_init_file
@sec_counter = SecCounter.new(5, @chapter)
@nonum_counter = 0
@body_ext = nil
@toc = nil
end
private :builder_init_file

Expand Down Expand Up @@ -97,9 +99,9 @@ def result
@next_title = @next ? compile_inline(@next.title) : ""
@prev_title = @prev ? compile_inline(@prev.title) : ""

io = StringIO.new
ReVIEW::HTMLTOCPrinter.new(10, {}, io).print_book(@book)
@toc = io.read
if @book.config.maker == "webmaker"
@toc = ReVIEW::WEBTOCPrinter.book_to_string(@book)
end

tmpl = ReVIEW::Template.load(layout_file)
tmpl.result(binding)
Expand Down
34 changes: 29 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 Down Expand Up @@ -143,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 @@ -214,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 @@ -228,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
23 changes: 12 additions & 11 deletions templates/web/html/layout-html5.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +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>
<footer>
<% if @prev.present? %>
<a href="<%= h(@prev.id.to_s+"."+@book.config['htmlext']) %>"><%= h(@prev_title) %></a>
<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>
| <a href="<%= h(@next.id.to_s+"."+@book.config['htmlext']) %>"><%= h(@next_title) %></a>
<% end %>
</footer>
</footer>
</div>
</body>
</html>

0 comments on commit 56a8af4

Please sign in to comment.