pdf_links_checker 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +16 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/exe/pdf_links_checker +18 -0
- data/lib/pdf_links_checker.rb +8 -0
- data/lib/pdf_links_checker/annotations.rb +27 -0
- data/lib/pdf_links_checker/links.rb +24 -0
- data/lib/pdf_links_checker/page.rb +25 -0
- data/lib/pdf_links_checker/pdf_document.rb +25 -0
- data/lib/pdf_links_checker/url.rb +27 -0
- data/lib/pdf_links_checker/version.rb +5 -0
- data/pdf_links_checker.gemspec +29 -0
- metadata +66 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 264e2db0471dc175e0f036a4a6b6ac8d4c1cfbde048ca5c68c25627157d55a58
|
|
4
|
+
data.tar.gz: 590bb293d281298ba78bfc68fb19af7619086036a6f0ef577516b141bafcc835
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3c0cb19ffa322a61f780efdbf3c53bfb9e208fcde5088659bf43719ca3c1eeadd39a83155913ddc8df057c2f33f963308980c8c866452e1e72829ee6658b24c0
|
|
7
|
+
data.tar.gz: 56397078ac6a86ed07bbbef086c315fd7f713e013e7e5f9918ca0561e56b351abeb82e390e7285a3c77c7c8cb2a26234ea6601b5ebf4a07584ee4bd219f27718
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pdf_links_checker
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby-2.7.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Andy Wendt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# PdfLinksChecker
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.org/AndyWendt/pdf_links_checker)
|
|
4
|
+
|
|
5
|
+
TODO: describe
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'pdf_links_checker'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle install
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install pdf_links_checker
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pdf_links_checker.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rake/testtask"
|
|
5
|
+
|
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
|
7
|
+
t.libs << "test"
|
|
8
|
+
t.libs << "lib"
|
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
require "rubocop/rake_task"
|
|
13
|
+
|
|
14
|
+
RuboCop::RakeTask.new
|
|
15
|
+
|
|
16
|
+
task default: %i[test rubocop]
|
data/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "pdf_links_checker"
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require "irb"
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "pdf_links_checker"
|
|
5
|
+
require "pdf_links_checker/pdf_document"
|
|
6
|
+
require "pdf-reader"
|
|
7
|
+
require "net/http"
|
|
8
|
+
|
|
9
|
+
file_name = ARGV[0]
|
|
10
|
+
file_path = "#{Dir.pwd}/#{file_name}"
|
|
11
|
+
|
|
12
|
+
path = File.expand_path(file_path)
|
|
13
|
+
doc = PDF::Reader.new(path)
|
|
14
|
+
|
|
15
|
+
links = PdfLinksChecker::PdfDocument.new(doc).links
|
|
16
|
+
|
|
17
|
+
puts "Working links: #{links.valid_count} of #{links.total_count}"
|
|
18
|
+
links.invalid.each { |invalid_link| puts invalid_link }
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module PdfLinksChecker
|
|
2
|
+
class Annotations
|
|
3
|
+
def initialize(pdf_doc)
|
|
4
|
+
@objects = pdf_doc.objects
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def find(references)
|
|
8
|
+
lookup_all(references)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def lookup_all(refs)
|
|
12
|
+
refs = *refs
|
|
13
|
+
refs.map { |ref| lookup(ref) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def lookup(ref)
|
|
17
|
+
object = objects[ref]
|
|
18
|
+
return object unless object.is_a?(Array)
|
|
19
|
+
|
|
20
|
+
lookup_all(object)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
attr_reader :objects
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require "pdf_links_checker/url"
|
|
2
|
+
|
|
3
|
+
module PdfLinksChecker
|
|
4
|
+
class Links
|
|
5
|
+
attr_reader :invalid
|
|
6
|
+
|
|
7
|
+
def initialize(links_array)
|
|
8
|
+
@urls= links_array.map { |link| URL.new(link) }
|
|
9
|
+
@invalid = @urls.select { |url| url.invalid? }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def total_count
|
|
13
|
+
urls.count
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def valid_count
|
|
17
|
+
total_count - invalid.count
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
attr_reader :urls
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module PdfLinksChecker
|
|
2
|
+
class Page
|
|
3
|
+
def initialize(pdf_page, annotations)
|
|
4
|
+
@pdf_page = pdf_page
|
|
5
|
+
@annotations = annotations
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def links
|
|
9
|
+
find_links
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def find_links
|
|
15
|
+
references = (pdf_page.attributes[:Annots] || [])
|
|
16
|
+
annotations.find(references).flatten.select{ |object| link?(object) }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def link?(object)
|
|
20
|
+
object[:Type] == :Annot && [:Link].include?(object[:Subtype])
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attr_reader :pdf_page, :annotations
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require "pdf_links_checker/annotations"
|
|
2
|
+
require "pdf_links_checker/links"
|
|
3
|
+
require "pdf_links_checker/page"
|
|
4
|
+
|
|
5
|
+
module PdfLinksChecker
|
|
6
|
+
class PdfDocument
|
|
7
|
+
def initialize(pdf_doc)
|
|
8
|
+
@pdf_doc = pdf_doc
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def links
|
|
12
|
+
pages
|
|
13
|
+
.reduce([]) { |links, page| links.concat(page.links) }
|
|
14
|
+
.then { |links| Links.new(links) }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def pages
|
|
20
|
+
pdf_doc.pages.map { |page| Page.new(page, Annotations.new(pdf_doc)) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
attr_reader :pdf_doc
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class URL
|
|
2
|
+
def initialize(pdf_link)
|
|
3
|
+
@pdf_link = pdf_link
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def invalid?
|
|
7
|
+
uri = to_s
|
|
8
|
+
url = URI.parse(uri)
|
|
9
|
+
req = Net::HTTP.new(url.host, url.port)
|
|
10
|
+
req.use_ssl = true if uri.include?("https")
|
|
11
|
+
|
|
12
|
+
begin
|
|
13
|
+
res = req.request_head(url.path)
|
|
14
|
+
true if res.code == "404"
|
|
15
|
+
rescue StandardError => e
|
|
16
|
+
true
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def to_s
|
|
21
|
+
pdf_link[:A][:URI]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
attr_reader :pdf_link
|
|
27
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/pdf_links_checker/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "pdf_links_checker"
|
|
7
|
+
spec.version = PdfLinksChecker::VERSION
|
|
8
|
+
spec.authors = ["Andy Wendt"]
|
|
9
|
+
spec.email = ["andy@awendt.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "A utility to check links in a PDF"
|
|
12
|
+
spec.description = "Ensures links in PDFs are accessible"
|
|
13
|
+
spec.homepage = "https://github.com/AndyWendt/pdf_links_checker"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
|
16
|
+
|
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/AndyWendt/pdf_links_checker"
|
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/AndyWendt/pdf_links_checker"
|
|
20
|
+
|
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
|
25
|
+
end
|
|
26
|
+
spec.bindir = "exe"
|
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
28
|
+
spec.require_paths = ["lib"]
|
|
29
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pdf_links_checker
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andy Wendt
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-12-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Ensures links in PDFs are accessible
|
|
14
|
+
email:
|
|
15
|
+
- andy@awendt.com
|
|
16
|
+
executables:
|
|
17
|
+
- pdf_links_checker
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- ".gitignore"
|
|
22
|
+
- ".ruby-gemset"
|
|
23
|
+
- ".ruby-version"
|
|
24
|
+
- ".travis.yml"
|
|
25
|
+
- Gemfile
|
|
26
|
+
- LICENSE.txt
|
|
27
|
+
- README.md
|
|
28
|
+
- Rakefile
|
|
29
|
+
- bin/console
|
|
30
|
+
- bin/setup
|
|
31
|
+
- exe/pdf_links_checker
|
|
32
|
+
- lib/pdf_links_checker.rb
|
|
33
|
+
- lib/pdf_links_checker/annotations.rb
|
|
34
|
+
- lib/pdf_links_checker/links.rb
|
|
35
|
+
- lib/pdf_links_checker/page.rb
|
|
36
|
+
- lib/pdf_links_checker/pdf_document.rb
|
|
37
|
+
- lib/pdf_links_checker/url.rb
|
|
38
|
+
- lib/pdf_links_checker/version.rb
|
|
39
|
+
- pdf_links_checker.gemspec
|
|
40
|
+
homepage: https://github.com/AndyWendt/pdf_links_checker
|
|
41
|
+
licenses:
|
|
42
|
+
- MIT
|
|
43
|
+
metadata:
|
|
44
|
+
homepage_uri: https://github.com/AndyWendt/pdf_links_checker
|
|
45
|
+
source_code_uri: https://github.com/AndyWendt/pdf_links_checker
|
|
46
|
+
changelog_uri: https://github.com/AndyWendt/pdf_links_checker
|
|
47
|
+
post_install_message:
|
|
48
|
+
rdoc_options: []
|
|
49
|
+
require_paths:
|
|
50
|
+
- lib
|
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: 2.7.0
|
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
requirements: []
|
|
62
|
+
rubygems_version: 3.1.2
|
|
63
|
+
signing_key:
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: A utility to check links in a PDF
|
|
66
|
+
test_files: []
|