lib/epubmaker/epubcommon.rb in review-4.2.0 vs lib/epubmaker/epubcommon.rb in review-5.0.0

- old
+ new

@@ -8,11 +8,15 @@ # For details of the GNU LGPL, see the file "COPYING". # require 'review/i18n' require 'review/template' -require 'cgi' +begin + require 'cgi/escape' +rescue LoadError + require 'cgi/util' +end module EPUBMaker # EPUBCommon is the common class for EPUB producer. class EPUBCommon # Construct object with parameter hash +config+ and message resource hash +res+. @@ -20,10 +24,14 @@ @body_ext = '' @producer = producer @body_ext = nil end + def h(str) + CGI.escapeHTML(str) + end + # Return mimetype content. def mimetype 'application/epub+zip' end @@ -57,24 +65,24 @@ end def ncx_doctitle <<EOT <docTitle> - <text>#{CGI.escapeHTML(@producer.config['title'])}</text> + <text>#{h(@producer.config['title'])}</text> </docTitle> <docAuthor> - <text>#{@producer.config['aut'].nil? ? '' : CGI.escapeHTML(join_with_separator(@producer.config['aut'], ReVIEW::I18n.t('names_splitter')))}</text> + <text>#{@producer.config['aut'].nil? ? '' : h(join_with_separator(@producer.config['aut'], ReVIEW::I18n.t('names_splitter')))}</text> </docAuthor> EOT end def ncx_navmap(indentarray) s = <<EOT <navMap> <navPoint id="top" playOrder="1"> <navLabel> - <text>#{CGI.escapeHTML(@producer.config['title'])}</text> + <text>#{h(@producer.config['title'])}</text> </navLabel> <content src="#{@producer.config['cover']}"/> </navPoint> EOT @@ -82,11 +90,11 @@ unless @producer.config['mytoc'].nil? s << <<EOT <navPoint id="toc" playOrder="#{nav_count}"> <navLabel> - <text>#{CGI.escapeHTML(@producer.res.v('toctitle'))}</text> + <text>#{h(@producer.res.v('toctitle'))}</text> </navLabel> <content src="#{@producer.config['bookname']}-toc.#{@producer.config['htmlext']}"/> </navPoint> EOT nav_count += 1 @@ -98,11 +106,11 @@ level = item.level.nil? ? 0 : (item.level - 1) level = indent.size - 1 if level >= indent.size s << <<EOT <navPoint id="nav-#{nav_count}" playOrder="#{nav_count}"> <navLabel> - <text>#{indent[level]}#{CGI.escapeHTML(item.title)}</text> + <text>#{indent[level]}#{h(item.title)}</text> </navLabel> <content src="#{item.file}"/> </navPoint> EOT nav_count += 1 @@ -129,25 +137,25 @@ if @producer.config['coverimage'] file = @producer.coverimage raise "coverimage #{@producer.config['coverimage']} not found. Abort." unless file @body = <<-EOT <div id="cover-image" class="cover-image"> - <img src="#{file}" alt="#{CGI.escapeHTML(@producer.config.name_of('title'))}" class="max"/> + <img src="#{file}" alt="#{h(@producer.config.name_of('title'))}" class="max"/> </div> EOT else @body = <<-EOT -<h1 class="cover-title">#{CGI.escapeHTML(@producer.config.name_of('title'))}</h1> +<h1 class="cover-title">#{h(@producer.config.name_of('title'))}</h1> EOT if @producer.config['subtitle'] @body << <<-EOT -<h2 class="cover-subtitle">#{CGI.escapeHTML(@producer.config.name_of('subtitle'))}</h2> +<h2 class="cover-subtitle">#{h(@producer.config.name_of('subtitle'))}</h2> EOT end end - @title = CGI.escapeHTML(@producer.config.name_of('title')) + @title = h(@producer.config.name_of('title')) @language = @producer.config['language'] @stylesheets = @producer.config['stylesheet'] tmplfile = if @producer.config['htmlversion'].to_i == 5 File.expand_path('./html/layout-html5.html.erb', ReVIEW::Template::TEMPLATE_DIR) else @@ -159,29 +167,29 @@ # Return title (copying) content. # NOTE: this method is not used yet. # see lib/review/epubmaker.rb#build_titlepage def titlepage - @title = CGI.escapeHTML(@producer.config.name_of('title')) + @title = h(@producer.config.name_of('title')) @body = <<EOT <h1 class="tp-title">#{@title}</h1> EOT if @producer.config['subtitle'] @body << <<EOT - <h2 class="tp-subtitle">#{CGI.escapeHTML(@producer.config.name_of('subtitle'))}</h2> + <h2 class="tp-subtitle">#{h(@producer.config.name_of('subtitle'))}</h2> EOT end if @producer.config['aut'] @body << <<EOT <p> <br /> <br /> </p> - <h2 class="tp-author">#{CGI.escapeHTML(join_with_separator(@producer.config.names_of('aut'), ReVIEW::I18n.t('names_splitter')))}</h2> + <h2 class="tp-author">#{h(join_with_separator(@producer.config.names_of('aut'), ReVIEW::I18n.t('names_splitter')))}</h2> EOT end publisher = @producer.config.names_of('pbl') if publisher @@ -190,11 +198,11 @@ <br /> <br /> <br /> <br /> </p> - <h3 class="tp-publisher">#{CGI.escapeHTML(join_with_separator(publisher, ReVIEW::I18n.t('names_splitter')))}</h3> + <h3 class="tp-publisher">#{h(join_with_separator(publisher, ReVIEW::I18n.t('names_splitter')))}</h3> EOT end @language = @producer.config['language'] @stylesheets = @producer.config['stylesheet'] @@ -207,40 +215,40 @@ tmpl.result(binding) end # Return colophon content. def colophon - @title = CGI.escapeHTML(@producer.res.v('colophontitle')) + @title = h(@producer.res.v('colophontitle')) @body = <<EOT <div class="colophon"> EOT if @producer.config['subtitle'].nil? @body << <<EOT - <p class="title">#{CGI.escapeHTML(@producer.config.name_of('title'))}</p> + <p class="title">#{h(@producer.config.name_of('title'))}</p> EOT else @body << <<EOT - <p class="title">#{CGI.escapeHTML(@producer.config.name_of('title'))}<br /><span class="subtitle">#{CGI.escapeHTML(@producer.config.name_of('subtitle'))}</span></p> + <p class="title">#{h(@producer.config.name_of('title'))}<br /><span class="subtitle">#{h(@producer.config.name_of('subtitle'))}</span></p> EOT end @body << colophon_history if @producer.config['date'] || @producer.config['history'] @body << %Q( <table class="colophon">\n) @body << @producer.config['colophon_order'].map do |role| if @producer.config[role] - %Q( <tr><th>#{CGI.escapeHTML(@producer.res.v(role))}</th><td>#{CGI.escapeHTML(join_with_separator(@producer.config.names_of(role), ReVIEW::I18n.t('names_splitter')))}</td></tr>\n) + %Q( <tr><th>#{h(@producer.res.v(role))}</th><td>#{h(join_with_separator(@producer.config.names_of(role), ReVIEW::I18n.t('names_splitter')))}</td></tr>\n) else '' end end.join @body << %Q( <tr><th>ISBN</th><td>#{@producer.isbn_hyphen}</td></tr>\n) if @producer.isbn_hyphen @body << %Q( </table>\n) if @producer.config['rights'] && !@producer.config['rights'].empty? - @body << %Q( <p class="copyright">#{join_with_separator(@producer.config.names_of('rights').map { |m| CGI.escapeHTML(m) }, '<br />')}</p>\n) + @body << %Q( <p class="copyright">#{join_with_separator(@producer.config.names_of('rights').map { |m| h(m) }, '<br />')}</p>\n) end @body << %Q( </div>\n) @language = @producer.config['language'] @stylesheets = @producer.config['stylesheet'] @@ -259,15 +267,15 @@ if @producer.config['history'] @producer.config['history'].each_with_index do |items, edit| items.each_with_index do |item, rev| editstr = edit == 0 ? ReVIEW::I18n.t('first_edition') : ReVIEW::I18n.t('nth_edition', (edit + 1).to_s) revstr = ReVIEW::I18n.t('nth_impression', (rev + 1).to_s) - if item =~ /\A\d+\-\d+\-\d+\Z/ + if item =~ /\A\d+-\d+-\d+\Z/ buf << %Q( <p>#{ReVIEW::I18n.t('published_by1', [date_to_s(item), editstr + revstr])}</p>\n) - elsif item =~ /\A(\d+\-\d+\-\d+)[\s ](.+)/ + elsif item =~ /\A(\d+-\d+-\d+)[\s ](.+)/ # custom date with string - item.match(/\A(\d+\-\d+\-\d+)[\s ](.+)/) do |m| + item.match(/\A(\d+-\d+-\d+)[\s ](.+)/) do |m| buf << %Q( <p>#{ReVIEW::I18n.t('published_by3', [date_to_s(m[1]), m[2]])}</p>\n) end else # free format buf << %Q( <p>#{item}</p>\n) @@ -287,13 +295,13 @@ d.strftime(ReVIEW::I18n.t('date_format')) end # Return own toc content. def mytoc - @title = CGI.escapeHTML(@producer.res.v('toctitle')) + @title = h(@producer.res.v('toctitle')) - @body = %Q( <h1 class="toc-title">#{CGI.escapeHTML(@producer.res.v('toctitle'))}</h1>\n) + @body = %Q( <h1 class="toc-title">#{h(@producer.res.v('toctitle'))}</h1>\n) if @producer.config['epubmaker']['flattoc'].nil? @body << hierarchy_ncx('ul') else @body << flat_ncx('ul', @producer.config['epubmaker']['flattocindent']) end @@ -383,10 +391,10 @@ def flat_ncx(type, indent = nil) s = %Q(<#{type} class="toc-h1">\n) @producer.contents.each do |item| next if !item.notoc.nil? || item.level.nil? || item.file.nil? || item.title.nil? || item.level > @producer.config['toclevel'].to_i is = indent == true ? ' ' * item.level : '' - s << %Q(<li><a href="#{item.file}">#{is}#{CGI.escapeHTML(item.title)}</a></li>\n) + s << %Q(<li><a href="#{item.file}">#{is}#{h(item.title)}</a></li>\n) end s << %Q(</#{type}>\n) s end