lib/helper.rb in narou-2.9.5 vs lib/helper.rb in narou-3.0.0
- old
+ new
@@ -97,10 +97,16 @@
puts "―" * 35
end
def replace_filename_special_chars(str, invalid_replace = false)
result = str.tr("/:*?\"<>|.", "/:*?”〈〉|.").gsub("\\", "¥").gsub("\t", "").gsub("\n", "")
+ if Inventory.load("local_setting")["normalize-filename"]
+ begin
+ result.unicode_normalize!
+ rescue Encoding::CompatibilityError
+ end
+ end
if invalid_replace
org_encoding = result.encoding
result = result.encode(Encoding::Windows_31J, invalid: :replace, undef: :replace, replace: "_")
.encode(org_encoding)
end
@@ -109,20 +115,28 @@
#
# ダウンロードした文字列をエンコード及び不正な文字列除去、改行コード統一
#
def pretreatment_source(src, encoding = Encoding::UTF_8)
+ encoding_class = Encoding.find(encoding)
src.force_encoding(encoding)
+ .tap do |this|
+ if encoding_class != Encoding::UTF_8
+ this.encode!(Encoding::UTF_8, invalid: :replace, undef: :replace)
+ end
+ end
.scrub("?")
.gsub("\r", "")
+ .gsub(/&#x([0-9a-f]+);/i) { [$1.hex].pack("U") }
+ .gsub(/&#(\d+);/) { [$1.to_i].pack("U") }
end
ENTITIES = { quot: '"', amp: "&", nbsp: " ", lt: "<", gt: ">", copy: "(c)", "#39" => "'" }
#
# エンティティ復号
#
- def restor_entity(str)
+ def restore_entity(str)
result = str.dup
ENTITIES.each do |key, value|
result.gsub!("&#{key};", value)
end
result
@@ -249,27 +263,36 @@
# ファイルを指定したディレクトリにまとめてコピーする
# 指定したディレクトリが存在しなければ作成する
#
# from: ファイルパスをまとめた Array
# dest_dir: コピー先のディレクトリ
+ # check_timestamp: タイムスタンプを比較して新しければコピーする
#
- def copy_files(from, dest_dir)
+ def copy_files(from, dest_dir, check_timestamp: true)
from.each do |path|
basename = File.basename(path)
dirname = File.basename(File.dirname(path))
save_dir = File.join(dest_dir, dirname)
unless File.directory?(save_dir)
FileUtils.mkdir_p(save_dir)
end
- FileUtils.copy(path, File.join(save_dir, basename))
+ dest = File.join(save_dir, basename)
+ if check_timestamp && File.exist?(dest)
+ src_mtime = File.mtime(path)
+ dest_mtime = File.mtime(dest)
+ next if dest_mtime >= src_mtime
+ end
+ FileUtils.copy(path, dest)
end
end
#
# 日付形式の文字列をTime型に変換する
#
def date_string_to_time(date)
date ? Time.parse(date.sub(/[\((].+?[\))]/, "").tr("年月日時分秒@;", "///::: :")) : nil
+ rescue ArgumentError
+ nil
end
#
# 指定のファイルが前回のチェック時より新しいかどうか
#