lib/kitabu/exporter/html.rb in kitabu-2.1.0 vs lib/kitabu/exporter/html.rb in kitabu-3.0.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
module Kitabu
class Exporter
class HTML < Base
class << self
# The footnote index control. We have to manipulate footnotes
@@ -19,11 +21,11 @@
File.open(root_dir.join("output/#{name}.html"), "w") do |file|
file << parse_layout(content)
end
true
- rescue Exception => error
+ rescue StandardError => error
handle_error(error)
false
end
def reset_footnote_index!
@@ -31,85 +33,89 @@
end
# Return all chapters wrapped in a <tt>div.chapter</tt> tag.
#
def content
- String.new.tap do |content|
+ buffer = [].tap do |content|
source_list.each_chapter do |files|
content << %[<div class="chapter">#{render_chapter(files)}</div>]
end
end
+
+ buffer.join
end
- private
# Render +file+ considering its extension.
#
- def render_file(file)
- if format(file) == :erb
- content = render_template(file, config)
- else
- content = File.read(file)
- end
+ private def render_file(file_path)
+ content = if file_format(file_path) == :erb
+ render_template(file_path, config)
+ else
+ File.read(file_path)
+ end
Kitabu::Markdown.render(content)
end
- def format(file)
- if File.extname(file) == '.erb'
+ private def file_format(file_path)
+ if File.extname(file_path) == ".erb"
:erb
else
:markdown
end
end
# Parse layout file, making available all configuration entries.
#
- def parse_layout(html)
+ private def parse_layout(html)
toc = TOC::HTML.generate(html)
- content = Footnotes::HTML.process(toc.content).html.css('body').first.inner_html
+ content =
+ Footnotes::HTML.process(toc.content).html.css("body").first.inner_html
- locals = config.merge({
+ locals = config.merge(
content: content,
toc: toc.to_html,
changelog: render_changelog
- })
+ )
render_template(root_dir.join("templates/html/layout.erb"), locals)
end
# Render changelog file.
# This file can be used to inform any book change.
#
- def render_changelog
+ private def render_changelog
changelog = Dir[root_dir.join("text/CHANGELOG.*")].first
render_file(changelog) if changelog
end
# Render all +files+ from a given chapter.
#
- def render_chapter(files)
- String.new.tap do |chapter|
+ private def render_chapter(files)
+ buffer = [].tap do |chapter|
files.each do |file|
chapter << render_file(file) << "\n\n"
end
end
+
+ buffer.join
end
# Copy images
#
- def copy_images!
+ private def copy_images!
copy_directory("images", "output/images")
end
# Copy font files
#
- def copy_fonts!
+ private def copy_fonts!
copy_directory("fonts", "output/fonts")
end
# Export all root stylesheets.
#
- def export_stylesheets!
+ private def export_stylesheets!
Exporter::CSS.new(root_dir).export
end
end
end
end