#! /usr/bin/env ruby # coding: utf-8 # OPTIONS # # -s, -k, -l=2 # x x x : 月火水 -> 月/月火水 # o x x : 月火水 -> 月/火水 # x o x : 月火水 -> け/月火水 # o o x : 月火水 -> 実行不可 # x x o : 月火水 -> 月火/月火水 # o x o : 月火水 -> 月火/水 # x o o : 月火水 -> げつ/月火水 # o o o : 月火水 -> 実行不可 ## for --unite option UNITE_CHARS = { #### English ## uppercase "A" => "a", "B" => "b", "C" => "c", "D" => "d", "E" => "e", "F" => "f", "G" => "g", "H" => "h", "I" => "i", "J" => "j", "K" => "k", "L" => "l", "M" => "m", "N" => "n", "O" => "o", "P" => "p", "Q" => "q", "R" => "r", "S" => "s", "T" => "t", "U" => "u", "V" => "v", "W" => "w", "X" => "x", "Y" => "y", "Z" => "z", ## lowercase "a" => "a", "b" => "b", "c" => "c", "d" => "d", "e" => "e", "f" => "f", "g" => "g", "h" => "h", "i" => "i", "j" => "j", "k" => "k", "l" => "l", "m" => "m", "n" => "n", "o" => "o", "p" => "p", "q" => "q", "r" => "r", "s" => "s", "t" => "t", "u" => "u", "v" => "v", "w" => "w", "x" => "x", "y" => "y", "z" => "z", #### ひらがな ## 清音 "あ"=>"あ", "い"=>"い", "う"=>"う", "え"=>"え", "お"=>"お", "か"=>"か", "き"=>"き", "く"=>"く", "け"=>"け", "こ"=>"こ", "さ"=>"さ", "し"=>"し", "す"=>"す", "せ"=>"せ", "そ"=>"そ", "た"=>"た", "ち"=>"ち", "つ"=>"つ", "て"=>"て", "と"=>"と", "な"=>"な", "に"=>"に", "ぬ"=>"ぬ", "ね"=>"ね", "の"=>"の", "は"=>"は", "ひ"=>"ひ", "ふ"=>"ふ", "へ"=>"へ", "ほ"=>"ほ", "ま"=>"ま", "み"=>"み", "む"=>"む", "め"=>"め", "も"=>"も", "や"=>"や", "ゆ"=>"ゆ", "よ"=>"よ", "ら"=>"ら", "り"=>"り", "る"=>"る", "れ"=>"れ", "ろ"=>"ろ", "わ"=>"わ", "ゐ"=>"ゐ", "ゑ"=>"ゑ", "を"=>"を", "ん"=>"ん", ## 濁音・半濁音 "が"=>"か", "ぎ"=>"き", "ぐ"=>"く", "げ"=>"け", "ご"=>"こ", "ざ"=>"さ", "じ"=>"し", "ず"=>"す", "ぜ"=>"せ", "ぞ"=>"そ", "だ"=>"た", "ぢ"=>"ち", "づ"=>"つ", "で"=>"て", "ど"=>"と", "ば"=>"は", "び"=>"ひ", "ぶ"=>"ふ", "べ"=>"へ", "ぼ"=>"ほ", "ぱ"=>"は", "ぴ"=>"ひ", "ぷ"=>"ふ", "ぺ"=>"へ", "ぽ"=>"ほ", ## 小字 "ぁ"=>"あ", "ぃ"=>"い", "ぅ"=>"う", "ぇ"=>"え", "ぉ"=>"お", "っ"=>"つ", "ゃ"=>"や", "ゅ"=>"ゆ", "ょ"=>"よ", "ゎ"=>"わ", #### カタカナ ## 清音 "ア"=>"あ", "イ"=>"い", "ウ"=>"う", "エ"=>"え", "オ"=>"お", "カ"=>"か", "キ"=>"き", "ク"=>"く", "ケ"=>"け", "コ"=>"こ", "サ"=>"さ", "シ"=>"し", "ス"=>"す", "セ"=>"せ", "ソ"=>"そ", "タ"=>"た", "チ"=>"ち", "ツ"=>"つ", "テ"=>"て", "ト"=>"と", "ナ"=>"な", "ニ"=>"に", "ヌ"=>"ぬ", "ネ"=>"ね", "ノ"=>"の", "ハ"=>"は", "ヒ"=>"ひ", "フ"=>"ふ", "ヘ"=>"へ", "ホ"=>"ほ", "マ"=>"ま", "ミ"=>"み", "ム"=>"む", "メ"=>"め", "モ"=>"も", "ヤ"=>"や", "ユ"=>"ゆ", "ヨ"=>"よ", "ラ"=>"ら", "リ"=>"り", "ル"=>"る", "レ"=>"れ", "ロ"=>"ろ", "ワ"=>"わ", "ヰ"=>"ゐ", "ヱ"=>"ゑ", "ヲ"=>"を", "ン"=>"ん", ## 濁音・半濁音 "ガ"=>"か", "ギ"=>"き", "グ"=>"く", "ゲ"=>"け", "ゴ"=>"こ", "ザ"=>"さ", "ジ"=>"し", "ズ"=>"す", "ゼ"=>"せ", "ゾ"=>"そ", "ダ"=>"た", "ヂ"=>"ち", "ヅ"=>"つ", "デ"=>"て", "ド"=>"と", "バ"=>"は", "ビ"=>"ひ", "ブ"=>"ふ", "ベ"=>"へ", "ボ"=>"ほ", "パ"=>"は", "ピ"=>"ひ", "プ"=>"ふ", "ペ"=>"へ", "ポ"=>"ほ", "ヴ"=>"う", ## 小字 "ァ"=>"あ", "ィ"=>"い", "ゥ"=>"う", "ェ"=>"え", "ォ"=>"お", "ッ"=>"つ", "ャ"=>"や", "ュ"=>"ゆ", "ョ"=>"よ", "ヮ"=>"わ", ## 記号 ##"ヵ"=>"か", "ヶ"=>"け", } require "pp" gem "builtinextension" require "string/escapezsh.rb" require "filerenamer.rb" OPTIONS = {} op = FileRenamer::OptionParser.new op.on("-k" , "--kakasi" , "Japanese yomi dirname. (Using kakasi)"){ OPTIONS[:kakasi] = true} op.on("-u" , "--unite" , "Unite group of chars. E.g., t -> T."){ OPTIONS[:unite] = true} op.on("-l number", "--length=number", "Indicate length of initial characters. Default number = 1."){|v| OPTIONS[:length] = v.to_i} op.on("-a number", "--auto-length=number", "Automatically decided length for each filename. 'number' is identificatio length."){|v| OPTIONS[:auto] = v.to_i} op.on("-s" , "--shorten", "Shorten filename, duplicated part with the dirname."){ OPTIONS[:shorten] = true} op.on("-4" , "--2x2", "Classify to 2layer-directories of 2chars. Implying 'shorten'."){ OPTIONS[:two_two] = true} op.parse!(ARGV) OPTIONS[:length] ||= 1 if OPTIONS[:shorten] && OPTIONS[:kakasi] puts "Options -s and -k cannot be used at the same time." exit end fr = FileRenamer::Commander.new(op.options, ARGV) fr.execute do |filename| next if filename.size <= OPTIONS[:length] if OPTIONS[:kakasi] dirname = `echo #{filename.escape_zsh} | nkf -e | kakasi -JH | nkf -w` else dirname = filename end dirname = dirname[0..(OPTIONS[:length]-1)] if OPTIONS[:unite] dirname = UNITE_CHARS[dirname] if UNITE_CHARS.keys.include? dirname end if OPTIONS[:shorten] filename = filename[OPTIONS[:length]..(-1)] end if OPTIONS[:two_two] next if filename.size <= 4 dirname = filename[0..1] + "/" + filename[2..3] filename = filename[4..-1] end dirname + "/" + filename end