scholar-rename 0.3.4 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/bin/scholar-rename +2 -2
- data/lib/renamer.rb +48 -35
- data/lib/selector.rb +54 -58
- data/lib/version.rb +1 -1
- data/readme.md +1 -1
- metadata +6 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cdb5cff14beb38b02d6e11aa44a05c8770dcea22aa520f6b61e77c25c9e8088b
|
4
|
+
data.tar.gz: 36d7dc8907251cb05df51f66323ec5ba2c55046fbb03247c33ca1a962ef5ceec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa2b9fd4f226bfb6ca45c2847c9e16ec6bebe97a99e00b3cb4f220414bda6d55cf3b7f7a898c58fc843e5e08ed7ba66b86d5fee2733fe2eef6d89da01898c85d
|
7
|
+
data.tar.gz: e24de26e632677c59ebddb7464eba87434a4ecabbe8579278399b2a4edb9cf7d9c63f38f82e60719b0df027fe90bde61eb44cc2375ecf8447b29284ce1af3e32
|
data/bin/scholar-rename
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
# renames a pdf file to author-title-year.pdf
|
8
8
|
# or other formats based on your selection
|
9
9
|
|
10
|
-
require_relative
|
11
|
-
require_relative
|
10
|
+
require_relative "../lib/selector"
|
11
|
+
require_relative "../lib/renamer"
|
12
12
|
|
13
13
|
r = Renamer.new ARGV
|
data/lib/renamer.rb
CHANGED
@@ -1,69 +1,82 @@
|
|
1
1
|
require_relative "./version.rb"
|
2
2
|
|
3
3
|
class Renamer
|
4
|
-
|
4
|
+
attr_reader :version, :formats, :selector, :format
|
5
5
|
|
6
|
-
attr_reader :vers, :formats, :selector, :format
|
7
6
|
def initialize(*args)
|
8
7
|
args = args.flatten
|
9
8
|
a0 = args.first
|
10
|
-
|
9
|
+
al = args.last
|
11
10
|
|
12
|
-
@
|
13
|
-
@
|
11
|
+
@real_file = false
|
12
|
+
@options = { :format => 0, :auto => false, :debug => false, :test => false }
|
13
|
+
@version = SR::Version
|
14
14
|
@selector = Selector.new
|
15
15
|
@formats = @selector.gen_forms("Year", "Title", "Author")
|
16
16
|
|
17
|
-
if
|
18
|
-
@
|
17
|
+
if args.include? "--debug"
|
18
|
+
@options[:debug] = true
|
19
|
+
end
|
20
|
+
|
21
|
+
if args.include? "--test"
|
22
|
+
@options[:test] = true
|
23
|
+
def puts(*x) x; end
|
19
24
|
end
|
20
25
|
|
21
|
-
|
26
|
+
# Filename comes last.
|
27
|
+
if !al.nil? && al[0] != "-"
|
28
|
+
@file = File.join(Dir.pwd, args.last).to_s
|
29
|
+
@real_file = @file && File.file?(@file)
|
30
|
+
end
|
31
|
+
if @file && !@real_file
|
22
32
|
puts "Input #{@file} not found."
|
33
|
+
puts "Please specify a pdf file to use with scholar-rename."
|
23
34
|
exit 1
|
24
35
|
end
|
25
36
|
|
26
|
-
|
27
|
-
|
37
|
+
# Main argument processing.
|
38
|
+
if args.length == 0 || a0 == "--h" || a0 == "--help"
|
39
|
+
puts "usage: scholar-rename (--format #) (--auto) [file.pdf]"
|
40
|
+
puts "\t--show-formats\tshow format options"
|
41
|
+
puts "\t--auto\tpick default formatter"
|
28
42
|
puts "\t-v, --version\tshow version number"
|
29
|
-
puts "\t--format\tshow format options"
|
30
43
|
elsif a0 == "-v" || a0 == "--version"
|
31
|
-
puts @
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
puts "
|
36
|
-
|
37
|
-
elsif a0 == "--
|
38
|
-
@
|
44
|
+
puts @version
|
45
|
+
exit 0 unless @options[:test]
|
46
|
+
elsif !has_prereq?
|
47
|
+
puts "please install pdftotext (via poppler) to use scholar-rename"
|
48
|
+
puts "OSX: brew install pkg-config poppler"
|
49
|
+
exit 1
|
50
|
+
elsif a0 == "--show-formats"
|
51
|
+
@formats.each_with_index { |x, i| puts "\t#{i}: #{x}" }
|
52
|
+
elsif a0 == "--format"
|
53
|
+
@options[:format] = args[1].to_i
|
54
|
+
elsif a0 == "--auto"
|
55
|
+
@options[:auto] = true
|
39
56
|
end
|
40
57
|
|
41
|
-
|
42
|
-
rename
|
43
|
-
end
|
58
|
+
rename args if @real_file
|
44
59
|
end
|
45
60
|
|
46
61
|
def has_prereq?
|
47
|
-
system("pdftotext 2> /dev/null")
|
62
|
+
system("pdftotext -v 2> /dev/null")
|
48
63
|
$?.success?
|
49
64
|
end
|
50
65
|
|
51
|
-
def rename
|
66
|
+
def rename(args)
|
52
67
|
raw = `pdftotext -q '#{@file}' -` # may contain non-ascii characters
|
53
|
-
content = raw.encode(
|
54
|
-
|
55
|
-
# for debugging
|
56
|
-
#now = Time.now.to_i.to_s # current time, unique temp file
|
57
|
-
#temp = File.join(Dir.pwd, "temp-scholar-rename-text-#{now}")
|
58
|
-
#system("pdftotext -q '#{@file}' '#{temp}'")
|
59
|
-
|
68
|
+
content = raw.encode("UTF-8", :invalid => :replace, :undef => :replace)
|
69
|
+
|
60
70
|
# Choose pdf qualities
|
61
71
|
@selector.set_content(content)
|
62
|
-
@selector.options =
|
63
|
-
@selector.select_all
|
72
|
+
@selector.options = @options
|
73
|
+
@selector.select_all
|
74
|
+
md = @selector.metadata
|
64
75
|
|
65
|
-
if
|
66
|
-
puts
|
76
|
+
if @options[:debug]
|
77
|
+
puts md[:year] if args.include? "--show-year"
|
78
|
+
puts md[:title] if args.include? "--show-title"
|
79
|
+
puts md[:author] if args.include? "--show-author"
|
67
80
|
else
|
68
81
|
File.rename(@file, @selector.title)
|
69
82
|
puts "Saved #{@selector.title}"
|
data/lib/selector.rb
CHANGED
@@ -2,106 +2,102 @@
|
|
2
2
|
# I wonder if this could be augmented with information from google scholar
|
3
3
|
# somehow, there is probably a ruby (or at least python) api.
|
4
4
|
|
5
|
-
require 'colored'
|
6
|
-
|
7
|
-
class String
|
8
|
-
alias_method :bow, :black_on_white
|
9
|
-
end
|
10
|
-
|
11
5
|
class Selector
|
12
|
-
attr_reader :title
|
13
|
-
attr_accessor :
|
6
|
+
attr_reader :title, :metadata, :content
|
7
|
+
attr_accessor :options
|
14
8
|
|
15
|
-
|
16
|
-
def initialize(c = '', opts = {:format => 0})
|
9
|
+
def initialize(c = "Test\nPDF\nContent", opts = { :format => 0, :auto => true })
|
17
10
|
set_content(c)
|
18
|
-
@fulltxt = c.split("\n")
|
19
11
|
@options = opts
|
12
|
+
if opts[:test]
|
13
|
+
def puts(*x) x; end
|
14
|
+
end
|
20
15
|
end
|
21
16
|
|
22
17
|
def set_content(str)
|
23
|
-
@
|
24
|
-
|
25
|
-
.
|
18
|
+
@full_text = str.split("\n")
|
19
|
+
@content = @full_text[0..14]
|
20
|
+
.reject { |x| x.length < 2 }
|
21
|
+
.map { |x| x[0..100] } # trim
|
26
22
|
end
|
27
23
|
|
28
24
|
def select_all
|
29
|
-
|
30
|
-
|
31
|
-
|
25
|
+
if !@options[:auto]
|
26
|
+
puts "Options:"
|
27
|
+
@content.each_with_index { |l, i| puts "#{i}\t#{l}" }
|
28
|
+
end
|
29
|
+
printf "Select title line number " unless @options[:auto]
|
32
30
|
title = choose(@content, print: false)
|
33
31
|
|
34
|
-
printf "Select author line number "
|
32
|
+
printf "Select author line number " unless @options[:auto]
|
35
33
|
authors = choose(@content, print: false)
|
36
34
|
|
37
|
-
puts "Select author form:"
|
35
|
+
puts "Select author form:" unless @options[:auto]
|
38
36
|
author = gen_authors(authors)
|
39
37
|
|
40
|
-
|
41
|
-
|
38
|
+
# Automatically match.
|
39
|
+
year = gen_year
|
40
|
+
|
42
41
|
forms = gen_forms(year, title, author)
|
42
|
+
@metadata = {:year => year, :title => title, :author => author}
|
43
43
|
@title = forms[@options[:format]]
|
44
|
-
|
45
|
-
#puts "Select desired title:".bow
|
46
|
-
#@title = choose(forms)
|
47
44
|
end
|
48
45
|
|
49
46
|
# 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
47
|
def gen_forms(y, t, a)
|
55
48
|
ad = a.downcase
|
56
49
|
au = a.upcase
|
57
50
|
return [
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
51
|
+
"#{y} - #{a} - #{t}.pdf",
|
52
|
+
"#{y} #{a} #{t}.pdf",
|
53
|
+
"#{a}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
54
|
+
"#{au}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
55
|
+
"#{ad}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
56
|
+
"#{a} #{y} #{t}.pdf",
|
57
|
+
"#{au} #{y} #{t}.pdf",
|
58
|
+
"#{ad} #{y} #{t}.pdf",
|
59
|
+
]
|
67
60
|
end
|
68
61
|
|
69
|
-
private
|
70
62
|
# Pass in an array to list and be selected, and return the element that the
|
71
|
-
# user selects back to the calling method.
|
72
|
-
# the users input as a range
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
else
|
84
|
-
mout
|
63
|
+
# user selects back to the calling method. this is a way to interpret
|
64
|
+
# the users input as a range (e.g., 0..2) or an integer (1).
|
65
|
+
def choose(list, print: true)
|
66
|
+
raise "List is empty." if list.empty?
|
67
|
+
|
68
|
+
if @options[:auto]
|
69
|
+
line = 0
|
70
|
+
else
|
71
|
+
# Never print when --auto.
|
72
|
+
if print
|
73
|
+
list.each_with_index { |l, i| puts "#{i}\t#{l}" }
|
74
|
+
printf "[0 - #{options.length - 1}]: "
|
85
75
|
end
|
76
|
+
line = STDIN.gets.chomp || 0
|
86
77
|
end
|
78
|
+
|
79
|
+
meta = "list[#{line}]"
|
80
|
+
mout = eval meta
|
81
|
+
mout = mout.join " " if mout.is_a? (Array)
|
82
|
+
mout
|
87
83
|
end
|
88
84
|
|
89
85
|
# Generate different forms for author selection, enumerating the different
|
90
86
|
# author that you want to save the file as. Split based on a comma.
|
91
87
|
def gen_authors(aline)
|
92
|
-
lines = aline.split(", ").map {|a| a.sub(/\d$/,
|
88
|
+
lines = aline.split(", ").map { |a| a.sub(/\d$/, "") } # delete ref number.
|
93
89
|
if lines.is_a?(String)
|
94
|
-
aline
|
90
|
+
aline
|
95
91
|
else # its an array, augment w/ lname and choose
|
96
|
-
alines = lines.map {|a| a.split.last } + lines
|
92
|
+
alines = lines.map { |a| a.split.last } + lines
|
97
93
|
choose alines
|
98
94
|
end
|
99
95
|
end
|
100
96
|
|
101
|
-
#
|
102
|
-
#
|
97
|
+
# Parse out a year from a string, for each line of the document until found.
|
98
|
+
# Then clean it up and return it.
|
103
99
|
def gen_year
|
104
|
-
@
|
100
|
+
@full_text.each do |l| # find year
|
105
101
|
lm = l.match(/(19|20)\d\d/)
|
106
102
|
return lm[0] if !lm.nil?
|
107
103
|
end
|
data/lib/version.rb
CHANGED
data/readme.md
CHANGED
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scholar-rename
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Warner
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: colored
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rake
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -68,7 +54,7 @@ homepage: http://github.com/jeremywrnr/scholar-rename
|
|
68
54
|
licenses:
|
69
55
|
- MIT
|
70
56
|
metadata: {}
|
71
|
-
post_install_message:
|
57
|
+
post_install_message:
|
72
58
|
rdoc_options: []
|
73
59
|
require_paths:
|
74
60
|
- lib
|
@@ -83,9 +69,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
69
|
- !ruby/object:Gem::Version
|
84
70
|
version: '0'
|
85
71
|
requirements: []
|
86
|
-
|
87
|
-
|
88
|
-
signing_key:
|
72
|
+
rubygems_version: 3.2.15
|
73
|
+
signing_key:
|
89
74
|
specification_version: 4
|
90
75
|
summary: Rename pdfs based on author/title/year.
|
91
76
|
test_files: []
|