scholar-rename 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/lib/renamer.rb +24 -12
  3. data/lib/selector.rb +23 -11
  4. data/lib/version.rb +3 -1
  5. data/readme.md +22 -6
  6. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d039aaa0b17ca8cbc90f85c2451bddece6057ddb
4
- data.tar.gz: 4815af5eb0879eeabd6cae2b460e2bc9ddde8676
3
+ metadata.gz: 79107ab113b4a37427bee2fd9a2996a46ec3bbc4
4
+ data.tar.gz: 3b8f5e99e677875181da3d34782b558894ad2fd5
5
5
  SHA512:
6
- metadata.gz: 0e1b4510e462585608e88f6058c22565ca572d66dc55dcc4fb2858bb885800aa40094282b7a16cf9599f68132edfb5d19a8ecde839d3800e864d2a3acda41bea
7
- data.tar.gz: 77bdc55ea52ac0c6fd3deff70ac13e61b8dd10419c986582f899ea566ca072e77a4451ee3e7f77e3f98cfb001d22d0f7b3d1680eeff0b0b7f48b68056ce96390
6
+ metadata.gz: cc6e359f1efebc07bce6d59f9202e7ded85a281d4ebb2b59698cfcbeadccb226e5d7ebfd8c996399742f2734fce7b4cde9f3b893b0b11503f2cc7e358f3d576f
7
+ data.tar.gz: 522fc2acf038c82ddf6b2c3ef783e540dafdf8c830a6d0064739c1503d067ea066ce4900b5acb1d81e6d6b84ac35a8e21ffcbed0961a6e636c619815e0fb0e08
data/lib/renamer.rb CHANGED
@@ -3,32 +3,44 @@ class Renamer
3
3
  a1 = args.first
4
4
  @file = File.join(Dir.pwd, a1)
5
5
 
6
- if @file == Dir.pwd.to_s + '/'
7
- puts "scholar-rename: provide a file"
8
- elsif a1 == "-v"
6
+ if a1 == "-v"
9
7
  puts SR::Version
8
+ elsif @file == Dir.pwd.to_s + '/'
9
+ puts "usage: scholar-rename [pdf_file]"
10
+ puts "please specify a pdf file"
11
+ elsif has_prereq?
12
+ puts "please install pdftotext to use scholar-rename"
13
+ puts "osx: brew install xpdf"
10
14
  else
11
15
  rename
12
16
  end
13
17
  end
14
18
 
15
- def rename # move file based on users input
16
- now = Time.now.to_i.to_s # current time, unique temp file
17
- temp = File.join(Dir.pwd, "temp-scholar-rename-text-#{now}")
18
- system("pdftotext -q '#{@file}' '#{temp}'")
19
- content = File.read temp
19
+ def has_prereq?
20
+ system("pdftotext 2> /dev/null")
21
+ $?.success?
22
+ end
23
+
24
+ def rename
25
+ raw = `pdftotext -q '#{@file}' -` # may contain non-ascii characters
26
+ content = raw.encode('UTF-8', :invalid => :replace, :undef => :replace)
27
+
28
+ # for debugging
29
+ #now = Time.now.to_i.to_s # current time, unique temp file
30
+ #temp = File.join(Dir.pwd, "temp-scholar-rename-text-#{now}")
31
+ #system("pdftotext -q '#{@file}' '#{temp}'")
20
32
 
21
33
  # Choose pdf qualities
22
34
  s = Selector.new(content)
23
35
  s.select_all # choose props
24
36
  printf "Ok? [Yn]: "
25
- conf = STDIN.gets.chomp # confirm title
26
37
 
27
- begin # file read in from first ARGV above
38
+ begin
39
+ conf = STDIN.gets.chomp # confirm title
28
40
  File.rename(@file, s.title) unless conf.match(/^(n|N).*/)
29
- ensure # cleaning up @file cleanup from pdftotext
41
+ ensure
30
42
  puts "Cleaning up..."
31
- File.delete(temp)
43
+ #File.delete(temp)
32
44
  end
33
45
  end
34
46
  end
data/lib/selector.rb CHANGED
@@ -2,29 +2,40 @@
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
+
5
11
  class Selector
6
12
  attr_reader :title
7
13
 
8
14
  # take the first 10 lines of the pdftotext output and assign it to the
9
15
  # context class instance variable, use later when choosing selector
