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

refactoring, escaping and i18ning. #533

Merged
merged 1 commit into from
Apr 10, 2016
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
24 changes: 12 additions & 12 deletions lib/epubmaker/epubcommon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def ncx_doctitle
<text>#{CGI.escapeHTML(@producer.params["title"])}</text>
</docTitle>
<docAuthor>
<text>#{@producer.params["aut"].nil? ? "" : CGI.escapeHTML(join_with_separator(@producer.params["aut"], ", "))}</text>
<text>#{@producer.params["aut"].nil? ? "" : CGI.escapeHTML(join_with_separator(@producer.params["aut"], ReVIEW::I18n.t("names_splitter")))}</text>
</docAuthor>
EOT
end
Expand All @@ -86,7 +86,7 @@ def ncx_navmap(indentarray)
s << <<EOT
<navPoint id="toc" playOrder="#{nav_count}">
<navLabel>
<text>#{@producer.res.v("toctitle")}</text>
<text>#{CGI.escapeHTML(@producer.res.v("toctitle"))}</text>
</navLabel>
<content src="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}"/>
</navPoint>
Expand Down Expand Up @@ -144,7 +144,7 @@ def cover(type=nil)
EOT
end

@title = @producer.params['title']
@title = CGI.escapeHTML(@producer.params['title'])
@language = @producer.params['language']
@stylesheets = @producer.params["stylesheet"]
if @producer.params["htmlversion"].to_i == 5
Expand All @@ -158,18 +158,18 @@ def cover(type=nil)

# Return title (copying) content.
def titlepage
@title = @producer.params["title"]
@title = CGI.escapeHTML(@producer.params["title"])

@body = <<EOT
<h1 class="tp-title">#{CGI.escapeHTML(@title)}</h1>
<h1 class="tp-title">#{@title}</h1>
EOT
if @producer.params["aut"]
@body << <<EOT
<p>
<br />
<br />
</p>
<h2 class="tp-author">#{CGI.escapeHTML(join_with_separator(@producer.params["aut"], ", "))}</h2>
<h2 class="tp-author">#{CGI.escapeHTML(join_with_separator(@producer.params["aut"], ReVIEW::I18n.t("names_splitter")))}</h2>
EOT
end

Expand All @@ -182,7 +182,7 @@ def titlepage
<br />
<br />
</p>
<h3 class="tp-publisher">#{CGI.escapeHTML(join_with_separator(publisher, ", "))}</h3>
<h3 class="tp-publisher">#{CGI.escapeHTML(join_with_separator(publisher, ReVIEW::I18n.t("names_splitter")))}</h3>
EOT
end

Expand All @@ -199,7 +199,7 @@ def titlepage

