Skip to content

Commit

Permalink
get_title_and_rename增加测试,让代码更干净
Browse files Browse the repository at this point in the history
  • Loading branch information
404 committed Dec 6, 2012
1 parent 1fb85ff commit 29cd4d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
37 changes: 0 additions & 37 deletions #get_title_and_rename.rb#

This file was deleted.

37 changes: 28 additions & 9 deletions get_title_and_rename.rb
@@ -1,9 +1,10 @@
# -*- coding: utf-8 -*-
require 'find'
require 'fileutils'
require 'minitest/autorun'

# USAGE: ruby -w script.rb inputfolder
# !! update filename in place !!
# usage: ruby -w script.rb inputfolder
# files in inputfolder will be renamed accordingly

path = File.expand_path ARGV[0]
Expand All @@ -14,24 +15,42 @@
filepath = File.dirname file
filename = File.basename(file, '.txt')
File.open(file, 'r') do |l|
title = l.gets
title = l.gets # 默认第一行就是题目 且题目格式是 title: some title
# The (?:…) construct provides grouping without capturing.
#if m = title.match(/The Project Gutenberg (?:EBook|etext) of(.+)/i)
$realtitle = title.split(':')[1] # first line is titile, format, title: this is title

if $realtitle
$realtitle = title.iyc_get_title
if $realtitle # 也许有的文件就没有 title: some title 这行 必须得确认 #realtitle 不是 nil
$realtitle = $realtitle.strip.tr(',;:', '').gsub(/\s+/, '_').gsub(/\n|\r/, '').downcase
else
$realtitle = ''
$realtitle = ''
end
end
# p $realtitle
p filepath
newfilename = filename.downcase + '_' + $realtitle + '.txt'
newfilename = newfilename.gsub(/_+\./, '.') # handle "An_Essay_on_Profits_.txt"
p "rename #{filename} to #{newfilename}"
if filename + '.txt' != newfilename
if filename + '.txt' != newfilename # 如果将文件改名为它自身会报错,比如 mv file file 报错的
FileUtils.mv(file, "#{filepath}/#{newfilename}")
end # can't get title for some files so newfilename would be equal to filename, skip those!
end

class String
def iyc_get_title
self.split(':')[1..-1].join.strip
# array[1..-1] to cater to special case Title: title with colon: a true story
end
def iyc_title_to_filename
self.strip.tr(',;:!', '').gsub(/\s+/, '_').gsub(/\n|\r/, '').downcase
end
end

# tests

class TestSanitize < MiniTest::Unit::TestCase
def test_iyc_get_title
assert_equal 'The History Of The Decline And Fall Of The Roman Empire', 'Title: The History Of The Decline And Fall Of The Roman Empire'.iyc_get_title, 'test case 1'
assert_equal 'Les Miserables', 'Title: Les Miserables'.iyc_get_title, 'test case 2'
end
def test_iyc_title_to_filename
assert_equal 'some_strange_characters', 'Title: some; strange: characters!'.iyc_get_title.iyc_title_to_filename, 'test case 3'
end
end

0 comments on commit 29cd4d2

Please sign in to comment.