From 64e7e00b6731a7be1dc2c5afe26011ca7250dfdd Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 23 May 2021 15:18:52 +0900 Subject: [PATCH 1/4] epub2html: scope of some variables could be narrowed --- lib/review/epub2html.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/review/epub2html.rb b/lib/review/epub2html.rb index 74e0896c9..bd0cae43c 100644 --- a/lib/review/epub2html.rb +++ b/lib/review/epub2html.rb @@ -47,35 +47,35 @@ def execute(*args) exit 1 end - parse_epub(args[0]) - puts join_html(args[1]) + htmls = parse_epub(args[0]) + puts join_html(args[1], htmls) end def initialize @opfxml = nil - @htmls = {} - @head = nil - @tail = nil @inline_footnote = nil end def parse_epub(epubname) + htmls = {} Zip::File.open(epubname) do |zio| zio.each do |entry| if /.+\.opf\Z/.match?(entry.name) opf = entry.get_input_stream.read @opfxml = REXML::Document.new(opf) elsif /.+\.x?html\Z/.match?(entry.name) - @htmls[entry.name.sub('OEBPS/', '')] = entry.get_input_stream.read.force_encoding('utf-8') + htmls[entry.name.sub('OEBPS/', '')] = entry.get_input_stream.read.force_encoding('utf-8') end end end - nil + htmls end def take_headtail(html) - @head = html.sub(/().*/m, '\1') - @tail = html.sub(%r{.*()}m, '\1') + head = html.sub(/().*/m, '\1') + tail = html.sub(%r{.*()}m, '\1') + + [head, tail] end def sanitize(s) @@ -142,16 +142,17 @@ def modify_html(fname, html) sub(%r{().*}m, '') end - def join_html(reffile) + def join_html(reffile, htmls) + head = tail = nil body = [] make_list.each do |fname| - if @head.nil? && (reffile.nil? || reffile == fname) - take_headtail(@htmls[fname]) + if head.nil? && (reffile.nil? || reffile == fname) + head, tail = take_headtail(htmls[fname]) end - body << modify_html(fname, @htmls[fname]) + body << modify_html(fname, htmls[fname]) end - "#{@head}\n#{body.join("\n")}\n#{@tail}" + "#{head}\n#{body.join("\n")}\n#{tail}" end def make_list From 8f3fd82e3ebdfc6d4c7fa2af060b5995edc0566f Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 23 May 2021 15:24:12 +0900 Subject: [PATCH 2/4] epub2html: compare with File.basename It assumes that all HTML files are in the same directory and that there are no duplicate filenames. --- lib/review/epub2html.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/review/epub2html.rb b/lib/review/epub2html.rb index bd0cae43c..3ed4e3600 100644 --- a/lib/review/epub2html.rb +++ b/lib/review/epub2html.rb @@ -64,7 +64,7 @@ def parse_epub(epubname) opf = entry.get_input_stream.read @opfxml = REXML::Document.new(opf) elsif /.+\.x?html\Z/.match?(entry.name) - htmls[entry.name.sub('OEBPS/', '')] = entry.get_input_stream.read.force_encoding('utf-8') + htmls[File.basename(entry.name)] = entry.get_input_stream.read.force_encoding('utf-8') end end end @@ -145,7 +145,8 @@ def modify_html(fname, html) def join_html(reffile, htmls) head = tail = nil body = [] - make_list.each do |fname| + make_list.each do |href_value| + fname = File.basename(href_value) if head.nil? && (reffile.nil? || reffile == fname) head, tail = take_headtail(htmls[fname]) end From dcb0c0db7b7754335c593e12921c0438ac6ad2d8 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sun, 23 May 2021 15:52:56 +0900 Subject: [PATCH 3/4] epub2html: add execute_with_params() to use without ARGV --- lib/review/epub2html.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/review/epub2html.rb b/lib/review/epub2html.rb index 3ed4e3600..3f7182e42 100644 --- a/lib/review/epub2html.rb +++ b/lib/review/epub2html.rb @@ -23,7 +23,7 @@ def self.execute(*args) new.execute(*args) end - def execute(*args) + def parse_options!(*args) opts = OptionParser.new opts.banner = < Date: Sun, 23 May 2021 15:53:30 +0900 Subject: [PATCH 4/4] epub2html: move initialize() --- lib/review/epub2html.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/review/epub2html.rb b/lib/review/epub2html.rb index 3f7182e42..4fcc41692 100644 --- a/lib/review/epub2html.rb +++ b/lib/review/epub2html.rb @@ -23,6 +23,11 @@ def self.execute(*args) new.execute(*args) end + def initialize + @opfxml = nil + @inline_footnote = nil + end + def parse_options!(*args) opts = OptionParser.new @@ -60,11 +65,6 @@ def execute_with_params(epubname, reffile) puts join_html(reffile, htmls) end - def initialize - @opfxml = nil - @inline_footnote = nil - end - def parse_epub(epubname) htmls = {} Zip::File.open(epubname) do |zio|