Class: Renamer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Renamer

Returns a new instance of Renamer.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/renamer.rb', line 6

def initialize(*args)
  args = args.flatten
  a0 = args.first
  al = args.last

  @real_file = false
  @options = { :format => 0, :auto => false, :debug => false, :test => false }
  @version = SR::Version
  @selector = Selector.new
  @formats = @selector.gen_forms("Year", "Title", "Author")

  if args.include? "--debug"
    @options[:debug] = true
  end

  if args.include? "--test"
    @options[:test] = true
    def puts(*x) x; end
  end

  # Filename comes last.
  if !al.nil? && al[0] != "-"
    @file = File.join(Dir.pwd, args.last).to_s
    @real_file = @file && File.file?(@file)
  end
  if @file && !@real_file
    puts "Input #{@file} not found."
    puts "Please specify a pdf file to use with scholar-rename."
    exit 1
  end

  # Main argument processing.
  if args.length == 0 || a0 == "--h" || a0 == "--help"
    puts "usage: scholar-rename (--format #) (--auto) [file.pdf]"
    puts "\t--show-formats\tshow format options"
    puts "\t--auto\tpick default formatter"
    puts "\t-v, --version\tshow version number"
  elsif a0 == "-v" || a0 == "--version"
    puts @version
    exit 0 unless @options[:test]
  elsif !has_prereq?
    puts "please install pdftotext (via poppler) to use scholar-rename"
    puts "OSX: brew install pkg-config poppler"
    exit 1
  elsif a0 == "--show-formats"
    @formats.each_with_index { |x, i| puts "\t#{i}: #{x}" }
  elsif a0 == "--format"
    @options[:format] = args[1].to_i
  elsif a0 == "--auto"
    @options[:auto] = true
  end

  rename args if @real_file
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



4
5
6
# File 'lib/renamer.rb', line 4

def format
  @format
end

#formatsObject (readonly)

Returns the value of attribute formats.



4
5
6
# File 'lib/renamer.rb', line 4

def formats
  @formats
end

#selectorObject (readonly)

Returns the value of attribute selector.



4
5
6
# File 'lib/renamer.rb', line 4

def selector
  @selector
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/renamer.rb', line 4

def version
  @version
end

Instance Method Details

#has_prereq?Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/renamer.rb', line 61

def has_prereq?
  system("pdftotext -v 2> /dev/null")
  $?.success?
end

#rename(args) ⇒ Object



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

def rename(args)
  raw = `pdftotext -q '#{@file}' -` # may contain non-ascii characters
  content = raw.encode("UTF-8", :invalid => :replace, :undef => :replace)
  
  # Choose pdf qualities
  @selector.set_content(content)
  @selector.options = @options
  @selector.select_all
  md = @selector.

  if @options[:debug]
    puts md[:year] if args.include? "--show-year"
    puts md[:title] if args.include? "--show-title"
    puts md[:author] if args.include? "--show-author"
  else
    File.rename(@file, @selector.title)
    puts "Saved #{@selector.title}"
  end
end