Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ReVIEW::Template.generate #1648

Merged
merged 1 commit into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 29 additions & 39 deletions lib/epubmaker/epubcommon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def ncx(indentarray)
# Return container content.
def container
@opf_path = opf_path
tmplfile = File.expand_path('./xml/container.xml.erb', ReVIEW::Template::TEMPLATE_DIR)
tmpl = ReVIEW::Template.load(tmplfile)
tmpl.result(binding)
ReVIEW::Template.generate(path: './xml/container.xml.erb', binding: binding)
end

def coverimage
Expand All @@ -109,19 +107,17 @@ def cover
@coverimage_src = coverimage
raise "coverimage #{config['coverimage']} not found. Abort." unless @coverimage_src
end
tmplfile0 = File.expand_path('./html/_cover.html.erb', ReVIEW::Template::TEMPLATE_DIR)
@body = ReVIEW::Template.load(tmplfile0).result(binding)
@body = ReVIEW::Template.generate(path: './html/_cover.html.erb', binding: binding)

@title = h(config.name_of('title'))
@language = config['language']
@stylesheets = config['stylesheet']
tmplfile = if config['htmlversion'].to_i == 5
File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR)
else
File.expand_path('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR)
end
tmpl = ReVIEW::Template.load(tmplfile)
tmpl.result(binding)
template_path = if config['htmlversion'].to_i == 5
'./html/layout-html5.html.erb'
else
'./html/layout-xhtml1.html.erb'
end
ReVIEW::Template.generate(path: template_path, binding: binding)
end

# Return title (copying) content.
Expand All @@ -140,37 +136,33 @@ def titlepage
if config.names_of('pbl')
@publisher_str = join_with_separator(config.names_of('pbl'), ReVIEW::I18n.t('names_splitter'))
end
tmplfile0 = File.expand_path('./html/_titlepage.html.erb', ReVIEW::Template::TEMPLATE_DIR)
@body = ReVIEW::Template.load(tmplfile0).result(binding)
@body = ReVIEW::Template.generate(path: './html/_titlepage.html.erb', binding: binding)

@language = config['language']
@stylesheets = config['stylesheet']
tmplfile = if config['htmlversion'].to_i == 5
File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR)
else
File.expand_path('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR)
end
tmpl = ReVIEW::Template.load(tmplfile)
tmpl.result(binding)
template_path = if config['htmlversion'].to_i == 5
'./html/layout-html5.html.erb'
else
'./html/layout-xhtml1.html.erb'
end
ReVIEW::Template.generate(path: template_path, binding: binding)
end

# Return colophon content.
def colophon
@title = h(ReVIEW::I18n.t('colophontitle'))
@isbn_hyphen = isbn_hyphen

tmplfile0 = File.expand_path('./html/_colophon.html.erb', ReVIEW::Template::TEMPLATE_DIR)
@body = ReVIEW::Template.load(tmplfile0).result(binding)
@body = ReVIEW::Template.generate(path: './html/_colophon.html.erb', binding: binding)

@language = config['language']
@stylesheets = config['stylesheet']
tmplfile = if config['htmlversion'].to_i == 5
File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR)
else
File.expand_path('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR)
end
tmpl = ReVIEW::Template.load(tmplfile)
tmpl.result(binding)
template_path = if config['htmlversion'].to_i == 5
'./html/layout-html5.html.erb'
else
'./html/layout-xhtml1.html.erb'
end
ReVIEW::Template.generate(path: template_path, binding: binding)
end

def isbn_hyphen
Expand Down Expand Up @@ -205,8 +197,7 @@ def colophon_history
end
end

tmplfile = File.expand_path('./html/_colophon_history.html.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './html/_colophon_history.html.erb', binding: binding)
end

def date_to_s(date)
Expand All @@ -228,13 +219,12 @@ def mytoc

@language = config['language']
@stylesheets = config['stylesheet']
tmplfile = if config['htmlversion'].to_i == 5
File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR)
else
File.expand_path('./html/layout-xhtml1.html.erb', ReVIEW::Template::TEMPLATE_DIR)
end
tmpl = ReVIEW::Template.load(tmplfile)
tmpl.result(binding)
template_path = if config['htmlversion'].to_i == 5
'./html/layout-html5.html.erb'
else
'./html/layout-xhtml1.html.erb'
end
ReVIEW::Template.generate(path: template_path, binding: binding)
end

def hierarchy_ncx(type)
Expand Down
16 changes: 5 additions & 11 deletions lib/epubmaker/epubv2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def opf
@opf_manifest = opf_manifest
@opf_toc = opf_tocx

tmplfile = File.expand_path('./opf/epubv2.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
tmpl = ReVIEW::Template.load(tmplfile)
tmpl.result(binding)
ReVIEW::Template.generate(path: './opf/epubv2.opf.erb', binding: binding)
end

def opf_metainfo
Expand Down Expand Up @@ -60,15 +58,13 @@ def opf_metainfo
end
end.flatten.compact

tmplfile = File.expand_path('./opf/opf_metainfo_epubv2.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './opf/opf_metainfo_epubv2.opf.erb', binding: binding)
end

def opf_manifest
@items = contents.find_all { |item| item.file !~ /#/ } # skip subgroup

tmplfile = File.expand_path('./opf/opf_manifest_epubv2.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './opf/opf_manifest_epubv2.opf.erb', binding: binding)
end

def opf_tocx
Expand All @@ -79,8 +75,7 @@ def opf_tocx
end
@ncx_contents = contents.find_all { |content| content.media =~ /xhtml\+xml/ } # skip non XHTML

tmplfile = File.expand_path('./opf/opf_tocx_epubv2.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './opf/opf_tocx_epubv2.opf.erb', binding: binding)
end

# Return ncx content. +indentarray+ has prefix marks for each level.
Expand All @@ -89,8 +84,7 @@ def ncx(indentarray)
@ncx_doctitle = ncx_doctitle
@ncx_navmap = ncx_navmap(indentarray)

tmplfile = File.expand_path('./ncx/epubv2.ncx.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './ncx/epubv2.ncx.erb', binding: binding)
end

def ncx_isbn
Expand Down
15 changes: 5 additions & 10 deletions lib/epubmaker/epubv3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def opf
@package_attrs << %Q( prefix="#{prefixes_str}")
end

tmplfile = File.expand_path('./opf/epubv3.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './opf/epubv3.opf.erb', binding: binding)
end

def opf_metainfo
Expand Down Expand Up @@ -146,8 +145,7 @@ def opf_metainfo
end
end.flatten.compact

tmplfile = File.expand_path('./opf/opf_metainfo_epubv3.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './opf/opf_metainfo_epubv3.opf.erb', binding: binding)
end

def opf_manifest
Expand All @@ -160,8 +158,7 @@ def opf_manifest
contents.find_all { |content| content.file !~ /#/ }
end

tmplfile = File.expand_path('./opf/opf_manifest_epubv3.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './opf/opf_manifest_epubv3.opf.erb', binding: binding)
end

def opf_tocx
Expand All @@ -178,8 +175,7 @@ def opf_tocx
@tocx_contents << item
end

tmplfile = File.expand_path('./opf/opf_tocx_epubv3.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './opf/opf_tocx_epubv3.opf.erb', binding: binding)
end

def ncx(indentarray)
Expand All @@ -198,8 +194,7 @@ def ncx(indentarray)
@title = h(ReVIEW::I18n.t('toctitle'))
@language = config['language']
@stylesheets = config['stylesheet']
tmplfile = File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR)
ReVIEW::Template.load(tmplfile).result(binding)
ReVIEW::Template.generate(path: './html/layout-html5.html.erb', binding: binding)
end

# Produce EPUB file +epubfile+.
Expand Down
8 changes: 2 additions & 6 deletions lib/review/epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,7 @@ def build_part(part, basetmpdir, htmlfile)

@language = @producer.config['language']
@stylesheets = @producer.config['stylesheet']
tmplfile = File.expand_path(template_name, ReVIEW::Template::TEMPLATE_DIR)
tmpl = ReVIEW::Template.load(tmplfile)
f.write tmpl.result(binding)
f.write ReVIEW::Template.generate(path: template_name, binding: binding)
end
end

Expand Down Expand Up @@ -571,9 +569,7 @@ def build_titlepage(basetmpdir, htmlfile)

@language = @producer.config['language']
@stylesheets = @producer.config['stylesheet']
tmplfile = File.expand_path(template_name, ReVIEW::Template::TEMPLATE_DIR)
tmpl = ReVIEW::Template.load(tmplfile)
f.write tmpl.result(binding)
f.write ReVIEW::Template.generate(path: template_name, binding: binding)
end
end

Expand Down
22 changes: 9 additions & 13 deletions lib/review/pdfmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,34 +450,30 @@ def erb_config
@locale_latex['postappendixname'] = appendix_tuple[1].to_s
end

def erb_content(file)
@texcompiler = File.basename(@config['texcommand'], '.*')
erb = ReVIEW::Template.load(file, '-')
@logger.debug("erb processes #{File.basename(file)}") if @config['debug']
erb.result(binding)
end

def latex_config
result = erb_content(File.expand_path('./latex/config.erb', ReVIEW::Template::TEMPLATE_DIR))
result = ReVIEW::Template.generate(path: './latex/config.erb', mode: '-', binding: binding)
local_config_file = File.join(@basedir, 'layouts', 'config-local.tex.erb')
if File.exist?(local_config_file)
result << "%% BEGIN: config-local.tex.erb\n"
result << erb_content(local_config_file)
result << ReVIEW::Template.generate(path: 'layouts/config-local.tex.erb', mode: '-', binding: binding, template_dir: @basedir)
result << "%% END: config-local.tex.erb\n"
end
result
end

def template_content
template = File.expand_path('./latex/layout.tex.erb', ReVIEW::Template::TEMPLATE_DIR)
template_dir = ReVIEW::Template::TEMPLATE_DIR
if @config.check_version('2', exception: false)
template = File.expand_path('./latex-compat2/layout.tex.erb', ReVIEW::Template::TEMPLATE_DIR)
template_path = './latex-compat2/layout.tex.erb'
else
template_path = './latex/layout.tex.erb'
end
layout_file = File.join(@basedir, 'layouts', 'layout.tex.erb')
if File.exist?(layout_file)
template = layout_file
template_dir = @basedir
template_path = 'layouts/layout.tex.erb'
end
erb_content(template)
ReVIEW::Template.generate(path: template_path, mode: '-', binding: binding, template_dir: template_dir)
end

def copy_sty(dirname, copybase, extname = 'sty')
Expand Down
5 changes: 5 additions & 0 deletions lib/review/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def self.load(filename, mode = 1)
self.new(filename, mode)
end

def self.generate(path:, binding:, mode: 1, template_dir: ReVIEW::Template::TEMPLATE_DIR)
template_file = File.expand_path(path, template_dir)
self.new(template_file, mode).result(binding)
end

def initialize(filename = nil, mode = nil)
return unless filename

Expand Down
9 changes: 3 additions & 6 deletions lib/review/webmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ def build_part(part, basetmpdir, htmlfile)

@language = @config['language']
@stylesheets = @config['stylesheet']
tmplfile = File.expand_path(template_name, ReVIEW::Template::TEMPLATE_DIR)
f.write ReVIEW::Template.load(tmplfile).result(binding)
f.write ReVIEW::Template.generate(path: template_name, binding: binding)
end
end

Expand Down Expand Up @@ -269,8 +268,7 @@ def build_indexpage(basetmpdir)
@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)
f.write ReVIEW::Template.load(tmplfile).result(binding)
f.write ReVIEW::Template.generate(path: template_name, binding: binding)
end
end

Expand All @@ -290,8 +288,7 @@ def build_titlepage(basetmpdir, htmlfile)

@language = @config['language']
@stylesheets = @config['stylesheet']
tmplfile = File.expand_path(template_name, ReVIEW::Template::TEMPLATE_DIR)
f.write ReVIEW::Template.load(tmplfile).result(binding)
f.write ReVIEW::Template.generate(path: template_name, binding: binding)
end
end

Expand Down
10 changes: 10 additions & 0 deletions test/test_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ def test_open_with_value
@name = 'test'
assert_equal("<test>\n<name>test</name>\n</test>\n", tmpl.result(binding))
end

def test_generate
result = ReVIEW::Template.generate(path: './assets/test.xml.erb', binding: binding, template_dir: __dir__)
assert_equal("<test>\n<name></name>\n</test>\n", result)
end

def test_generate_without_template_dir
result = ReVIEW::Template.generate(path: '../test/assets/test.xml.erb', binding: binding)
assert_equal("<test>\n<name></name>\n</test>\n", result)
end
end