scholar-rename 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/bin/scholar-rename +13 -0
- data/lib/renamer.rb +31 -0
- data/lib/selector.rb +89 -0
- data/lib/version.rb +1 -0
- data/readme.md +9 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ed79b92fcf9f579c34e3eaba52af4af7aee50e1
|
4
|
+
data.tar.gz: 4daa459bf18e325d1de3fb55096b652d8f600c04
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5be2495e46ce5ce2dcc299061aaa56bb598785dd1e152aaedd1922970df6b8326a285a7cc8b71412516d9d3a30763e2655355e03562502871584256dd83bd4c3
|
7
|
+
data.tar.gz: 4f3082e00d2e661a6b99b33e1780ff7f6a5be069ab6281d5971cc368af0fbb0abb3ea840bd94e1d9380853bdfff6ecd1c2e389e303d458a5311fc4286048de93
|
data/bin/scholar-rename
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# by jeremy warner, fall 2016
|
4
|
+
# scholar-rename, interactive pdf-namer
|
5
|
+
# prereq: pdftotext installation for text dump
|
6
|
+
# renames a pdf file to author-title-year.pdf
|
7
|
+
# or other formats based on your selection
|
8
|
+
# todo - make a suite of decent tests
|
9
|
+
|
10
|
+
require_relative '../lib/selector'
|
11
|
+
require_relative '../lib/renamer'
|
12
|
+
|
13
|
+
r = Renamer.new ARGV
|
data/lib/renamer.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
class Renamer
|
2
|
+
def initialize(*args)
|
3
|
+
@file = File.join(Dir.pwd, args.first)
|
4
|
+
|
5
|
+
if @file == Dir.pwd.to_s + '/'
|
6
|
+
puts "scholar-rename: provide a file"
|
7
|
+
else
|
8
|
+
rename
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def rename # move file based on users input
|
13
|
+
now = Time.now.to_i.to_s # current time, unique temp file
|
14
|
+
temp = File.join(Dir.pwd, "temp-scholar-rename-text-#{now}")
|
15
|
+
system("pdftotext -q '#{@file}' '#{temp}'")
|
16
|
+
content = File.read temp
|
17
|
+
|
18
|
+
# Choose pdf qualities
|
19
|
+
s = Selector.new(content)
|
20
|
+
s.select_all # choose props
|
21
|
+
printf "Ok? [Yn]: "
|
22
|
+
conf = STDIN.gets.chomp # confirm title
|
23
|
+
|
24
|
+
begin # file read in from first ARGV above
|
25
|
+
File.rename(@file, s.title) unless conf.match /^(n|N).*/
|
26
|
+
ensure # cleaning up @file cleanup from pdftotext
|
27
|
+
puts "Cleaning up..."
|
28
|
+
File.delete(temp)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/selector.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# Class for choosing and displaying the information from the pdf.
|
2
|
+
# I wonder if this could be augmented with information from google scholar
|
3
|
+
# somehow, there is probably a ruby (or at least python) api.
|
4
|
+
|
5
|
+
class Selector
|
6
|
+
attr_reader :title
|
7
|
+
|
8
|
+
# take the first 10 lines of the pdftotext output and assign it to the
|
9
|
+
# context class instance variable, use later when choosing selector
|
10
|
+
def initialize(c)
|
11
|
+
@content = c.split("\n")[0..14].reject {|x| x.length < 2 }
|
12
|
+
@fulltxt = c.split("\n")
|
13
|
+
end
|
14
|
+
|
15
|
+
def select_all
|
16
|
+
puts "Select title line number:"
|
17
|
+
title = choose @content
|
18
|
+
|
19
|
+
puts "Select author line number:"
|
20
|
+
authors = choose @content
|
21
|
+
|
22
|
+
puts "Select author form number:"
|
23
|
+
author = gen_authors(authors)
|
24
|
+
|
25
|
+
year = gen_year # just read it
|
26
|
+
|
27
|
+
puts "Select desired title format:"
|
28
|
+
@title = choose(gen_forms year, title, author)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
# Pass in an array to list and be selected, and return the element that the
|
33
|
+
# user selects back to the calling method. pretty sketchy way to interpret
|
34
|
+
# the users input as a range or integer, but seems to be working for now.
|
35
|
+
# requires you to check for an array and join it on the downside though
|
36
|
+
def choose(opts)
|
37
|
+
opts.each_with_index {|l, i| puts "#{i}\t#{l}" }
|
38
|
+
printf "Your selection [0 - #{opts.length-1}]: "
|
39
|
+
line = STDIN.gets.chomp
|
40
|
+
meta = "opts[#{line}]"
|
41
|
+
mout = eval meta # in theory terrible but this aint a rails app............
|
42
|
+
if mout.is_a? (Array)
|
43
|
+
mout.join ' '
|
44
|
+
else
|
45
|
+
mout
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Generate different forms for author selection, enumerating the different
|
50
|
+
# author that you want to save the file as. Split based on a comma.
|
51
|
+
def gen_authors(aline)
|
52
|
+
lines = aline.split(", ").map {|a| a.sub /\d$/, '' } # delete ref number.
|
53
|
+
if lines.is_a?(String)
|
54
|
+
aline # return first
|
55
|
+
else # its an array, augment w/ lname and choose
|
56
|
+
alines = lines.map {|a| a.split.last } + lines
|
57
|
+
choose alines
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# based on the collected information, generate different forms of the title.
|
62
|
+
# perhaps at some point this could be autoselected from the command line,
|
63
|
+
# like how the date formatting works, using symbols for different features...
|
64
|
+
# in theory could also handle this with the eval method like above - but this
|
65
|
+
# would probably be better as a send call or an instance_eval method :/
|
66
|
+
def gen_forms(y, t, a)
|
67
|
+
ad = a.downcase
|
68
|
+
au = a.upcase
|
69
|
+
return [
|
70
|
+
"#{a}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
71
|
+
"#{au}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
72
|
+
"#{ad}_#{y}_#{t}.pdf".gsub(" ", "_"),
|
73
|
+
"#{a} #{y} #{t}.pdf",
|
74
|
+
"#{au} #{y} #{t}.pdf",
|
75
|
+
"#{ad} #{y} #{t}.pdf",
|
76
|
+
]
|
77
|
+
end
|
78
|
+
|
79
|
+
# parse out a year from a string, for each line of the document until found.
|
80
|
+
# then clean it up and return it
|
81
|
+
def gen_year
|
82
|
+
@fulltxt.each do |l| # find year
|
83
|
+
lm = l.match(/(19|20)\d\d/)
|
84
|
+
return lm[0] if !lm.nil?
|
85
|
+
end
|
86
|
+
|
87
|
+
Time.now.year.to_s # not matched, just return current year
|
88
|
+
end
|
89
|
+
end
|
data/lib/version.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
module SR; Version = "0.1"; end
|
data/readme.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# scholar-rename `interactive pdf-namer`
|
2
|
+
|
3
|
+
- prereq: pdftotext installation for text dump
|
4
|
+
- renames a pdf file to author-title-year.pdf
|
5
|
+
- or other formats based on your selection
|
6
|
+
- by jeremy warner, fall 2016
|
7
|
+
|
8
|
+
You need this: https://en.wikipedia.org/wiki/Pdftotext
|
9
|
+
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scholar-rename
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Warner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Interactive tool to rename pdfs based on author/title/year.
|
42
|
+
email: jeremywrnr@gmail.com
|
43
|
+
executables:
|
44
|
+
- scholar-rename
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- bin/scholar-rename
|
49
|
+
- lib/renamer.rb
|
50
|
+
- lib/selector.rb
|
51
|
+
- lib/version.rb
|
52
|
+
- readme.md
|
53
|
+
homepage: http://github.com/jeremywrnr/scholar-rename
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.6.8
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Rename pdfs based on author/title/year.
|
77
|
+
test_files: []
|