class Selector

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.

Attributes

content[R]
metadata[R]
options[RW]
title[R]

Public Class Methods

new(c = "Test\nPDF\nContent", opts = { :format => 0, :auto => true }) click to toggle source
# 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

Public Instance Methods

choose(list, print: true) click to toggle source

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).

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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_year() click to toggle source

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

# 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
puts(*x) click to toggle source
# File lib/selector.rb, line 13
def puts(*x) x; end
select_all() click to toggle source
# 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) click to toggle source
# 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