16
+ # https://stackoverflow.com/questions/9503554/
10
17
  def initialize(c)
11
- @content = c.split("\n")[0..14].reject {|x| x.length < 2 }
18
+ @content = c.split("\n")[0..14]
19
+ .reject {|x| x.length < 2 }
20
+ .map {|x| x[0..100] } # trim
12
21
  @fulltxt = c.split("\n")
13
22
  end
14
23
 
15
24
  def select_all
16
- puts "Select title line number:"
17
- title = choose @content
25
+ puts "Options:".bow
26
+ @content.each_with_index {|l, i| puts "#{i}\t#{l}" }
27
+ printf "Select title line number ".bow
28
+ title = choose(@content, print: false)
18
29
 
19
- puts "Select author line number:"
20
- authors = choose @content
30
+ printf "Select author line number ".bow
31
+ authors = choose(@content, print: false)
21
32
 
22
- puts "Select author form number:"
33
+ puts "Select author form:".bow
23
34
  author = gen_authors(authors)
24
35
 
25
36
  year = gen_year # just read it
26
37
 
27
- puts "Select desired title format:"
38
+ puts "Select desired title:".bow
28
39
  @title = choose(gen_forms year, title, author)
29
40
  end
30
41
 
@@ -33,11 +44,11 @@ class Selector
33
44
  # user selects back to the calling method. pretty sketchy way to interpret
34
45
  # the users input as a range or integer, but seems to be working for now.
35
46
  # 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}]: "
47
+ def choose(options, print: true)
48
+ options.each_with_index {|l, i| puts "#{i}\t#{l}" } if print
49
+ printf "[0 - #{options.length-1}]: ".bow
39
50
  line = STDIN.gets.chomp
40
- meta = "opts[#{line}]"
51
+ meta = "options[#{line}]"
41
52
  mout = eval meta # in theory terrible but this aint a rails app............
42
53
  if mout.is_a? (Array)
43
54
  mout.join ' '
@@ -68,6 +79,7 @@ class Selector
68
79
  au = a.upcase
69
80
  return [
70
81
  "#{y} #{a} #{t}.pdf",
82
+ "#{y} - #{a} - #{t}.pdf",
71
83
  "#{a}_#{y}_#{t}.pdf".gsub(" ", "_"),
72
84
  "#{au}_#{y}_#{t}.pdf".gsub(" ", "_"),
73
85
  "#{ad}_#{y}_#{t}.pdf".gsub(" ", "_"),
data/lib/version.rb CHANGED
@@ -1 +1,3 @@
1
- module SR; Version = "0.1.2"; end
1
+ module SR
2
+ Version = "0.1.4"
3
+ end
data/readme.md CHANGED
@@ -1,9 +1,25 @@
1
- # scholar-rename `interactive pdf-namer`
1
+ # scholar-rename
2
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
3
+ [![MIT](https://img.shields.io/npm/l/alt.svg?style=flat)](http://jeremywrnr.com/mit-license)
4
+ [![Gem Version](https://badge.fury.io/rb/scholar-rename.svg)](https://badge.fury.io/rb/scholar-rename)
7
5
 
8
- You need this: https://en.wikipedia.org/wiki/Pdftotext
6
+ an interactive pdf-renamer tool.
9
7
 
8
+ ## install
9
+
10
+ [sudo] gem install scholar-rename
11
+
12
+ you'll also need: https://en.wikipedia.org/wiki/Pdftotext
13
+
14
+ ## about
15
+
16
+ renames a pdf file to author-title-year.pdf or other formats based on your
17
+ selection. there isn't actually anything scholarly about this tool, though
18
+ academic people may find it useful for renaming pdfs that come in arbitrarily
19
+ named file formats. services like mendeley kind of make this not super
20
+ relevant. it helps when searching for a specific pdf and when labeling pdfs.
21
+
22
+ ## todo
23
+
24
+ - only show index lines once
25
+ - actually clean up tempfile
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scholar-rename
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Warner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-18 00:00:00.000000000 Z
11
+ date: 2018-01-22 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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -70,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
84
  version: '0'
71
85
  requirements: []
72
86
  rubyforge_project:
73
- rubygems_version: 2.6.11
87
+ rubygems_version: 2.6.14
74
88
  signing_key:
75
89
  specification_version: 4
76
90
  summary: Rename pdfs based on author/title/year.