# Return colophon content.
def colophon
@title = @producer.res.v("colophontitle")
@title = CGI.escapeHTML(@producer.res.v("colophontitle"))
@body = <<EOT
<div class="colophon">
EOT
Expand Down Expand Up @@ -240,7 +240,7 @@ def colophon
@body << %Q[ <table class="colophon">\n]
@body << @producer.params["colophon_order"].map{ |role|
if @producer.params[role]
%Q[ <tr><th>#{@producer.res.v(role)}</th><td>#{CGI.escapeHTML(join_with_separator(@producer.params[role], ", "))}</td></tr>\n]
%Q[ <tr><th>#{CGI.escapeHTML(@producer.res.v(role))}</th><td>#{CGI.escapeHTML(join_with_separator(@producer.params[role], ReVIEW::I18n.t("names_splitter")))}</td></tr>\n]
else
""
end
Expand All @@ -251,7 +251,7 @@ def colophon
end
@body << %Q[ </table>\n]
if !@producer.params["rights"].nil? && @producer.params["rights"].size > 0
@body << %Q[ <p class="copyright">#{join_with_separator(@producer.params["rights"], "<br />")}</p>]
@body << %Q[ <p class="copyright">#{join_with_separator(@producer.params["rights"].map {|m| CGI.escapeHTML(m)}, "<br />")}</p>\n]
end
@body << %Q[ </div>\n]

Expand All @@ -274,9 +274,9 @@ def date_to_s(date)

# Return own toc content.
def mytoc
@title = @producer.res.v("toctitle")
@title = CGI.escapeHTML(@producer.res.v("toctitle"))

@body = %Q[ <h1 class="toc-title">#{@producer.res.v("toctitle")}</h1>\n]
@body = %Q[ <h1 class="toc-title">#{CGI.escapeHTML(@producer.res.v("toctitle"))}</h1>\n]
if @producer.params["epubmaker"]["flattoc"].nil?
@body << hierarchy_ncx("ul")
else
Expand Down
10 changes: 5 additions & 5 deletions lib/epubmaker/epubv2.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8
# = epubv2.rb -- EPUB version 2 producer.
#
# Copyright (c) 2010-2015 Kenshi Muto and Masayoshi Takahashi
# Copyright (c) 2010-2016 Kenshi Muto and Masayoshi Takahashi
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -36,8 +36,8 @@ def opf
def opf_metainfo
s = ""
%w[title language date type format source description relation coverage subject rights].each do |item|
next if @producer.params[item].nil?
if @producer.params[item].instance_of?(Array)
next unless @producer.params[item]
if @producer.params[item].kind_of?(Array)
s << @producer.params[item].map {|i| %Q[ <dc:#{item}>#{CGI.escapeHTML(i.to_s)}</dc:#{item}>\n]}.join
else
s << %Q[ <dc:#{item}>#{CGI.escapeHTML(@producer.params[item].to_s)}</dc:#{item}>\n]
Expand All @@ -53,15 +53,15 @@ def opf_metainfo

# creator (should be array)
%w[aut a-adp a-ann a-arr a-art a-asn a-aqt a-aft a-aui a-ant a-bkp a-clb a-cmm a-dsr a-edt a-ill a-lyr a-mdc a-mus a-nrt a-oth a-pht a-prt a-red a-rev a-spn a-ths a-trc a-trl].each do |role|
next if @producer.params[role].nil?
next unless @producer.params[role]
@producer.params[role].each do |v|
s << %Q[ <dc:creator opf:role="#{role.sub('a-', '')}">#{CGI.escapeHTML(v)}</dc:creator>\n]
end
end

# contributor (should be array)
%w[adp ann arr art asn aqt aft aui ant bkp clb cmm dsr edt ill lyr mdc mus nrt oth pht prt red rev spn ths trc trl].each do |role|
next if @producer.params[role].nil?
next unless @producer.params[role]
@producer.params[role].each do |v|
s << %Q[ <dc:contributor opf:role="#{role}">#{CGI.escapeHTML(v)}</dc:contributor>\n]
if role == "prt"
Expand Down
22 changes: 11 additions & 11 deletions lib/epubmaker/epubv3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def opf
def opf_metainfo
s = ""
%w[title language date type format source description relation coverage subject rights].each do |item|
next if @producer.params[item].nil?
if @producer.params[item].instance_of?(Array)
next unless @producer.params[item]
if @producer.params[item].kind_of?(Array)
@producer.params[item].each_with_index do |v, i|
if v.instance_of?(Hash)
if v.kind_of?(Hash)
s << %Q[ <dc:#{item} id="#{item}-#{i}">#{CGI.escapeHTML(v["name"])}</dc:#{item}>\n]
v.each_pair do |name, val|
next if name == "name"
Expand All @@ -59,7 +59,7 @@ def opf_metainfo
s << %Q[ <dc:#{item} id="#{item}-#{i}">#{CGI.escapeHTML(v.to_s)}</dc:#{item}>\n]
end
end
elsif @producer.params[item].instance_of?(Hash)
elsif @producer.params[item].kind_of?(Hash)
s << %Q[ <dc:#{item} id="#{item}">#{CGI.escapeHTML(@producer.params[item]["name"])}</dc:#{item}>\n]
@producer.params[item].each_pair do |name, val|
next if name == "name"
Expand All @@ -81,9 +81,9 @@ def opf_metainfo

# creator (should be array)
%w[a-adp a-ann a-arr a-art a-asn a-aqt a-aft a-aui a-ant a-bkp a-clb a-cmm a-csl a-dsr a-edt a-ill a-lyr a-mdc a-mus a-nrt a-oth a-pht a-prt a-red a-rev a-spn a-ths a-trc a-trl aut].each do |role|
next if @producer.params[role].nil?
next unless @producer.params[role]
@producer.params[role].each_with_index do |v, i|
if v.instance_of?(Hash)
if v.kind_of?(Hash)
s << %Q[ <dc:creator id="#{role}-#{i}">#{CGI.escapeHTML(v["name"])}</dc:creator>\n]
s << %Q[ <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role.sub('a-', '')}</meta>\n]
v.each_pair do |name, val|
Expand All @@ -99,9 +99,9 @@ def opf_metainfo

# contributor (should be array)
%w[adp ann arr art asn aqt aft aui ant bkp clb cmm csl dsr edt ill lyr mdc mus nrt oth pbd pbl pht prt red rev spn ths trc trl].each do |role|
next if @producer.params[role].nil?
next unless @producer.params[role]
@producer.params[role].each_with_index do |v, i|
if v.instance_of?(Hash)
if v.kind_of?(Hash)
s << %Q[ <dc:contributor id="#{role}-#{i}">#{CGI.escapeHTML(v["name"])}</dc:contributor>\n]
s << %Q[ <meta refines="##{role}-#{i}" property="role" scheme="marc:relators">#{role}</meta>\n]
v.each_pair do |name, val|
Expand All @@ -114,7 +114,7 @@ def opf_metainfo
end

if role == "prt" || role == "pbl"
if v.instance_of?(Hash)
if v.kind_of?(Hash)
s << %Q[ <dc:publisher id="pub-#{role}-#{i}">#{CGI.escapeHTML(v["name"])}</dc:publisher>\n]
s << %Q[ <meta refines="#pub-#{role}-#{i}" property="role" scheme="marc:relators">#{role}</meta>\n]
v.each_pair do |name, val|
Expand Down Expand Up @@ -204,11 +204,11 @@ def ncx(indentarray)

@body = <<EOT
<nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
<h1 class="toc-title">#{@producer.res.v("toctitle")}</h1>
<h1 class="toc-title">#{CGI.escapeHTML(@producer.res.v("toctitle"))}</h1>
#{ncx_main} </nav>
EOT

@title = @producer.res.v("toctitle")
@title = CGI.escapeHTML(@producer.res.v("toctitle"))
@language = @producer.params['language']
@stylesheets = @producer.params["stylesheet"]
tmplfile = File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR)
Expand Down
25 changes: 8 additions & 17 deletions lib/epubmaker/producer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ def initialize(params=nil, version=nil)
@params["epubversion"] = version unless version.nil?
@res = ReVIEW::I18n

unless params.nil?
if params
merge_params(params)
end
end

def coverimage
if !params["coverimage"]
return nil
end
return nil unless params["coverimage"]
@contents.each do |item|
if item.media.start_with?('image') && item.file =~ /#{params["coverimage"]}\Z/ # /
return item.file
Expand All @@ -71,7 +69,7 @@ def coverimage

# Update parameters by merging from new parameter hash +params+.
def merge_params(params)
@params = @params.merge(params)
@params = @params.deep_merge(params)
complement

unless @params["epubversion"].nil?
Expand Down Expand Up @@ -214,7 +212,6 @@ def complement
@params["htmlext"] = "html" if @params["htmlext"].nil?
defaults = {
"cover" => "#{@params["bookname"]}.#{@params["htmlext"]}",
"title" => @params["booktitle"],
"language" => "ja",
"date" => Time.now.strftime("%Y-%m-%d"),
"modified" => Time.now.strftime("%Y-%02m-%02dT%02H:%02M:%02SZ"),
Expand Down Expand Up @@ -259,15 +256,8 @@ def complement
"font_ext" => %w(ttf woff otf),
}

defaults.each_pair do |k, v|
if k == "epubmaker" && !@params[k].nil?
v.each_pair do |k2, v2|
@params[k][k2] = v2 if @params[k][k2].nil?
end
else
@params[k] = v if @params[k].nil?
end
end
@params = defaults.deep_merge(@params)
@params["title"] = @params["booktitle"] unless @params["title"]

deprecated_parameters = {
"ncxindent" => "epubmaker:ncxindent",
Expand Down Expand Up @@ -300,11 +290,12 @@ def complement
@params["htmlversion"] = 5 if @params["epubversion"] >= 3

%w[bookname title].each do |k|
raise "Key #{k} must have a value. Abort." if @params[k].nil?
raise "Key #{k} must have a value. Abort." unless @params[k]
end
# array
%w[subject aut a-adp a-ann a-arr a-art a-asn a-aqt a-aft a-aui a-ant a-bkp a-clb a-cmm a-dsr a-edt a-ill a-lyr a-mdc a-mus a-nrt a-oth a-pht a-prt a-red a-rev a-spn a-ths a-trc a-trl adp ann arr art asn aut aqt aft aui ant bkp clb cmm dsr edt ill lyr mdc mus nrt oth pht pbl prt red rev spn ths trc trl stylesheet rights].each do |item|
@params[item] = [@params[item]] if !@params[item].nil? && @params[item].instance_of?(String)
next unless @params[item]
@params[item] = [@params[item]] if @params[item].kind_of?(String)
end
# optional
# type, format, identifier, source, relation, coverpage, aut
Expand Down
25 changes: 8 additions & 17 deletions lib/review/epubmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ def build_part(part, basetmpdir, htmlfile)
File.open("#{basetmpdir}/#{htmlfile}", "w") do |f|
@body = ""
@body << "<div class=\"part\">\n"
@body << "<h1 class=\"part-number\">#{ReVIEW::I18n.t("part", part.number)}</h1>\n"
@body << "<h1 class=\"part-number\">#{CGI.escapeHTML(ReVIEW::I18n.t("part", part.number))}</h1>\n"
if part.name.strip.present?
@body << "<h2 class=\"part-title\">#{part.name.strip}</h2>\n"
@body << "<h2 class=\"part-title\">#{CGI.escapeHTML(part.name.strip)}</h2>\n"
end
@body << "</div>\n"

Expand Down Expand Up @@ -287,16 +287,6 @@ def build_chap(chap, base_path, basetmpdir, ispart)
write_buildlogtxt(basetmpdir, htmlfile, filename)
log("Create #{htmlfile} from #{filename}.")

# TODO: It would be nice if we can modify level in PART, PREDEF, or POSTDEF.
# But we have to care about section number reference (@<hd>) also.
#
# if !ispart.nil?
# level = @params["part_secnolevel"]
# else
# level = @params["pre_secnolevel"] if chap.on_PREDEF?
# level = @params["post_secnolevel"] if chap.on_APPENDIX?
# end

if @params["params"].present?
warn "'params:' in config.yml is obsoleted."
if @params["params"] =~ /stylesheet=/
Expand Down Expand Up @@ -398,15 +388,16 @@ def copy_frontmatter(basetmpdir)
end

def build_titlepage(basetmpdir, htmlfile)
# TODO: should be created via epubcommon
File.open("#{basetmpdir}/#{htmlfile}", "w") do |f|
@body = ""
@body << "<div class=\"titlepage\">"
@body << "<h1 class=\"tp-title\">#{CGI.escapeHTML(@params["booktitle"])}</h1>"
@body << "<div class=\"titlepage\">\n"
@body << "<h1 class=\"tp-title\">#{CGI.escapeHTML(@params["booktitle"])}</h1>\n"
if @params["aut"]
@body << "<h2 class=\"tp-author\">#{@params["aut"].join(", ")}</h2>"
@body << "<h2 class=\"tp-author\">#{CGI.escapeHTML(@params["aut"].join(ReVIEW::I18n.t("names_splitter")))}</h2>\n"
end
if @params["prt"]
@body << "<h3 class=\"tp-publisher\">#{@params["prt"].join(", ")}</h3>"
@body << "<h3 class=\"tp-publisher\">#{CGI.escapeHTML(@params["prt"].join(ReVIEW::I18n.t("names_splitter")))}</h3>\n"
end
@body << "</div>"

Expand All @@ -430,7 +421,7 @@ def copy_backmatter(basetmpdir)
end

if @params["colophon"]
if @params["colophon"].instance_of?(String) # FIXME:このやり方はやめる?
if @params["colophon"].kind_of?(String) # FIXME:このやり方はやめる?
FileUtils.cp(@params["colophon"], "#{basetmpdir}/colophon.#{@params["htmlext"]}")
else
File.open("#{basetmpdir}/colophon.#{@params["htmlext"]}", "w") {|f| @producer.colophon(f) }
Expand Down
Loading