Skip to content

Commit

Permalink
#21 小説のタイトル・作者名に、ファイル名に使えない文字が使われているとエラーになるのを修正
Browse files Browse the repository at this point in the history
  • Loading branch information
whiteleaf7 committed Feb 27, 2013
1 parent 8531225 commit 6a53bdc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
15 changes: 9 additions & 6 deletions lib/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def self.get_novel_section_save_dir(archive_path)
#
# target の種別を判別する
#
# ncodeの場合は破壊的に小文字に変更する
# ncodeの場合、targetを破壊的に変更する
#
def self.get_target_type(target)
case target
Expand Down Expand Up @@ -119,7 +119,7 @@ def self.get_novel_data_dir_by_target(target)
end
return nil unless data
id = data["id"]
path = File.join(Database.archive_root_path, data["sitename"], data["title"])
path = File.join(Database.archive_root_path, data["sitename"], data["file_title"])
if File.exists?(path)
return path
else
Expand Down Expand Up @@ -329,6 +329,7 @@ def update_database
"id" => @id,
"author" => @setting["author"],
"title" => @setting["title"],
"file_title" => @file_title,
"toc_url" => @setting["toc_url"],
"sitename" => @setting["name"],
"last_update" => Time.now
Expand Down Expand Up @@ -365,6 +366,7 @@ def get_latest_table_of_contents
end
@setting.multi_match(toc_source, "title", "author", "story", "tcode")
@title = @setting["title"]
@file_title = Helper.replace_filename_special_chars(@title)
toc_objects = {
"title" => @title,
"author" => @setting["author"],
Expand Down Expand Up @@ -411,6 +413,7 @@ def get_subtitles(toc_source)
"href" => @setting["href"],
"chapter" => @setting["chapter"],
"subtitle" => @setting["subtitle"],
"file_subtitle" => Helper.replace_filename_special_chars(@setting["subtitle"]),
"subdate" => @setting["subdate"],
"subupdate" => @setting["subupdate"]
}
Expand Down Expand Up @@ -439,12 +442,12 @@ def sections_download_and_save(subtitles)
else
sleep(interval_sleep_time) if i > 0
end
index, subtitle = subtitle_info["index"], subtitle_info["subtitle"]
index, subtitle, file_subtitle = %w(index subtitle file_subtitle).map {|k| subtitle_info[k] }
puts "第#{index}部分 #{subtitle} (#{i+1}/#{max})"
section_element = a_section_download(subtitle_info)
info = subtitle_info.dup
info["element"] = section_element
section_file_name = "#{index} #{subtitle}.yaml"
section_file_name = "#{index} #{file_subtitle}.yaml"
section_file_path = File.join(SECTION_SAVE_DIR_NAME, section_file_name)
move_to_cache_dir(section_file_path)
save_novel_data(section_file_path, info)
Expand Down Expand Up @@ -544,8 +547,8 @@ def slice_postscript(lines)
#
# 小説データの格納ディレクトリパス
def get_novel_data_dir
raise "小説名がまだ設定されていません" unless @title
File.join(Database.archive_root_path, @setting["name"], @title)
raise "小説名がまだ設定されていません" unless @file_title
File.join(Database.archive_root_path, @setting["name"], @file_title)
end

#
Expand Down
4 changes: 4 additions & 0 deletions lib/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def self.print_horizontal_rule
puts "―" * 35
end

def self.replace_filename_special_chars(str)
str.tr("/:*?\"<>|.", "/:*?”〈〉|.").gsub("\\", "¥")
end

#
# 外部コマンド実行中の待機ループの処理を書けるクラス
#
Expand Down
3 changes: 1 addition & 2 deletions lib/loadconverter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def converter(title, &block)
# buffer
# end
# end
def load_converter(archive_path)
title = File.basename(archive_path)
def load_converter(title, archive_path)
converter_path = File.join(archive_path, "converter.rb")
if File.exists?(converter_path)
if Helper.os_windows?
Expand Down
5 changes: 4 additions & 1 deletion lib/narou.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ def self.get_aozoraepub3_path
end

def self.create_novel_filename(novel_data, ext = "")
author, title = [novel_data["author"], novel_data["title"]].map { |v| v.sub(/^[  ]*(.+?)[  ]*$/, "\\1") }
author, title = %w(author title).map { |k|
# AozoraEpub3 の命名規則に沿った変換
novel_data[k].sub(/^[  ]*(.+?)[  ]*$/, "\\1").gsub(%r![/\\:*?\"<>|]!, "")
}
"[#{author}] #{title}#{ext}"
end

Expand Down
4 changes: 2 additions & 2 deletions lib/novelconverter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def initialize(setting, output_filename = nil, display_inspector = false)
end

def load_novel_section(subtitle_info)
path = File.join(@section_save_dir, "#{subtitle_info["index"]} #{subtitle_info["subtitle"]}.yaml")
path = File.join(@section_save_dir, "#{subtitle_info["index"]} #{subtitle_info["file_subtitle"]}.yaml")
YAML.load_file(path)
end

Expand Down Expand Up @@ -236,7 +236,7 @@ def convert_main(text = nil)
sections = []
@cover_chuki = create_cover_chuki

conv = load_converter(@setting.archive_path).new(@setting, @inspector, @illustration)
conv = load_converter(@novel_title, @setting.archive_path).new(@setting, @inspector, @illustration)
if text
result = conv.convert(text, "textfile")
unless @setting.enable_enchant_midashi
Expand Down

0 comments on commit 6a53bdc

Please sign in to comment.