Class: Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/selector.rb

Overview

Class for choosing and displaying the information from the pdf. I wonder if this could be augmented with information from google scholar somehow, there is probably a ruby (or at least python) api.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(c = "Test\nPDF\nContent", opts = { :format => 0, :auto => true }) ⇒ Selector

Returns a new instance of Selector.



9
10
11
12
13
14
15
# File 'lib/selector.rb', line 9

def initialize(c = "Test\nPDF\nContent", opts = { :format => 0, :auto => true })
  set_content(c)
  @options = opts
  if opts[:test]
    def puts(*x) x; end
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/selector.rb', line 6

def content
  @content
end

#metadataObject (readonly)

Returns the value of attribute metadata.



6
7
8
# File 'lib/selector.rb', line 6

def 
  @metadata
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/selector.rb', line 7

def options
  @options
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/selector.rb', line 6

def title
  @title
end

Instance Method Details

#choose(list, print: true) ⇒ Object

Pass in an array to list and be selected, and return the element that the user selects back to the calling method. this is a way to interpret the users input as a range (e.g., 0..2) or an integer (1).



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/selector.rb', line 65

def choose(list, print: true)
  raise "List is empty." if list.empty?

  if @options[:auto]
    line = 0
  else
    # Never print when --auto.
    if print
      list.each_with_index { |l, i| puts "#{i}\t#{l}" }
      printf "[0 - #{options.length - 1}]: "
    end
    line = STDIN.gets.chomp || 0
  end

  meta = "list[#{line}]"
  mout = eval meta
  mout = mout.join " " if mout.is_a? (Array)
  mout
end

#gen_authors(aline) ⇒ Object

Generate different forms for author selection, enumerating the different author that you want to save the file as. Split based on a comma.



87
88
89
90
91
92
93
94
95
# File 'lib/selector.rb', line 87

def gen_authors(aline)
  lines = aline.split(", ").map { |a| a.sub(/\d$/, "") } # delete ref number.
  if lines.is_a?(String)
    aline
  else # its an array, augment w/ lname and choose
    alines = lines.map { |a| a.split.last } + lines
    choose alines
  end
end

#gen_forms(y, t, a) ⇒ Object

based on the collected information, generate different forms of the title.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/selector.rb', line 47

def gen_forms(y, t, a)
  ad = a.downcase
  au = a.upcase
  return [
           "#{y} - #{a} - #{t}.pdf",
           "#{y} #{a} #{t}.pdf",
           "#{a}_#{y}_#{t}.pdf".gsub(" ", "_"),
           "#{au}_#{y}_#{t}.pdf".gsub(" ", "_"),
           "#{ad}_#{y}_#{t}.pdf".gsub(" ", "_"),
           "#{a} #{y} #{t}.pdf",
           "#{au} #{y} #{t}.pdf",
           "#{ad} #{y} #{t}.pdf",
         ]
end

#gen_yearObject

Parse out a year from a string, for each line of the document until found. Then clean it up and return it.



99
100
101
102
103
104
105
106
# File 'lib/selector.rb', line 99

def gen_year
  @full_text.each do |l| # find year
    lm = l.match(/(19|20)\d\d/)
    return lm[0] if !lm.nil?
  end

  Time.now.year.to_s # not matched, just return current year
end

#select_allObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/selector.rb', line 24

def select_all
  if !@options[:auto]
    puts "Options:"
    @content.each_with_index { |l, i| puts "#{i}\t#{l}" }
  end
  printf "Select title line number " unless @options[:auto]
  title = choose(@content, print: false)

  printf "Select author line number " unless @options[:auto]
  authors = choose(@content, print: false)

  puts "Select author form:" unless @options[:auto]
  author = gen_authors(authors)

  # Automatically match.
  year = gen_year
  
  forms = gen_forms(year, title, author)
  @metadata = {:year => year, :title => title, :author => author}
  @title = forms[@options[:format]]
end

#set_content(str) ⇒ Object



17
18
19
20
21
22
# File 'lib/selector.rb', line 17

def set_content(str)
  @full_text = str.split("\n")
  @content = @full_text[0..14]
    .reject { |x| x.length < 2 }
    .map { |x| x[0..100] } # trim
end