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

Web上にある青空文庫ファイル・zipファイルを直接サポート #8

Merged
merged 2 commits into from
Nov 3, 2015
Merged
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
39 changes: 28 additions & 11 deletions bin/aozora2html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'aozora2html'
require 'optparse'
require "tempfile"

# override Aozora2Html#push_chars
#
Expand Down Expand Up @@ -46,19 +47,35 @@ if ARGV.size != 2
exit 1
end

if !File.exists?(ARGV[0])
$stderr.print "file not found: #{ARGV[0]}\n"
exit 1
end
src_file, dest_file = ARGV[0], ARGV[1]

Dir.mktmpdir do |dir|
if src_file =~ /\Ahttps?:/
require 'open-uri'
down_file = File.join(dir, File.basename(src_file))
begin
open(down_file, "wb") do |f0|
open(src_file){|f1| f0.write(f1.read)}
end
src_file = down_file
rescue
$stderr.print "file not found: #{src_file}\n"
$stderr.print "Download Error: #{$!}\n"
exit 1
end
else
if !File.exists?(src_file)
$stderr.print "file not found: #{src_file}\n"
exit 1
end
end

if File.extname(ARGV[0]) == ".zip"
require "tempfile"
Dir.mktmpdir do |dir|
if File.extname(src_file) == ".zip"
tmpfile = File.join(dir, "aozora.txt")
Aozora2Html::Zip.unzip(ARGV[0], tmpfile)
Aozora2Html.new(tmpfile, ARGV[1]).process
Aozora2Html::Zip.unzip(src_file, tmpfile)
Aozora2Html.new(tmpfile, dest_file).process
else
Aozora2Html.new(src_file, dest_file).process
end
else
Aozora2Html.new(ARGV[0], ARGV[1]).process
end