-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipsj-rename-papers.rb
executable file
·76 lines (67 loc) · 1.81 KB
/
ipsj-rename-papers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
# coding: utf-8
#
# 情報処理学会情報学広場でダウンロードしたPDFのファイル名をわかりやす
# く変更するプログラム
#
# Copyright (C) 2018 Kota Abe
#
# Web site: <http://github.com/ktabe/ipsj-rename-papers/>
#
# Usage: ruby ipsj-rename-papers.rb <import.xmlを含むディレクトリ名>
#
require 'rexml/document'
if ARGV.size != 1 then
warn("specify a path that contains \"import.xml\"")
exit 1
end
dir = ARGV[0]
if !File.directory?(dir) then
warn("#{dir} is not directory")
exit 1
end
importFile = "#{dir}/import.xml"
if !File.exists?(importFile) then
warn("#{dir} does not contain \"import.xml\"")
exit 1
end
import = File.read(importFile)
doc = REXML::Document.new(import)
exp = doc.elements['export']
title = ""
exp.each_element do |elem|
case elem.name
when "repository_item" then
title = elem.attribute("title").value()
when "repository_file" then
filename = elem.attribute("file_name").value()
# puts "#{title} -- #{filename}"
if (File.extname(filename) == ".pdf") then
# replace invalid chars with fullwidth ones
title.tr!('\\\\/:*?"<>|', "\/:*?”<>|")
base = File.basename(filename, ".pdf")
base.gsub!(/^IPSJ-/, '')
newname = "#{base}-#{title}.pdf"
puts "rename #{filename} -> #{newname}"
if (File.exists?(filename)) then
begin
File.rename(filename, newname)
dir = File.dirname(filename)
if (dir != '.') then
begin
Dir.delete(dir)
rescue => error
warn("rmdir failed: #{error}")
end
end
rescue => error
warn("rename failed: #{error}")
end
else
puts "#{base} does not exist"
end
end
end
end
# EOF