scholar-rename 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 397978ea892a1945fb1d9736227e68ded227e9bef3e45df49fd159681bf6b616
4
- data.tar.gz: bbc246ab4d4d2435a39ba016f1d9e1a0bf38568e166b63f4b15ef36ef72f00d6
3
+ metadata.gz: 18d3a5bccb17ac4d45228e58e689124428493e4ccba7f83b1690c540a6919af3
4
+ data.tar.gz: 7ebe641484f488f50ae01dbe5358dcc3e2f8988fc9a15d877482d5330d2915cd
5
5
  SHA512:
6
- metadata.gz: bf18f43015aac9640cf69c33d5753e8eeb10c4ac92b6005e624e63ae0b216783741493ffcc889f27090bfe8efc7f1c044310cd51fd31735a17e094988656d177
7
- data.tar.gz: 56bfb5522f243da2ebe3ef4a1146ef5b1f7f45ee78d81b9ae09360a4347a7dc699c59d8ed148bb3142eace2e9bc0b926335d8a668a65c07a477b7b3b5f5f9ac3
6
+ metadata.gz: 80157449b67dad30f1df46217afbc2919f5ea9d8bd76e689d4fd2ba5dffff4c8dee0b1aeb9fb48a74e1874dd13302514a1891805724e463d513136947b356103
7
+ data.tar.gz: 4345f98dbf5b26f94922aa690280ebc22492882b4b42eece595cc1ca0194e466fe9104417247069701a466cdf25f740227d25309dd258cfdee257672d635fcb4
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 '../lib/selector'
11
- require_relative '../lib/renamer'
10
+ require_relative "../lib/selector"
11
+ require_relative "../lib/renamer"
12
12
 
13
13
  r = Renamer.new ARGV
data/lib/renamer.rb CHANGED
@@ -1,47 +1,61 @@
1
1
  require_relative "./version.rb"
2
2
 
3
3
  class Renamer
4
- @@TESTING = false
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
- #puts args.inspect
9
+ al = args.last
11
10
 
12
- @opts = {}
13
- @vers = SR::Version
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 !args.last.nil? && args.last[0] != "-"
18
- @file = File.join(Dir.pwd, args.last).to_s
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
- if @file && !File.file?(@file)
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
- if args.length == 0
27
- puts "usage: scholar-rename (--format #) [file.pdf]"
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 @vers
32
- elsif a0 == "--format" && args.length == 1
33
- @formats.each_with_index {|x, i| puts "\t#{i}: #{x}"}
44
+ puts @version
45
+ exit 0 unless @options[:test]
34
46
  elsif !has_prereq?
35
47
  puts "please install pdftotext (via poppler) to use scholar-rename"
36
- puts "brew install pkg-config poppler"
48
+ puts "OSX: brew install pkg-config poppler"
37
49
  exit 1
38
- elsif a0 == "--format" && args.length > 1
39
- @format = args[1].to_s
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
40
56
  end
41
57
 
42
- if @file && File.file?(@file)
43
- rename
44
- end
58
+ rename args if @real_file
45
59
  end
46
60
 
47
61
  def has_prereq?
@@ -49,22 +63,20 @@ class Renamer
49
63
  $?.success?
50
64
  end
51
65
 
52
- def rename
66
+ def rename(args)
53
67
  raw = `pdftotext -q '#{@file}' -` # may contain non-ascii characters
54
- content = raw.encode('UTF-8', :invalid => :replace, :undef => :replace)
55
-
56
- # for debugging
57
- #now = Time.now.to_i.to_s # current time, unique temp file
58
- #temp = File.join(Dir.pwd, "temp-scholar-rename-text-#{now}")
59
- #system("pdftotext -q '#{@file}' '#{temp}'")
60
-
68
+ content = raw.encode("UTF-8", :invalid => :replace, :undef => :replace)
69
+
61
70
  # Choose pdf qualities
62
71
  @selector.set_content(content)
63
- @selector.options = {:format => @format.to_i} if @format
64
- @selector.select_all # choose props
72
+ @selector.options = @options
73
+ @selector.select_all
74
+ md = @selector.metadata
65
75
 
66
- if @@TESTING
67
- puts @file, @selector.title
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"
68
80
  else
69
81
  File.rename(@file, @selector.title)
70
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 :content, :options
6
+ attr_reader :title, :metadata, :content
7
+ attr_accessor :options
14
8
 
15
- # https://stackoverflow.com/questions/9503554/
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
- @content = str.split("\n")[0..14]
24
- .reject {|x| x.length < 2 }
25
- .map {|x| x[0..100] } # trim
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
- puts "Options:".bow
30
- @content.each_with_index {|l, i| puts "#{i}\t#{l}" }
31
- printf "Select title line number ".bow
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 ".bow
32
+ printf "Select author line number " unless @options[:auto]
35
33
  authors = choose(@content, print: false)
36
34
 
37
- puts "Select author form:".bow
35
+ puts "Select author form:" unless @options[:auto]
38
36
  author = gen_authors(authors)
39
37
 
40
- year = gen_year # just read it
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
- "#{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
- ]
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. pretty sketchy way to interpret
72
- # the users input as a range or integer, but seems to be working for now.
73
- # requires you to check for an array and join it on the downside though
74
- def choose(options, print: true)
75
- if options.length > 0
76
- options.each_with_index {|l, i| puts "#{i}\t#{l}" } if print
77
- printf "[0 - #{options.length-1}]: ".bow
78
- line = STDIN.gets.chomp || 0
79
- meta = "options[#{line}]"
80
- mout = eval meta # in theory terrible but this ain't a rails app.....
81
- if mout.is_a? (Array)
82
- mout.join ' '
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.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$/, '') } # delete ref number.
88
+ lines = aline.split(", ").map { |a| a.sub(/\d$/, "") } # delete ref number.
93
89
  if lines.is_a?(String)
94
- aline # return first
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
- # parse out a year from a string, for each line of the document until found.
102
- # then clean it up and return it
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
- @fulltxt.each do |l| # find year
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
@@ -1,3 +1,3 @@
1
1
  module SR
2
- Version = "0.3.7"
2
+ Version = "0.4.0"
3
3
  end
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.3.7
4
+ version: 0.4.0
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: 2019-10-21 00:00:00.000000000 Z
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,8 +69,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
69
  - !ruby/object:Gem::Version
84
70
  version: '0'
85
71
  requirements: []
86
- rubygems_version: 3.0.6
87
- signing_key:
72
+ rubygems_version: 3.2.15
73
+ signing_key:
88
74
  specification_version: 4
89
75
  summary: Rename pdfs based on author/title/year.
90
76
  test_files: []