#! /usr/bin/env ruby # coding: utf-8 # Rename a filename which replace a string between parentheses to just before extension. # Parentheses after a string which is not in parentheses leave. e.g., # ((ab)cd)(ef)gh(ij)kl(mn).txt -> gh(ij)kl(mn)((ab)cd)(ef).txt # Not check correspondence parentheses # ((ab)cd)(ef)gh(ij)kl(mn.txt -> gh(ij)kl(mn((ab)cd)(ef).txt require "rubygems" gem "builtinextension" require "string_split_parens.rb" require "filerenamer/commander.rb" require "filerenamer/optionparser.rb" OPTIONS = {} op = FileRenamer::OptionParser.new op.parse!(ARGV) fr = FileRenamer::Commander.new(op.options, ARGV) fr.execute do |filename| extname = File.extname( filename) basename = File.basename(filename).sub(/#{extname}$/, "") head = [] tail = [] tail_flag = true basename.split_parens.each do |word| tail_flag = false unless word[0].open_paren? if tail_flag tail << word else head << word end end new_name = head.join + tail.join + extname end #def checkParen(char) # case char # when '(' then return 1 # when '{' then return 1 # when '[' then return 1 # when '<' then return 1 # when '(' then return 1 # when '{' then return 1 # #when '[' then return 1 #occur error? # when '<' then return 1 # when '〔' then return 1 # when '【' then return 1 # when '〈' then return 1 # when '《' then return 1 # #when '「' then return 1 # #when '『' then return 1 # # when ')' then return -1 # when '}' then return -1 # when ']' then return -1 # when '>' then return -1 # when ')' then return -1 # when '}' then return -1 # #when ']' then return -1 #occur error? # when '>' then return -1 # when '〕' then return -1 # when '】' then return -1 # when '〉' then return -1 # when '》' then return -1 # #when '」' then return -1 # #when '』' then return -1 # # else return 0 # end #end # 単純に正規表現をすると欲張りマッチで最初から最後までマッチしかねない。 # そのため括弧の対応を depth を見て処理する。 # すなわち ([ などの左括弧のたびに深さを 1 ずつ大きくし、 # )] などの右括弧のたびに深さを 1ずつ小さくする。 # プログラム開始時を depth = 0 とし、 # 最初の文字が左括弧でなければそこで終了。 # 左括弧ならば処理が進み、depth == 0 となる右括弧があればその時点で括弧内を # 拡張子直前に移す。 # #require 'optparse' #ProgramConfig = Hash.new #オプション保存用のハッシュ #opt = OptionParser.new #opt.on('-e', '--execute', 'execute to rename') { |v| ProgramConfig[:e] = true } ##opt.on('-c', '--copy', 'copy, not move') { |v| ProgramConfig[:c] = true } #opt.parse!(ARGV) # #oldNames = ARGV #oldNames.each do |oldName| # extName = File.extname(oldName) # basenameAry = File.basename(oldName, extName).split(//) # #String can hardly deal with multi byte char divide. Use array. # # depth = 0 # i = 0 # tailStrAry = Array.new # while(checkParen(basenameAry[0]) == 1) #continue while the strig starts with left paren # headStrAry = Array.new # begin # char = basenameAry[0] # depth += checkParen(char) # headStrAry.push( char ) # basenameAry.shift # # if ((basenameAry.size == 0) && (depth > 0 )) # error check. # print "Parentheses mismatch. Rmaining depth: #{depth}\n" # exit # end # end while (depth > 0) # tailStrAry += headStrAry # # #remove space characters at start. # basenameAry = basenameAry.join.sub(/^\s*/, '').split(//) # end # newName = (basenameAry + tailStrAry).join + extName # # FileRenamer.new(oldName, newName, ProgramConfig, Time.now ).checkAndGo # #end #