readability-rb 0.9.0 → 0.9.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: 2346d2fb227e1e0e43ccb9ac2402701831df9d73e4dc875c0318d6faf0111477
4
- data.tar.gz: 374f9b5cb46a851e6309a5b96ae1ae1695cc6c4fe79290a3b3fc2e793f2902c0
3
+ metadata.gz: 4571b7715f1e0980abd507f1420e793de091611a46ea3e10db606b19f2bc2f7b
4
+ data.tar.gz: e7e2426c6394d76e225d978db5877b56ab28c536c6ff6a0bcbd694bde66b61d3
5
5
  SHA512:
6
- metadata.gz: d3c9af48116272e35456017965f7620805163d2b795178ebc0cde82bcea418024cbff08b1ed48d5b408dee829f75dfb536288fc6b5d142fd7527e38f5bcaed59
7
- data.tar.gz: e1102be75b15b30f39291b15ebbb2250676aeb9e3df6f6c5e4a8817be89f665f3571e707d66f6984fc3fbb80638c24ef27cee1408f1aaee99e7ca6f6fca3c898
6
+ metadata.gz: 9fdbe2a9504d10a74ef1f25028b2d89640ad43b52e42449a11af1619cea5f58ba064f47c636c6f49eaf989eee099442fcbc7a85e3fb783106f70a7ea4822f2a5
7
+ data.tar.gz: 3df7a29716c46345a7611534b18e6253014995ce808cb5ff429a185c50ae379b84d54b7e52600f769e02b194db3be54ff8872eccc244b5fff776a1c7dc460135
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Readability
4
- VERSION = "0.9.0"
4
+ VERSION = "0.9.1"
5
5
  end
data/lib/readability.rb CHANGED
@@ -19,12 +19,23 @@ module Readability
19
19
  DEFAULT_MAX_TREE_DEPTH = 1000
20
20
 
21
21
  def self.parse(html, url: nil, max_attributes: DEFAULT_MAX_ATTRIBUTES, max_tree_depth: DEFAULT_MAX_TREE_DEPTH, **options)
22
- doc = Nokogiri::HTML5(html, max_attributes: max_attributes, max_tree_depth: max_tree_depth)
22
+ doc = parse_html(html, max_attributes: max_attributes, max_tree_depth: max_tree_depth)
23
23
  Document.new(doc, url: url, **options).parse
24
24
  end
25
25
 
26
26
  def self.readerable?(html, max_attributes: DEFAULT_MAX_ATTRIBUTES, max_tree_depth: DEFAULT_MAX_TREE_DEPTH, **options)
27
- doc = Nokogiri::HTML5(html, max_attributes: max_attributes, max_tree_depth: max_tree_depth)
27
+ doc = parse_html(html, max_attributes: max_attributes, max_tree_depth: max_tree_depth)
28
28
  Readerable.probably_readerable?(doc, **options)
29
29
  end
30
+
31
+ # Nokogiri's HTML5 parser converts the page to UTF-8 strictly, so a byte the page's
32
+ # charset doesn't define raises. Browsers replace such bytes with U+FFFD, so convert
33
+ # the page that way, in the charset Nokogiri chose, and parse it again.
34
+ def self.parse_html(html, **limits)
35
+ Nokogiri::HTML5(html, **limits)
36
+ rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError => e
37
+ utf8 = html.dup.force_encoding(e.source_encoding).encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
38
+ Nokogiri::HTML5(utf8, **limits)
39
+ end
40
+ private_class_method :parse_html
30
41
  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.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Croll