scholar-rename 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/scholar-rename +3 -4
- data/lib/renamer.rb +43 -25
- data/lib/selector.rb +35 -35
- data/lib/version.rb +1 -1
- data/readme.md +4 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47499931e9250cf000657f685993d0f04e6ee17c
|
4
|
+
data.tar.gz: 454568a17803cc45190267f660cb26ce54a93b41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5b907e260a3cf9dc1d37f685087cf18abbefa7d30917539b35f04b550bf382b6e33019ae20f214be50d0fd05ca11b9963ba414da67eabe7399045340fa574be
|
7
|
+
data.tar.gz: 2fcd1fdc6d81d8addf9616373114b6a70335b017f374a8af3fbb7726cd2acefc75ca4659f962f2308d9fa34dd9fbd8b7b57e93f43189ef0921f596d4f62378ef
|
data/bin/scholar-rename
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
# by jeremy warner, fall 2016
|
4
|
-
# scholar-rename
|
3
|
+
# by jeremy warner, started fall 2016
|
4
|
+
# scholar-rename: interactive pdf-namer
|
5
5
|
|
6
6
|
# prereq: pdftotext installation for text dump
|
7
7
|
# renames a pdf file to author-title-year.pdf
|
8
8
|
# or other formats based on your selection
|
9
|
-
# todo - make a suite of decent tests
|
10
9
|
|
11
10
|
require_relative '../lib/selector'
|
12
11
|
require_relative '../lib/renamer'
|
13
12
|
|
14
|
-
Renamer.new ARGV
|
13
|
+
r = Renamer.new ARGV
|
data/lib/renamer.rb
CHANGED
@@ -1,23 +1,45 @@
|
|
1
1
|
require_relative "./version.rb"
|
2
2
|
|
3
3
|
class Renamer
|
4
|
-
|
4
|
+
@@TESTING = false
|
5
|
+
|
6
|
+
attr_reader :vers, :formats, :selector, :format
|
5
7
|
def initialize(*args)
|
6
|
-
|
7
|
-
|
8
|
+
args = args.flatten
|
9
|
+
a0 = args.first
|
10
|
+
#puts args.inspect
|
11
|
+
|
12
|
+
@opts = {}
|
8
13
|
@vers = SR::Version
|
14
|
+
@selector = Selector.new
|
15
|
+
@formats = @selector.gen_forms("Year", "Title", "Author")
|
16
|
+
|
17
|
+
if !args.last.nil? && args.last[0] != "-"
|
18
|
+
@file = File.join(Dir.pwd, args.last).to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
if @file && !File.file?(@file)
|
22
|
+
puts "Input #{@file} not found."
|
23
|
+
exit 1
|
24
|
+
end
|
9
25
|
|
10
|
-
if
|
26
|
+
if args.length == 0
|
27
|
+
puts "usage: scholar-rename (--format #) [file.pdf]"
|
28
|
+
puts "\t-v, --version\tshow version number"
|
29
|
+
puts "\t--format\tshow format options"
|
30
|
+
elsif a0 == "-v" || a0 == "--version"
|
11
31
|
puts @vers
|
12
|
-
|
13
|
-
|
14
|
-
puts "usage: scholar-rename [pdf_file]"
|
15
|
-
puts "please specify a pdf file"
|
32
|
+
elsif a0 == "--format" && args.length == 1
|
33
|
+
@formats.each_with_index {|x, i| puts "\t#{i}: #{x}"}
|
16
34
|
elsif has_prereq?
|
17
35
|
puts "please install pdftotext to use scholar-rename"
|
18
36
|
puts "osx: brew install xpdf"
|
19
|
-
|
20
|
-
|
37
|
+
elsif a0 == "--format" && args.length > 1
|
38
|
+
@format = args[1].to_i
|
39
|
+
end
|
40
|
+
|
41
|
+
if @file && File.file?(@file)
|
42
|
+
rename
|
21
43
|
end
|
22
44
|
end
|
23
45
|
|
@@ -26,7 +48,7 @@ class Renamer
|
|
26
48
|
$?.success?
|
27
49
|
end
|
28
50
|
|
29
|
-
def rename
|
51
|
+
def rename
|
30
52
|
raw = `pdftotext -q '#{@file}' -` # may contain non-ascii characters
|
31
53
|
content = raw.encode('UTF-8', :invalid => :replace, :undef => :replace)
|
32
54
|
|
@@ -36,19 +58,15 @@ class Renamer
|
|
36
58
|
#system("pdftotext -q '#{@file}' '#{temp}'")
|
37
59
|
|
38
60
|
# Choose pdf qualities
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
#File.delete(temp)
|
50
|
-
#end
|
51
|
-
|
52
|
-
File.rename(@file, s.title)
|
61
|
+
@selector.set_content(content)
|
62
|
+
@selector.options = {:format => @format} if @format
|
63
|
+
@selector.select_all # choose props
|
64
|
+
|
65
|
+
if @@TESTING
|
66
|
+
puts @file, @selector.title
|
67
|
+
else
|
68
|
+
File.rename(@file, @selector.title)
|
69
|
+
puts "Saved #{@selector.title}"
|
70
|
+
end
|
53
71
|
end
|
54
72
|
end
|
data/lib/selector.rb
CHANGED
@@ -10,18 +10,19 @@ end
|
|
10
10
|
|
11
11
|
class Selector
|
12
12
|
attr_reader :title
|
13
|
+
attr_accessor :content, :options
|
13
14
|
|
14
|
-
# take the first 10 lines of the pdftotext output and assign it to the
|
15
|
-
# context class instance variable, use later when choosing selector
|
16
15
|
# https://stackoverflow.com/questions/9503554/
|
17
|
-
def initialize(c, opts)
|
18
|
-
|
16
|
+
def initialize(c = '', opts = {:format => 0})
|
17
|
+
set_content(c)
|
18
|
+
@fulltxt = c.split("\n")
|
19
|
+
@options = opts
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_content(str)
|
23
|
+
@content = str.split("\n")[0..14]
|
19
24
|
.reject {|x| x.length < 2 }
|
20
25
|
.map {|x| x[0..100] } # trim
|
21
|
-
@fulltxt = c.split("\n")
|
22
|
-
if opts == "-f"
|
23
|
-
@format = TRUE # autoselect format type
|
24
|
-
end
|
25
26
|
end
|
26
27
|
|
27
28
|
def select_all
|
@@ -38,12 +39,31 @@ class Selector
|
|
38
39
|
|
39
40
|
year = gen_year # just read it
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
forms = gen_forms(year, title, author)
|
43
|
+
@title = forms[@options[:format]]
|
44
|
+
|
45
|
+
#puts "Select desired title:".bow
|
46
|
+
#@title = choose(forms)
|
47
|
+
end
|
48
|
+
|
49
|
+
# based on the collected information, generate different forms of the title.
|
50
|
+
# perhaps at some point this could be autoselected from the command line,
|
51
|
+
# like how the date formatting works, using symbols for different features...
|
52
|
+
# in theory could also handle this with the eval method like above - but this
|
53
|
+
# would probably be better as a send call or an instance_eval method :/
|
54
|
+
def gen_forms(y, t, a)
|
55
|
+
ad = a.downcase
|
56
|
+
au = a.upcase
|
57
|
+
return [
|
58
|
+
"#{y} - #{a} - #{t}.pdf",
|
59
|
+
"#{y} #{a} #{t}.pdf",
|
60
|
+
"#{a}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
61
|
+
"#{au}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
62
|
+
"#{ad}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
63
|
+
"#{a} #{y} #{t}.pdf",
|
64
|
+
"#{au} #{y} #{t}.pdf",
|
65
|
+
"#{ad} #{y} #{t}.pdf",
|
66
|
+
]
|
47
67
|
end
|
48
68
|
|
49
69
|
private
|
@@ -57,7 +77,7 @@ class Selector
|
|
57
77
|
printf "[0 - #{options.length-1}]: ".bow
|
58
78
|
line = STDIN.gets.chomp || 0
|
59
79
|
meta = "options[#{line}]"
|
60
|
-
mout = eval meta # in theory terrible but this
|
80
|
+
mout = eval meta # in theory terrible but this ain't a rails app.....
|
61
81
|
if mout.is_a? (Array)
|
62
82
|
mout.join ' '
|
63
83
|
else
|
@@ -78,26 +98,6 @@ class Selector
|
|
78
98
|
end
|
79
99
|
end
|
80
100
|
|
81
|
-
# based on the collected information, generate different forms of the title.
|
82
|
-
# perhaps at some point this could be autoselected from the command line,
|
83
|
-
# like how the date formatting works, using symbols for different features...
|
84
|
-
# in theory could also handle this with the eval method like above - but this
|
85
|
-
# would probably be better as a send call or an instance_eval method :/
|
86
|
-
def gen_forms(y, t, a)
|
87
|
-
ad = a.downcase
|
88
|
-
au = a.upcase
|
89
|
-
return [
|
90
|
-
"#{y} - #{a} - #{t}.pdf",
|
91
|
-
"#{y} #{a} #{t}.pdf",
|
92
|
-
"#{a}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
93
|
-
"#{au}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
94
|
-
"#{ad}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
95
|
-
"#{a} #{y} #{t}.pdf",
|
96
|
-
"#{au} #{y} #{t}.pdf",
|
97
|
-
"#{ad} #{y} #{t}.pdf",
|
98
|
-
]
|
99
|
-
end
|
100
|
-
|
101
101
|
# parse out a year from a string, for each line of the document until found.
|
102
102
|
# then clean it up and return it
|
103
103
|
def gen_year
|
data/lib/version.rb
CHANGED
data/readme.md
CHANGED
@@ -11,6 +11,10 @@ an interactive pdf-renamer tool.
|
|
11
11
|
|
12
12
|
you'll also need: https://en.wikipedia.org/wiki/Pdftotext
|
13
13
|
|
14
|
+
for macOS, you can install w/ brew:
|
15
|
+
|
16
|
+
brew install poppler
|
17
|
+
|
14
18
|
## about
|
15
19
|
|
16
20
|
renames a pdf file to author-title-year.pdf or other formats based on your
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scholar-rename
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Warner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colored
|