burly 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/LICENSE +21 -0
- data/README.md +51 -0
- data/burly.gemspec +32 -0
- data/lib/burly/exceptions.rb +5 -0
- data/lib/burly/parser.rb +25 -0
- data/lib/burly/parsers/html_parser.rb +69 -0
- data/lib/burly/parsers/json_parser.rb +31 -0
- data/lib/burly/parsers/plaintext_parser.rb +19 -0
- data/lib/burly.rb +38 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5012d619b0f133bb3b826065339370fb825ad444bf832987bdb01485daa5c232
|
4
|
+
data.tar.gz: 74921dc5b75e56b3e38aec36b175176a790cbb81c5a8fc67e73f40f6f61ba505
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1d5c5f24ab94231093ebc53a9e2c47d595d1ff3a1a4810b8dc300467ae6475263e0f05a14f6f9051216d97dd77009018bb379d6519414603e254192bb09947f9
|
7
|
+
data.tar.gz: b0ad732e42aa2d84693eb727c4cca9e61fd85be214323fed5a79cc12663c953d5145a934fa3920fecd8b708a626e00322d1c60e84237fa60e335510a291039f9
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Jason Garber
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Burly
|
2
|
+
|
3
|
+
**A Ruby gem for extracting URLs from HTML, JSON, and plaintext documents.**
|
4
|
+
|
5
|
+
[](https://rubygems.org/gems/burly)
|
6
|
+
[](https://rubygems.org/gems/burly)
|
7
|
+
[](https://codeberg.org/jgarber/burly)
|
8
|
+
|
9
|
+
## Getting Started
|
10
|
+
|
11
|
+
Before installing and using Burly, you'll want to have [Ruby](https://www.ruby-lang.org) 2.6 (or newer) installed. Using a Ruby version managment tool like [rbenv](https://github.com/rbenv/rbenv), [chruby](https://github.com/postmodern/chruby), or [rvm](https://github.com/rvm/rvm) is recommended.
|
12
|
+
|
13
|
+
Burly is developed using Ruby 3.4 and is tested against additional Ruby versions using [Forgejo Actions](https://codeberg.org/jgarber/burly/actions).
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add Burly to your project's `Gemfile` and run `bundle install`:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
source "https://rubygems.org"
|
21
|
+
|
22
|
+
gem "burly"
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Using Burly to parse plaintext documents is as straightforward as:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Burly.parse(File.read("example.txt"))
|
31
|
+
```
|
32
|
+
|
33
|
+
Parsing JSON or HTML documents is only slightly more complicated:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
Burly.parse(File.read("example.json"), mime_type: "application/json")
|
37
|
+
|
38
|
+
Burly.parse(File.read("example.html", mime_type: "text/html"))
|
39
|
+
```
|
40
|
+
|
41
|
+
Burly uses _slightly_ different parsing rules for each supported MIME type:
|
42
|
+
|
43
|
+
- In plaintext documents, Burly extracts absolute URLs (e.g. `https://website.example`) from the document.
|
44
|
+
- In JSON documents, Burly extracts string values that _only_ contain absolute URLs (e.g. `{ "url": "https://website.example" }` and `{ "urls": ["https://website.example", "https://another-website.example] }`)
|
45
|
+
- In HTML documents, Burly extracts absolute and relative URLs from [URL attributes](https://html.spec.whatwg.org/#attributes-3) and [srcset attributes](https://html.spec.whatwg.org/#srcset-attributes).
|
46
|
+
|
47
|
+
In all cases, neither order nor uniqueness is guaranteed. You may also consider converting relative URLs extract from HTML documents to absolute URLs using the document's source URL and/or the `<base>` element's `href` attribute value (Ruby's [`URI.join` class method](https://docs.ruby-lang.org/en/master/URI.html#method-c-join) is good for this!).
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
Burly is freely available under the [MIT License](https://opensource.org/license/MIT).
|
data/burly.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.required_ruby_version = ">= 2.6"
|
5
|
+
|
6
|
+
spec.name = "burly"
|
7
|
+
spec.version = "0.1.0"
|
8
|
+
spec.authors = ["Jason Garber"]
|
9
|
+
spec.email = ["jason@sixtwothree.org"]
|
10
|
+
|
11
|
+
spec.summary = "Extract URLs from HTML, JSON, and plaintext documents."
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://codeberg.org/jgarber/burly"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = Dir["lib/**/*"].reject { |f| File.directory?(f) }
|
17
|
+
spec.files += ["LICENSE", "README.md"]
|
18
|
+
spec.files += ["burly.gemspec"]
|
19
|
+
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.metadata = {
|
23
|
+
"bug_tracker_uri" => "#{spec.homepage}/issues",
|
24
|
+
"changelog_uri" => "#{spec.homepage}/releases/tag/v#{spec.version}",
|
25
|
+
"documentation_uri" => "https://rubydoc.info/gems/#{spec.name}/#{spec.version}",
|
26
|
+
"homepage_uri" => spec.homepage,
|
27
|
+
"rubygems_mfa_required" => "true",
|
28
|
+
"source_code_uri" => "#{spec.homepage}/tree/v#{spec.version}",
|
29
|
+
}
|
30
|
+
|
31
|
+
spec.add_dependency "nokogiri", ">= 1.13"
|
32
|
+
end
|
data/lib/burly/parser.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Burly
|
4
|
+
# @api private
|
5
|
+
class Parser
|
6
|
+
URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::Generic::DEFAULT_PARSER
|
7
|
+
|
8
|
+
URI_REGEXP = URI_PARSER.make_regexp(["http", "https"])
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# @return [Array<String>]
|
12
|
+
attr_reader :mime_types
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param document [String]
|
16
|
+
def initialize(document)
|
17
|
+
@document = document
|
18
|
+
end
|
19
|
+
|
20
|
+
# @raise [NotImplementedError]
|
21
|
+
def parse
|
22
|
+
raise NotImplementedError
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Burly
|
4
|
+
module Parsers
|
5
|
+
# @api private
|
6
|
+
class HtmlParser < Parser
|
7
|
+
@mime_types = ["text/html"]
|
8
|
+
|
9
|
+
Burly.register_parser(self)
|
10
|
+
|
11
|
+
# A map of HTML +srcset+ attributes and their associated element names.
|
12
|
+
#
|
13
|
+
# @see https://html.spec.whatwg.org/#srcset-attributes
|
14
|
+
# @see https://html.spec.whatwg.org/#attributes-3
|
15
|
+
SRCSET_ATTRIBUTES_MAP = {
|
16
|
+
"imagesrcset" => ["link"],
|
17
|
+
"srcset" => ["img", "source"],
|
18
|
+
}.freeze
|
19
|
+
|
20
|
+
# A map of HTML URL attributes and their associated element names.
|
21
|
+
#
|
22
|
+
# @see https://html.spec.whatwg.org/#attributes-3
|
23
|
+
URL_ATTRIBUTES_MAP = {
|
24
|
+
"action" => ["form"],
|
25
|
+
"cite" => ["blockquote", "del", "ins", "q"],
|
26
|
+
"data" => ["object"],
|
27
|
+
"formaction" => ["button", "input"],
|
28
|
+
"href" => ["a", "area", "base", "link"],
|
29
|
+
"ping" => ["a", "area"],
|
30
|
+
"poster" => ["video"],
|
31
|
+
"src" => ["audio", "embed", "iframe", "img", "input", "script", "source", "track", "video"],
|
32
|
+
}.freeze
|
33
|
+
|
34
|
+
ATTRIBUTES_XPATHS =
|
35
|
+
URL_ATTRIBUTES_MAP.merge(SRCSET_ATTRIBUTES_MAP).flat_map do |attribute, names|
|
36
|
+
names.map { |name| "//#{name} / @#{attribute}" }
|
37
|
+
end
|
38
|
+
|
39
|
+
# Parse an HTML document for absolute or relative URLs.
|
40
|
+
#
|
41
|
+
# @return [Array<String>]
|
42
|
+
def parse
|
43
|
+
attr_nodes.flat_map do |attr_node|
|
44
|
+
if SRCSET_ATTRIBUTES_MAP.key?(attr_node.name)
|
45
|
+
urls_from_candidate_strings(attr_node.value.split(/\s*,\s*/))
|
46
|
+
else
|
47
|
+
attr_node.value.strip
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
# @return [Nokogiri::XML::NodeSet]
|
55
|
+
def attr_nodes
|
56
|
+
@attr_nodes ||= doc.xpath(*ATTRIBUTES_XPATHS)
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [Nokogiri::HTML5::Document]
|
60
|
+
def doc
|
61
|
+
@doc ||= Nokogiri.HTML5(@document)
|
62
|
+
end
|
63
|
+
|
64
|
+
def urls_from_candidate_strings(candidate_strings)
|
65
|
+
candidate_strings.flat_map { |candidate_string| candidate_string.strip.split(/\s|$/)[0] }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Burly
|
4
|
+
module Parsers
|
5
|
+
# @api private
|
6
|
+
class JsonParser < Parser
|
7
|
+
@mime_types = ["application/json"]
|
8
|
+
|
9
|
+
Burly.register_parser(self)
|
10
|
+
|
11
|
+
# Parse a JSON document for string values whose contents are absolute
|
12
|
+
# URLs.
|
13
|
+
#
|
14
|
+
# @return [Array<String>]
|
15
|
+
def parse
|
16
|
+
recursive_parse(JSON.parse(@document))
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def recursive_parse(*objs)
|
22
|
+
objs.flat_map do |obj|
|
23
|
+
return obj.flat_map { |value| recursive_parse(value) }.compact if obj.is_a?(Array)
|
24
|
+
return recursive_parse(obj.values) if obj.is_a?(Hash)
|
25
|
+
|
26
|
+
obj if obj.is_a?(String) && obj.match?(URI_REGEXP)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Burly
|
4
|
+
module Parsers
|
5
|
+
# @api private
|
6
|
+
class PlaintextParser < Parser
|
7
|
+
@mime_types = ["text/plain"]
|
8
|
+
|
9
|
+
Burly.register_parser(self)
|
10
|
+
|
11
|
+
# Parse a plaintext document for absolute URLs.
|
12
|
+
#
|
13
|
+
# @return [Array<String>]
|
14
|
+
def parse
|
15
|
+
URI_PARSER.extract(@document, ["http", "https"])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/burly.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "uri"
|
5
|
+
|
6
|
+
require "nokogiri"
|
7
|
+
|
8
|
+
require_relative "burly/exceptions"
|
9
|
+
require_relative "burly/parser"
|
10
|
+
|
11
|
+
module Burly
|
12
|
+
@registered_parsers = {}
|
13
|
+
|
14
|
+
class << self
|
15
|
+
# @api private
|
16
|
+
attr_reader :registered_parsers
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param document [String]
|
20
|
+
#
|
21
|
+
# @return [Array<String>]
|
22
|
+
def self.parse(document, mime_type: "text/plain")
|
23
|
+
parser = registered_parsers[mime_type]
|
24
|
+
|
25
|
+
raise UnsupportedMimeType unless parser
|
26
|
+
|
27
|
+
parser.new(document).parse
|
28
|
+
end
|
29
|
+
|
30
|
+
# @api private
|
31
|
+
def self.register_parser(klass)
|
32
|
+
klass.mime_types.each { |mime_type| @registered_parsers[mime_type] = klass }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
require_relative "burly/parsers/html_parser"
|
37
|
+
require_relative "burly/parsers/json_parser"
|
38
|
+
require_relative "burly/parsers/plaintext_parser"
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: burly
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Garber
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: nokogiri
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '1.13'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '1.13'
|
26
|
+
description: Extract URLs from HTML, JSON, and plaintext documents.
|
27
|
+
email:
|
28
|
+
- jason@sixtwothree.org
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
35
|
+
- burly.gemspec
|
36
|
+
- lib/burly.rb
|
37
|
+
- lib/burly/exceptions.rb
|
38
|
+
- lib/burly/parser.rb
|
39
|
+
- lib/burly/parsers/html_parser.rb
|
40
|
+
- lib/burly/parsers/json_parser.rb
|
41
|
+
- lib/burly/parsers/plaintext_parser.rb
|
42
|
+
homepage: https://codeberg.org/jgarber/burly
|
43
|
+
licenses:
|
44
|
+
- MIT
|
45
|
+
metadata:
|
46
|
+
bug_tracker_uri: https://codeberg.org/jgarber/burly/issues
|
47
|
+
changelog_uri: https://codeberg.org/jgarber/burly/releases/tag/v0.1.0
|
48
|
+
documentation_uri: https://rubydoc.info/gems/burly/0.1.0
|
49
|
+
homepage_uri: https://codeberg.org/jgarber/burly
|
50
|
+
rubygems_mfa_required: 'true'
|
51
|
+
source_code_uri: https://codeberg.org/jgarber/burly/tree/v0.1.0
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '2.6'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.6.8
|
67
|
+
specification_version: 4
|
68
|
+
summary: Extract URLs from HTML, JSON, and plaintext documents.
|
69
|
+
test_files: []
|