pdfh 0.2.1 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a3761420b1befc7788493ed341094bffcf3b6f86bc2f6685f7b4de0a8201752b
4
- data.tar.gz: 9da09a68dff012c3e22e67581c73b1bf874ed3abd0f9d58be8c760fa37ca6561
3
+ metadata.gz: e56c6dcb8915785a475e34a5120d44556fd6630d3738849161697b76ded58fa6
4
+ data.tar.gz: 335b684595f23f61607d3ff3e0c6ed106da1e52842090c4ddc89381252f5771a
5
5
  SHA512:
6
- metadata.gz: 5a9ba91f43fcb3e96469bf1d4499ab9e857211553bebe4732e0d3d287e4fe7946fe635b7f6e345576d69da6b07573acce2794619098d62ae40309c7f3d170f2d
7
- data.tar.gz: 80a52f67d7c47159918b5cff641f5744022d1400bf71ac9fe6247814a0369e0a2ec8e2915b791a5e85e1107a28d9c64777471aa1ec448aa1ad093ecf83805287
6
+ metadata.gz: 45bbfee057aeda92595c687b67a3e2f34c8ed4f36f28dbc5b3880fafb05f7a9e170b70e1ec61bc21709ba7ed3a1401667fca03cadf35593fb8843b93ee7fdfdb
7
+ data.tar.gz: 930644f381d3e338ec3161d8346179f3268c336bf3407ae64ed256eb9c30b0bd2b0d0bcf6c0ab31c2d0a57a25d56f05fd0f99bf89f7978be77a0d78f985da101
data/.rspec CHANGED
@@ -1,3 +1,3 @@
1
- --format documentation
1
+ --format progress
2
2
  --color
3
3
  --require spec_helper
data/.rubocop.yml CHANGED
@@ -7,7 +7,7 @@ require:
7
7
 
8
8
  AllCops:
9
9
  NewCops: enable
10
- TargetRubyVersion: 2.5
10
+ TargetRubyVersion: 3.0
11
11
  Exclude:
12
12
  - doc/**/*
13
13
  - pkg/**/*
@@ -16,7 +16,7 @@ AllCops:
16
16
 
17
17
  Layout/LineLength:
18
18
  Max: 120
19
- IgnoredPatterns: ['^\s*#']
19
+ AllowedPatterns: ['^\s*#']
20
20
 
21
21
  Style/StringLiterals:
22
22
  Enabled: true
data/.rubocop_todo.yml CHANGED
@@ -1,36 +1,31 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2021-05-15 01:26:47 UTC using RuboCop version 1.14.0.
3
+ # on 2023-07-13 05:43:39 UTC using RuboCop version 1.54.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 5
10
- # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
9
+ # Offense count: 7
10
+ # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
11
11
  Metrics/AbcSize:
12
- Max: 24
13
-
14
- # Offense count: 6
15
- # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
16
- # IgnoredMethods: refine
17
- Metrics/BlockLength:
18
- Max: 87
12
+ Max: 25
19
13
 
20
14
  # Offense count: 1
21
- # Configuration parameters: CountComments, CountAsOne.
22
- Metrics/ClassLength:
23
- Max: 121
15
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
16
+ # AllowedMethods: refine
17
+ Metrics/BlockLength:
18
+ Max: 26
24
19
 
25
20
  # Offense count: 6
26
- # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
21
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
27
22
  Metrics/MethodLength:
28
23
  Max: 17
29
24
 
30
25
  # Offense count: 2
31
26
  RSpec/AnyInstance:
32
27
  Exclude:
33
- - 'spec/pdfh/document_processor_spec.rb'
28
+ - 'spec/pdfh/main_spec.rb'
34
29
 
35
30
  # Offense count: 1
36
31
  # Configuration parameters: CountAsOne.
@@ -41,7 +36,20 @@ RSpec/ExampleLength:
41
36
  RSpec/MultipleExpectations:
42
37
  Max: 2
43
38
 
39
+ # Offense count: 1
40
+ # Configuration parameters: AllowedPatterns.
41
+ # AllowedPatterns: ^expect_, ^assert_
42
+ RSpec/NoExpectationExample:
43
+ Exclude:
44
+ - 'spec/pdfh/pdf_handler_spec.rb'
45
+
44
46
  # Offense count: 1
