Class: Renamer
- Inherits:
-
Object
- Object
- Renamer
- Defined in:
- lib/renamer.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#formats ⇒ Object
readonly
Returns the value of attribute formats.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Renamer
constructor
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity Legacy CLI arg-parser predating this cleanup pass; splitting it apart is a real refactor, not a lint fix, so it's exempted rather than restructured here.
-
#prereq? ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity.
- #rename(args) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Renamer
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity Legacy CLI arg-parser predating this cleanup pass; splitting it apart is a real refactor, not a lint fix, so it's exempted rather than restructured here.
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 60 61 62 63 64 65 66 |
# File 'lib/renamer.rb', line 12 def initialize(*args) args = args.flatten a0 = args.first al = args.last @real_file = false @options = { format: 0, auto: false, debug: false, test: false, no_lookup: false } @version = SR::VERSION @selector = Selector.new @formats = @selector.gen_forms('Year', 'Title', 'Author') @options[:debug] = true if args.include? '--debug' if args.include? '--test' @options[:test] = true def puts(*x) = x end @options[:no_lookup] = true if args.include? '--no-lookup' @options[:auto] = true if args.include? '--auto' @options[:format] = args[args.index('--format') + 1].to_i if args.include? '--format' # 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.empty? || a0 == '--h' || a0 == '--help' puts 'usage: scholar-rename (--format #) (--auto) (--no-lookup) [file.pdf]' puts "\t--show-formats\tshow format options" puts "\t--auto\tpick default formatter" puts "\t--no-lookup\tdisable Semantic Scholar metadata lookup" puts "\t-v, --version\tshow version number" elsif ['-v', '--version'].include?(a0) puts @version exit 0 unless @options[:test] elsif !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}" } end rename args if @real_file end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
7 8 9 |
# File 'lib/renamer.rb', line 7 def format @format end |
#formats ⇒ Object (readonly)
Returns the value of attribute formats.
7 8 9 |
# File 'lib/renamer.rb', line 7 def formats @formats end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
7 8 9 |
# File 'lib/renamer.rb', line 7 def selector @selector end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
7 8 9 |
# File 'lib/renamer.rb', line 7 def version @version end |
Instance Method Details
#prereq? ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
69 70 71 72 |
# File 'lib/renamer.rb', line 69 def prereq? system('pdftotext -v 2> /dev/null') $CHILD_STATUS.success? end |
#rename(args) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/renamer.rb', line 74 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.content = content @selector. = @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 |