readability-rb 0.5.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1b1f82a83ce6cdeb11e08496ffdac052e828ae70938e9febf1f054cf0fffff1
4
- data.tar.gz: 26b68d2cf32967ea3e0aaa486eb591639001831b461f2ad0b86015d26b9c41f7
3
+ metadata.gz: c451d1e4e2e12c168b323f813a36594c25177e7a8fa4c1999cd55fd652128413
4
+ data.tar.gz: be5c5fe5aef9cc1186c25112f6e3ca11220aec7b4cb122682092fe3db14c4008
5
5
  SHA512:
6
- metadata.gz: ccabf0a76b0f48e7047e97303e13314801159fa368329729ab34079f7f679ab92e86fe3a055a0251c3b4c13dd9028d447c3f560d4762e58b770f7853966f96f3
7
- data.tar.gz: 7ac4044347dc3f4efc7e24ce182a0b3cfd53504ebf9429a8ca6950f4a140663a3a5f84e19578692d0fc8ba1299bf730d9b1c127b14cc2d0b6bb266d1d5d39cf2
6
+ metadata.gz: 210bd7531212e4c9ad91ec6da99e3bd0a74eb1dd62040b646bc7e90a07daadc13e19c9681d2bd6c7e2412c2ef3f30752671b52472f22b26fbc263eaf2f473e64
7
+ data.tar.gz: 692bcc0189b89d1b440d7a5bf0cf4bd53a313ef30cebab92ca93d2c6f6d88a81fb531d1cabe85c07487748467e17a1e591b6f32237b5f01ed89eb14f7f5ea329
@@ -170,7 +170,7 @@ module Readability
170
170
  nxt = get_next_node(nxt)
171
171
  end
172
172
  end
173
- @article_byline = (item_prop_name_node || node).text.strip
173
+ @article_byline = js_trim((item_prop_name_node || node).text)
174
174
  node = remove_and_get_next(node)
175
175
  next
176
176
  end
@@ -41,7 +41,7 @@ module Readability
41
41
 
42
42
  if parsed.is_a?(Array)
43
43
  parsed = parsed.find do |it|
44
- Array(it["@type"]).any? { |t| t.match?(JSON_LD_ARTICLE_TYPES) }
44
+ Array(it["@type"]).any? { |t| t.is_a?(String) && t.match?(JSON_LD_ARTICLE_TYPES) }
45
45
  end
46
46
  next unless parsed
47
47
  end
@@ -64,6 +64,8 @@ module Readability
64
64
  end
65
65
  end
66
66
 
67
+ # Unlike JS, accept an array @type, which schema.org allows. JS calls
68
+ # @type.match, which throws for an array, so it skips the script.
67
69
  next if !parsed || !parsed["@type"] || !Array(parsed["@type"]).any? { |t| t.is_a?(String) && t.match?(JSON_LD_ARTICLE_TYPES) }
68
70
 
69
71
  metadata = {}
@@ -88,13 +90,13 @@ module Readability
88
90
 
89
91
  if parsed["author"]
90
92
  if parsed["author"].is_a?(Hash) && parsed["author"]["name"].is_a?(String)
91
- metadata["byline"] = parsed["author"]["name"].strip
93
+ metadata["byline"] = js_trim(parsed["author"]["name"])
92
94
  elsif parsed["author"].is_a?(Array) &&
93
- parsed["author"][0] &&
95
+ parsed["author"][0].is_a?(Hash) &&
94
96
  parsed["author"][0]["name"].is_a?(String)
95
97
  metadata["byline"] = parsed["author"]
96
- .select { |author| author && author["name"].is_a?(String) }
97
- .map { |author| author["name"].strip }
98
+ .select { |author| author.is_a?(Hash) && author["name"].is_a?(String) }
99
+ .map { |author| js_trim(author["name"]) }
98
100
  .join(", ")
99
101
  end
100
102
  end
@@ -110,8 +112,8 @@ module Readability
110
112
  if parsed["datePublished"].is_a?(String)
111
113
  metadata["date_published"] = parsed["datePublished"].strip
112
114
  end
113
- rescue JSON::ParserError => e
114
- # Handle malformed JSON gracefully
115
+ rescue => e
116
+ # Skip malformed JSON or JSON with an unexpected shape, as JS does
115
117
  log(e.message) if respond_to?(:log, true)
116
118
  end
117
119
  end
@@ -225,7 +227,7 @@ module Readability
225
227
  if matches
226
228
  # Convert to lowercase, and remove any whitespace
227
229
  name = matches[0].downcase.gsub(/\s/, "")
228
- values[name] = content.strip
230
+ values[name] = js_trim(content)
229
231
  end
230
232
  end
231
233
 
@@ -234,7 +236,7 @@ module Readability
234
236
  if content
235
237
  # Convert to lowercase, remove whitespace, convert dots to colons
236
238
  name = name.downcase.gsub(/\s/, "").gsub(".", ":")
237
- values[name] = content.strip
239
+ values[name] = js_trim(content)
238
240
  end
239
241
  end
240
242
  end
@@ -33,10 +33,10 @@ module Readability
33
33
  end
34
34
 
35
35
  # JS-compatible trim that also strips Unicode whitespace like \u00A0 (NBSP)
36
- # JS's String.prototype.trim() strips all Unicode whitespace including NBSP.
36
+ # JS's String.prototype.trim() strips all Unicode whitespace and \uFEFF (BOM).
37
37
  # Ruby's String#strip only removes ASCII whitespace.
38
38
  def js_trim(str)
39
- str.gsub(/\A[\s\u00A0]+|[\s\u00A0]+\z/, "")
39
+ str.gsub(/\A[[:space:]\uFEFF]+|[[:space:]\uFEFF]+\z/, "")
40
40
  end
41
41
 
42
42
  # Port of _getInnerText (JS line 2084)
@@ -211,9 +211,9 @@ module Readability
211
211
  end
212
212
 
213
213
  # Port of _isUrl (JS line 442)
214
+ # JS's URL constructor accepts any absolute URL, so any scheme counts.
214
215
  def is_url?(str)
215
- uri = URI.parse(str)
216
- uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
216
+ !URI.parse(str).scheme.nil?
217
217
  rescue URI::InvalidURIError, URI::InvalidComponentError
218
218
  false
219
219
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Readability
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: readability-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Croll
@@ -58,14 +58,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '3.2'
61
+ version: '3.3'
62
62
  required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  requirements:
64
64
  - - ">="
65
65
  - !ruby/object:Gem::Version
66
66
  version: '0'
67
67
  requirements: []
68
- rubygems_version: 3.6.9
68
+ rubygems_version: 4.0.20
69
69
  specification_version: 4
70
70
  summary: Extract readable article content from HTML pages
71
71
  test_files: []