45
47
  RSpec/SubjectStub:
46
48
  Exclude:
47
- - 'spec/pdfh/document_processor_spec.rb'
49
+ - 'spec/pdfh/main_spec.rb'
50
+
51
+ # Offense count: 3
52
+ # This cop supports safe autocorrection (--autocorrect).
53
+ Style/RedundantStringEscape:
54
+ Exclude:
55
+ - 'lib/pdfh/settings_template.rb'
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.3.0
data/CHANGELOG.md CHANGED
@@ -1,26 +1,31 @@
1
+ ## v3.0.0
2
+ * Migrate to `asdf` from `rvm`
3
+ * Upgrade old gems
4
+ * Bump to v3 (as this is project's third iteration)
5
+
1
6
  ## v0.2.0
2
- * Major gem refactoring
3
- * Changed setting `base_path` to `destination_base_path`
4
- * Add DocumentType listing option on executable file
5
- * Add process individual documents providing type and files
6
- ```bash
7
- pdfh -t document_type_id path/to_files.pdf
8
- ```
9
- * Add settings.yml template in order to create a sample file
7
+ * Major gem refactoring
8
+ * Change setting `base_path` to `destination_base_path`
9
+ * Add DocumentType listing option on executable file
10
+ * Add process individual documents providing type and files
11
+ ```bash
12
+ pdfh -t document_type_id path/to_files.pdf
13
+ ```
14
+ * Add `settings.yml` template in order to create a sample file
10
15
 
11
16
  ## v0.1.9
12
- * Add dependencies validation at run
17
+ * Add dependencies validation at run
13
18
 
14
19
  ## v0.1.5
15
- * Add print_cmd field in config file for information purposes
16
- * Settings now validates a no existing directory
17
- * Refactor for easier maintenance
20
+ * Add `print_cmd` field in config file for information purposes
21
+ * Settings now validates an unexisting directory
22
+ * Refactor for easier maintenance
18
23
 
19
24
  ## v0.1.4
20
- * Add titleize format when writing new file
25
+ * Add titleize format when writing new file
21
26
 
22
27
  ## v0.1.3
23
- * Fixed copy companion files, which was not copying the files.
28
+ * Fixed copy companion files, which was not copying the files.
24
29
 
25
30
  ## v0.1.2
26
- * Initial Release
31
+ * Initial Release
data/Gemfile CHANGED
@@ -7,14 +7,15 @@ gemspec
7
7
 
8
8
  gem "bundler", "~> 2.0"
9
9
  gem "code-scanning-rubocop", "~> 0.5"
10
+ gem "factory_bot", "~> 6.2"
10
11
  gem "pry", "~> 0.14"
11
12
  gem "rake", "~> 13.0"
12
- gem "rspec", "~> 3.9"
13
+ gem "rspec", "~> 3.12"
13
14
  gem "rspec_junit_formatter", "~> 0.4"
14
- gem "rubocop", "~> 1.0"
15
- gem "rubocop-performance", "~> 1.9"
15
+ gem "rubocop", "~> 1.50"
16
+ gem "rubocop-performance", "~> 1.18"
16
17
  gem "rubocop-rake", "~> 0.5"
17
18
  gem "rubocop-rspec", "~> 2.2"
18
- gem "simplecov", "~> 0.21"
19
+ gem "simplecov", "~> 0.22"
19
20
  gem "simplecov-console", "~> 0.9"
20
21
  gem "versionomy", "~> 0.5"
data/Gemfile.lock CHANGED
@@ -1,67 +1,102 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pdfh (0.2.1)
4
+ pdfh (3.0.1)
5
5
  colorize (~> 0.8.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
+ activesupport (7.1.2)
11
+ base64
12
+ bigdecimal
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ connection_pool (>= 2.2.5)
15
+ drb
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ mutex_m
19
+ tzinfo (~> 2.0)
10
20
  ansi (1.5.0)
11
21
  ast (2.4.2)
22
+ base64 (0.2.0)
23
+ bigdecimal (3.1.5)
12
24
  blockenspiel (0.5.0)
13
- code-scanning-rubocop (0.5.0)
25
+ code-scanning-rubocop (0.6.1)
14
26
  rubocop (~> 1.0)
15
27
  coderay (1.1.3)
16
28
  colorize (0.8.1)
29
+ concurrent-ruby (1.2.2)
30
+ connection_pool (2.4.1)
17
31
  diff-lcs (1.5.0)
18
32
  docile (1.4.0)
33
+ drb (2.2.0)
34
+ ruby2_keywords
35
+ factory_bot (6.4.5)
36
+ activesupport (>= 5.0.0)
37
+ i18n (1.14.1)
38
+ concurrent-ruby (~> 1.0)
39
+ json (2.7.1)
40
+ language_server-protocol (3.17.0.3)
19
41
  method_source (1.0.0)
20
- parallel (1.21.0)
21
- parser (3.1.0.0)
42
+ minitest (5.20.0)
43
+ mutex_m (0.2.0)
44
+ parallel (1.24.0)
45
+ parser (3.3.0.2)
22
46
  ast (~> 2.4.1)
23
- pry (0.14.1)
47
+ racc
48
+ pry (0.14.2)
24
49
  coderay (~> 1.1)
25
50
  method_source (~> 1.0)
26
- rainbow (3.0.0)
27
- rake (13.0.6)
28
- regexp_parser (2.2.0)
29
- rexml (3.2.5)
30
- rspec (3.10.0)
31
- rspec-core (~> 3.10.0)
32
- rspec-expectations (~> 3.10.0)
33
- rspec-mocks (~> 3.10.0)
34
- rspec-core (3.10.1)
35
- rspec-support (~> 3.10.0)
36
- rspec-expectations (3.10.1)
51
+ racc (1.7.3)
52
+ rainbow (3.1.1)
53
+ rake (13.1.0)
54
+ regexp_parser (2.9.0)
55
+ rexml (3.2.6)
56
+ rspec (3.12.0)
57
+ rspec-core (~> 3.12.0)
58
+ rspec-expectations (~> 3.12.0)
59
+ rspec-mocks (~> 3.12.0)
60
+ rspec-core (3.12.2)
61
+ rspec-support (~> 3.12.0)
62
+ rspec-expectations (3.12.3)
37
63
  diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.10.0)
39
- rspec-mocks (3.10.2)
64
+ rspec-support (~> 3.12.0)
65
+ rspec-mocks (3.12.6)
40
66
  diff-lcs (>= 1.2.0, < 2.0)
41
- rspec-support (~> 3.10.0)
42
- rspec-support (3.10.3)
43
- rspec_junit_formatter (0.5.1)
67
+ rspec-support (~> 3.12.0)
68
+ rspec-support (3.12.1)
69
+ rspec_junit_formatter (0.6.0)
44
70
  rspec-core (>= 2, < 4, != 2.12.0)
45
- rubocop (1.24.1)
71
+ rubocop (1.59.0)
72
+ json (~> 2.3)
73
+ language_server-protocol (>= 3.17.0)
46
74
  parallel (~> 1.10)
47
- parser (>= 3.0.0.0)
75
+ parser (>= 3.2.2.4)
48
76
  rainbow (>= 2.2.2, < 4.0)
49
77
  regexp_parser (>= 1.8, < 3.0)
50
- rexml
51
- rubocop-ast (>= 1.15.1, < 2.0)
78
+ rexml (>= 3.2.5, < 4.0)
79
+ rubocop-ast (>= 1.30.0, < 2.0)
52
80
  ruby-progressbar (~> 1.7)
53
- unicode-display_width (>= 1.4.0, < 3.0)
54
- rubocop-ast (1.15.1)
55
- parser (>= 3.0.1.1)
56
- rubocop-performance (1.13.1)
57
- rubocop (>= 1.7.0, < 2.0)
58
- rubocop-ast (>= 0.4.0)
81
+ unicode-display_width (>= 2.4.0, < 3.0)
82
+ rubocop-ast (1.30.0)
83
+ parser (>= 3.2.1.0)
84
+ rubocop-capybara (2.20.0)
85
+ rubocop (~> 1.41)
86
+ rubocop-factory_bot (2.25.1)
87
+ rubocop (~> 1.41)
88
+ rubocop-performance (1.20.2)
89
+ rubocop (>= 1.48.1, < 2.0)
90
+ rubocop-ast (>= 1.30.0, < 2.0)
59
91
  rubocop-rake (0.6.0)
60
92
  rubocop (~> 1.0)
61
- rubocop-rspec (2.7.0)
62
- rubocop (~> 1.19)
63
- ruby-progressbar (1.11.0)
64
- simplecov (0.21.2)
93
+ rubocop-rspec (2.26.1)
94
+ rubocop (~> 1.40)
95
+ rubocop-capybara (~> 2.17)
96
+ rubocop-factory_bot (~> 2.22)
97
+ ruby-progressbar (1.13.0)
98
+ ruby2_keywords (0.0.5)
99
+ simplecov (0.22.0)
65
100
  docile (~> 1.1)
66
101
  simplecov-html (~> 0.11)
67
102
  simplecov_json_formatter (~> 0.1)
@@ -70,10 +105,12 @@ GEM
70
105
  simplecov
71
106
  terminal-table
72
107
  simplecov-html (0.12.3)
73
- simplecov_json_formatter (0.1.3)
108
+ simplecov_json_formatter (0.1.4)
74
109
  terminal-table (3.0.2)
75
110
  unicode-display_width (>= 1.1.1, < 3)
76
- unicode-display_width (2.1.0)
111
+ tzinfo (2.0.6)
112
+ concurrent-ruby (~> 1.0)
113
+ unicode-display_width (2.5.0)
77
114
  versionomy (0.5.0)
78
115
  blockenspiel (~> 0.5)
79
116
 
@@ -83,18 +120,19 @@ PLATFORMS
83
120
  DEPENDENCIES
84
121
  bundler (~> 2.0)
85
122
  code-scanning-rubocop (~> 0.5)
123
+ factory_bot (~> 6.2)
86
124
  pdfh!
87
125
  pry (~> 0.14)
88
126
  rake (~> 13.0)
89
- rspec (~> 3.9)
127
+ rspec (~> 3.12)
90
128
  rspec_junit_formatter (~> 0.4)
91
- rubocop (~> 1.0)
92
- rubocop-performance (~> 1.9)
129
+ rubocop (~> 1.50)
130
+ rubocop-performance (~> 1.18)
93
131
  rubocop-rake (~> 0.5)
94
132
  rubocop-rspec (~> 2.2)
95
- simplecov (~> 0.21)
133
+ simplecov (~> 0.22)
96
134
  simplecov-console (~> 0.9)
97
135
  versionomy (~> 0.5)
98
136
 
99
137
  BUNDLED WITH
100
- 2.2.17
138
+ 2.5.4
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
1
  # PDF Handler (pdfh)
2
2
 
3
- [![Rubocop](https://github.com/iax7/pdfh/actions/workflows/rubocop-analysis.yml/badge.svg)](https://github.com/iax7/pdfh/actions/workflows/rubocop-analysis.yml)
4
- [![Ruby][ruby-badge]][ruby-url]
3
+ [![Rubocop][rubocop-img]][rubocop-url]
4
+ [![Ruby][ruby-img]][ruby-url]
5
+ [![Conventional Commits][cc-img]][cc-url]
6
+ [![Current version][gem-img]][gem-url]
5
7
 
6
- Examine all PDF files in Look up directories, remove password (if has one), rename and copy to a new directory using regular expresions.
8
+ Examine all PDF files in Look up directories, remove password (if has one), rename and copy to a new directory using regular expressions.
7
9
 
8
10
  ## Installation
9
11
 
@@ -13,11 +15,17 @@ gem install pdfh
13
15
 
14
16
  ### Dependencies
15
17
 
16
- You need to install pdf handling dependencies in order to use this gem. (I have only tested it on macOS)
18
+ You need to install pdf handling dependencies in order to use this gem.
17
19
 
20
+ #### macOS
18
21
  ```bash
19
- brew install qpdf
20
- brew install xpdf
22
+ brew install qpdf # for qpdf
23
+ brew install xpdf # for pdftotext
24
+ ```
25
+
26
+ #### Fedora
27
+ ```bash
28
+ sudo dnf install -y qpdf poppler-utils
21
29
  ```
22
30
 
23
31
  ## Usage
@@ -81,5 +89,11 @@ The gem is available as open source under the terms of the [MIT License](https:/
81
89
  Everyone interacting in the Pdfh project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/iax7/pdfh/blob/master/CODE_OF_CONDUCT.md).
82
90
 
83
91
  <!-- Links -->
84
- [ruby-badge]: https://img.shields.io/badge/ruby-3.1.0-blue?style=flat&logo=ruby&logoColor=CC342D&labelColor=white
92
+ [rubocop-img]: https://github.com/iax7/pdfh/actions/workflows/rubocop-analysis.yml/badge.svg
93
+ [rubocop-url]: https://github.com/iax7/pdfh/actions/workflows/rubocop-analysis.yml
94
+ [ruby-img]: https://img.shields.io/badge/ruby-3.1-blue?style=flat&logo=ruby&logoColor=CC342D&labelColor=white
85
95
  [ruby-url]: https://www.ruby-lang.org/en/
96
+ [cc-img]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=00&labelColor=fff
97
+ [cc-url]: https://conventionalcommits.org
98
+ [gem-img]: https://img.shields.io/gem/v/pdfh?labelColor=fff&label=version
99
+ [gem-url]: https://rubygems.org/gems/pdfh
data/bin/console CHANGED
@@ -10,7 +10,7 @@ require "pdfh"
10
10
  # (If you use this, don't forget to add pry to your Gemfile!)
11
11
  require "pry"
12
12
 
13
- p Pdfh.parse_argv
13
+ p Pdfh::OptParser.parse_argv
14
14
 
15
15
  Pry.start
16
16
 
data/bin/run CHANGED
@@ -5,4 +5,4 @@ require "bundler/setup"
5
5
  require "pdfh"
6
6
  require "pry"
7
7
 
8
- Pdfh::DocumentProcessor.new.start
8
+ Pdfh::Main.start
data/exe/pdfh CHANGED
@@ -20,7 +20,7 @@ end
20
20
  exit(1) unless validate_installed("qpdf", "pdftotext")
21
21
 
22
22
  begin
23
- Pdfh::DocumentProcessor.new.start
23
+ Pdfh::Main.start
24
24
  rescue StandardError => e
25
25
  Pdfh.error_print e.message
26
26
  end
data/lib/pdfh/main.rb ADDED
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pdfh
4
+ # Main functionality. This class is intended to manage the pdf documents
5
+ class Main
6
+ class << self
7
+ # @return [void]
8
+ def start
9
+ arg_options = Pdfh::OptParser.parse_argv
10
+ @options = Options.new(arg_options)
11
+
12
+ Pdfh.instance_variable_set(:@options, options)
13
+ Pdfh.instance_variable_set(:@console, Console.new(options.verbose?))
14
+ Pdfh.print_options(arg_options)
15
+
16
+ @settings = SettingsBuilder.build
17
+ Pdfh.debug "Destination path: #{settings.base_path.colorize(:light_blue)}"
18
+
19
+ options.file_mode? ? process_provided_files : process_lookup_dirs
20
+ rescue SettingsIOError => e
21
+ Pdfh.error_print(e.message, exit_app: false)
22
+ Pdfh.create_settings_file
23
+ exit(1)
24
+ rescue StandardError => e
25
+ Pdfh.error_print(e.message)
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :options, :settings
31
+
32
+ # @param [String] file_name
33
+ # @return [DocumentType]
34
+ def match_doc_type(file_name)
35
+ settings.document_types.each do |type|
36
+ match = type.re_file.match(file_name)
37
+ return type if match
38
+ end
39
+ nil
40
+ end
41
+
42
+ # @return [void]
43
+ def process_provided_files
44
+ type_id = options.type
45
+ raise ArgumentError, "No files provided to process #{type_id.inspect} type." unless options.files?
46
+
47
+ type = settings.document_type(type_id)
48
+ Pdfh.error_print "Type #{type_id.inspect} was not found." if type.nil?
49
+ options.files.each do |file|
50
+ next Pdfh.warn_print "File #{file.inspect} does not exist." unless File.exist?(file)
51
+ next Pdfh.warn_print "File #{file.inspect} is not a pdf." unless File.extname(file) == ".pdf"
52
+
53
+ PdfFileHandler.new(file, type).process_document(settings.base_path)
54
+ end
55
+ end
56
+
57
+ # @return [void]
58
+ def process_lookup_dirs
59
+ settings.lookup_dirs.each do |work_directory|
60
+ process_directory(work_directory)
61
+ end
62
+ end
63
+
64
+ # @param [String] work_directory
65
+ # @return [void]
66
+ def process_directory(work_directory)
67
+ Pdfh.headline(work_directory)
68
+ processed_count = 0
69
+ ignored_files = []
70
+ files = Dir["#{work_directory}/*.pdf"]
71
+ files.each do |pdf_file|
72
+ type = match_doc_type(pdf_file)
73
+ if type
74
+ processed_count += 1
75
+ PdfFileHandler.new(pdf_file, type).process_document(settings.base_path)
76
+ else
77
+ ignored_files << base_name_no_ext(pdf_file)
78
+ end
79
+ end
80
+ puts " (No files processed)".colorize(:light_black) if processed_count.zero?
81
+ return unless Pdfh.verbose?
82
+
83
+ puts "\n No document type found for these PDF files:" if ignored_files.any?
84
+ ignored_files.each.with_index(1) { |file, index| Pdfh.ident_print index, file, color: :magenta }
85
+ end
86
+
87
+ # @return [String]
88
+ def base_name_no_ext(file)
89
+ File.basename(file, File.extname(file))
90
+ end
91
+ end
92
+ end
93
+ end
@@ -3,27 +3,44 @@
3
3
  module Pdfh
4
4
  # Handles the PDF detected by the rules
5
5
  class Document
6
- attr_reader :text, :type, :file, :extra, :pdf_doc, :period
6
+ IDENT = 12
7
+
8
+ attr_reader :text, :type, :file, :extra, :period
7
9
 
8
10
  # @param file [String]
9
11
  # @param type [DocumentType]
12
+ # @param text [String]
10
13
  # @return [self]
11
- def initialize(file, type)
12
- raise IOError, "File #{file} not found" unless File.exist?(file)
13
-
14
+ def initialize(file, type, text)
14
15
  @file = file
15
16
  @type = type
16
- Pdfh.verbose_print "=== Document Type: #{type.name} =============================="
17
- @pdf_doc = PdfHandler.new(file, type.pwd)
18
- @text = @pdf_doc.extract_text
19
- Pdfh.verbose_print "~~~~~~~~~~~~~~~~~~ Finding a subtype"
17
+ Pdfh.debug "=== Document Type: #{type.name} =============================="
18
+ @text = text
19
+ Pdfh.debug "~~~~~~~~~~~~~~~~~~ Finding a subtype"
20
20
  @sub_type = type.sub_type(@text)
21
- Pdfh.verbose_print " SubType: #{@sub_type}"
21
+ Pdfh.debug " SubType: #{@sub_type}"
22
22
  @companion = search_companion_files
23
23
 
24
24
  month, year, @extra = match_data
25
25
  @period = DocumentPeriod.new(day: extra, month: month, month_offset: @sub_type&.month_offset, year: year)
26
- Pdfh.verbose_print " Period: #{@period.inspect}"
26
+ Pdfh.debug " Period: #{@period.inspect}"
27
+ end
28
+
29
+ # @return [void]
30
+ def print_info
31
+ print_info_line "Type", type.name
32
+ print_info_line "Sub-Type", sub_type
33
+ print_info_line "Period", period
34
+ print_info_line "New Name", new_name
35
+ print_info_line "Store Path", store_path
36
+ print_info_line "Extra files", companion_files(join: true)
37
+ print_info_line "Print CMD", print_cmd
38
+ print_info_line "Processed?", "No (in Dry mode)" if Pdfh.dry?
39
+ end
40
+
41
+ # @return [void]
42
+ def print_info_line(property, info)
43
+ Pdfh.ident_print property, info.to_s, color: :light_blue, width: IDENT
27
44
  end
28
45
 
29
46
  # @return [String]
@@ -100,13 +117,13 @@ module Pdfh
100
117
  # unnamed matches needs to be in order month, year
101
118
  # @return [Array] - format [month, year, day]
102
119
  def match_data
103
- Pdfh.verbose_print "~~~~~~~~~~~~~~~~~~ Match Data RegEx"
104
- Pdfh.verbose_print " Using regex: #{@type.re_date}"
105
- Pdfh.verbose_print " named: #{@type.re_date.named_captures}"
120
+ Pdfh.debug "~~~~~~~~~~~~~~~~~~ Match Data RegEx"
121
+ Pdfh.debug " Using regex: #{@type.re_date}"
122
+ Pdfh.debug " named: #{@type.re_date.named_captures}"
106
123
  matched = @type.re_date.match(@text)
107
124
  raise ReDateError unless matched
108
125
 
109
- Pdfh.verbose_print " captured: #{matched.captures}"
126
+ Pdfh.debug " captured: #{matched.captures}"
110
127
 
111
128
  return matched.captures.map(&:downcase) if @type.re_date.named_captures.empty?
112
129
 
@@ -116,12 +133,12 @@ module Pdfh
116
133
 
117
134
  # @return [Array]
118
135
  def search_companion_files
119
- Pdfh.verbose_print "~~~~~~~~~~~~~~~~~~ Searching Companion files"
120
- Pdfh.verbose_print " Searching on: #{home_dir.inspect}"
136
+ Pdfh.debug "~~~~~~~~~~~~~~~~~~ Searching Companion files"
137
+ Pdfh.debug " Searching on: #{home_dir.inspect}"
121
138
  Dir.chdir(home_dir) do
122
139
  files_matching = Dir["#{file_name_only}.*"]
123
140
  companion = files_matching.reject { |file| file.include? ".pdf" }
124
- Pdfh.verbose_print " Found: #{companion.inspect}"
141
+ Pdfh.debug " Found: #{companion.inspect}"
125
142
 
126
143
  companion
127
144
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "base64"
4
-
5
3
  module Pdfh
6
4
  DocumentSubType = Struct.new(:name, :month_offset, keyword_init: true)
7
5
 
@@ -13,10 +11,13 @@ module Pdfh
13
11
  self.name_template ||= "{original}"
14
12
  self.re_file = Regexp.new(re_file)
15
13
  self.re_date = Regexp.new(re_date)
16
- self.pwd = Base64.decode64(pwd) if pwd
17
14
  self.sub_types = extract_subtype(sub_types) if sub_types
18
15
  end
19
16
 
17
+ # removes special characters from string and replaces spaces with dashes
18
+ # @example usage
19
+ # "Test This?%&".gid
20
+ # # => "test-this"
20
21
  # @return [String]
21
22
  def gid
22
23
  name.downcase.gsub(/[^0-9A-Za-z\s]/, "").tr(" ", "-")
@@ -28,14 +29,26 @@ module Pdfh
28
29
  sub_types&.find { |st| /#{st.name}/i.match?(text) }
29
30
  end
30
31
 
32
+ # @return [String]
33
+ def password
34
+ return Base64.decode64(pwd) if base64?
35
+
36
+ pwd
37
+ end
38
+
31
39
  private
32
40
 
41
+ # @return [boolean]
42
+ def base64?
43
+ pwd.is_a?(String) && Base64.strict_encode64(Base64.decode64(pwd)) == pwd
44
+ end
45
+
33
46
  # @param sub_types [Array]
34
47
  # @return [DocumentSubType]
35
48
  def extract_subtype(sub_types)
36
49
  sub_types.map do |st|
37
- name = st["name"]
38
- offset = st["month_offset"].to_i
50
+ name = st[:name]
51
+ offset = st[:month_offset].to_i
39
52
  DocumentSubType.new(name: name, month_offset: offset)
40
53
  end
41
54
  end