readability-rb 0.9.1 → 0.10.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 +4 -4
- data/lib/readability/cleaner.rb +24 -30
- data/lib/readability/document.rb +23 -37
- data/lib/readability/readerable.rb +2 -1
- data/lib/readability/regexps.rb +11 -1
- data/lib/readability/scoring.rb +6 -6
- data/lib/readability/utils.rb +57 -19
- data/lib/readability/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cfe00eca1f970c106c2dfaa689dcc42ab22354c00afd0ba7007684e2ed34ef07
|
|
4
|
+
data.tar.gz: 2a5040fd348aa44e8436916525dab04be8485307d2c71550f9fba6de02fb132b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1696675db4eb241676ccf6348533c8ef19258d3575bb5d4404cf3ae8345e0955f7bab3c30c7299811bcfede26cc1bf3be127cbeed9c87061d3e5a3edb779f010
|
|
7
|
+
data.tar.gz: 0e8cfbb717d2e42bc881d8aabbcf7c920c24d496f293f09315f6228b0bd2841009654253916cb6028294dc1b962cb1a592c7bd2cfd2fad1cbb41143deb3cbbbd
|
data/lib/readability/cleaner.rb
CHANGED
|
@@ -231,7 +231,7 @@ module Readability
|
|
|
231
231
|
elem.remove_attribute("height")
|
|
232
232
|
end
|
|
233
233
|
|
|
234
|
-
cur = elem.
|
|
234
|
+
cur = elem.first_element_child
|
|
235
235
|
while cur
|
|
236
236
|
clean_styles(cur)
|
|
237
237
|
cur = cur.next_element
|
|
@@ -240,7 +240,7 @@ module Readability
|
|
|
240
240
|
|
|
241
241
|
# Port of _markDataTables (JS line 2297-2354)
|
|
242
242
|
def mark_data_tables(root)
|
|
243
|
-
tables = root
|
|
243
|
+
tables = css_query(root, "table")
|
|
244
244
|
tables.each do |table|
|
|
245
245
|
role = table["role"]
|
|
246
246
|
if role == "presentation"
|
|
@@ -258,21 +258,20 @@ module Readability
|
|
|
258
258
|
next
|
|
259
259
|
end
|
|
260
260
|
|
|
261
|
-
caption = table
|
|
261
|
+
caption = css_query(table, "caption").first
|
|
262
262
|
if caption && caption.children.length > 0
|
|
263
263
|
@data_tables.add(table)
|
|
264
264
|
next
|
|
265
265
|
end
|
|
266
266
|
|
|
267
267
|
# If the table has a descendant with any of these tags, consider a data table
|
|
268
|
-
|
|
269
|
-
if data_table_descendants.any? { |tag| table.at_css(tag) }
|
|
268
|
+
if css_query(table, "col, colgroup, tfoot, thead, th").first
|
|
270
269
|
@data_tables.add(table)
|
|
271
270
|
next
|
|
272
271
|
end
|
|
273
272
|
|
|
274
273
|
# Nested tables indicate a layout table
|
|
275
|
-
if table
|
|
274
|
+
if css_query(table, "table").first
|
|
276
275
|
next
|
|
277
276
|
end
|
|
278
277
|
|
|
@@ -296,13 +295,13 @@ module Readability
|
|
|
296
295
|
def get_row_and_column_count(table)
|
|
297
296
|
rows = 0
|
|
298
297
|
columns = 0
|
|
299
|
-
trs = table
|
|
298
|
+
trs = css_query(table, "tr")
|
|
300
299
|
trs.each do |tr|
|
|
301
300
|
rowspan = (tr["rowspan"] || 0).to_i
|
|
302
301
|
rows += (rowspan > 0 ? rowspan : 1)
|
|
303
302
|
|
|
304
303
|
columns_in_this_row = 0
|
|
305
|
-
cells = tr
|
|
304
|
+
cells = css_query(tr, "td")
|
|
306
305
|
cells.each do |cell|
|
|
307
306
|
colspan = (cell["colspan"] || 0).to_i
|
|
308
307
|
columns_in_this_row += (colspan > 0 ? colspan : 1)
|
|
@@ -410,17 +409,6 @@ module Readability
|
|
|
410
409
|
is_data_table = ->(t) { @data_tables.include?(t) }
|
|
411
410
|
|
|
412
411
|
remove_nodes(get_all_nodes_with_tag(elem, [tag])) do |node|
|
|
413
|
-
is_list = (tag == "ul" || tag == "ol")
|
|
414
|
-
|
|
415
|
-
unless is_list
|
|
416
|
-
list_length = 0
|
|
417
|
-
get_all_nodes_with_tag(node, ["ul", "ol"]).each do |list|
|
|
418
|
-
list_length += get_inner_text(list).length
|
|
419
|
-
end
|
|
420
|
-
node_text_length = get_inner_text(node).length
|
|
421
|
-
is_list = node_text_length > 0 && list_length.to_f / node_text_length > 0.9
|
|
422
|
-
end
|
|
423
|
-
|
|
424
412
|
# First check if this node IS a data table
|
|
425
413
|
if tag == "table" && is_data_table.call(node)
|
|
426
414
|
next false
|
|
@@ -436,7 +424,7 @@ module Readability
|
|
|
436
424
|
end
|
|
437
425
|
|
|
438
426
|
# Keep element if it contains a data table
|
|
439
|
-
if node
|
|
427
|
+
if css_query(node, "table").any? { |tbl| @data_tables.include?(tbl) }
|
|
440
428
|
next false
|
|
441
429
|
end
|
|
442
430
|
|
|
@@ -448,15 +436,25 @@ module Readability
|
|
|
448
436
|
end
|
|
449
437
|
|
|
450
438
|
inner_text = get_inner_text(node)
|
|
439
|
+
content_length = inner_text.length
|
|
440
|
+
|
|
441
|
+
is_list = (tag == "ul" || tag == "ol")
|
|
442
|
+
unless is_list
|
|
443
|
+
list_length = 0
|
|
444
|
+
get_all_nodes_with_tag(node, ["ul", "ol"]).each do |list|
|
|
445
|
+
list_length += inner_text_length(list)
|
|
446
|
+
end
|
|
447
|
+
is_list = content_length > 0 && list_length.to_f / content_length > 0.9
|
|
448
|
+
end
|
|
451
449
|
|
|
452
450
|
if inner_text.count(",") < 10
|
|
453
451
|
tag_counts = Hash.new(0)
|
|
454
|
-
node
|
|
452
|
+
css_query(node, "p, img, li, input").each { |n| tag_counts[n.name] += 1 }
|
|
455
453
|
p_count = tag_counts["p"]
|
|
456
454
|
img_count = tag_counts["img"]
|
|
457
455
|
li_count = tag_counts["li"] - 100
|
|
458
456
|
input_count = tag_counts["input"]
|
|
459
|
-
heading_density = get_text_density(node,
|
|
457
|
+
heading_density = get_text_density(node, HEADING_TAGS, text_length: content_length)
|
|
460
458
|
|
|
461
459
|
embed_count = 0
|
|
462
460
|
embeds = get_all_nodes_with_tag(node, ["object", "embed", "iframe"])
|
|
@@ -486,10 +484,8 @@ module Readability
|
|
|
486
484
|
next true
|
|
487
485
|
end
|
|
488
486
|
|
|
489
|
-
content_length = inner_text.length
|
|
490
487
|
link_density = get_link_density(node, text_length: content_length)
|
|
491
|
-
|
|
492
|
-
text_density = get_text_density(node, textish_tags)
|
|
488
|
+
text_density = get_text_density(node, TEXTISH_TAGS, text_length: content_length)
|
|
493
489
|
is_figure_child = has_ancestor_tag?(node, "figure")
|
|
494
490
|
|
|
495
491
|
# Apply shadiness checks
|
|
@@ -535,7 +531,7 @@ module Readability
|
|
|
535
531
|
end
|
|
536
532
|
|
|
537
533
|
if all_single_child
|
|
538
|
-
li_total = node
|
|
534
|
+
li_total = css_query(node, "li").length
|
|
539
535
|
have_to_remove = false if img_count == li_total
|
|
540
536
|
end
|
|
541
537
|
end
|
|
@@ -660,9 +656,7 @@ module Readability
|
|
|
660
656
|
link.replace(text_node)
|
|
661
657
|
else
|
|
662
658
|
container = Nokogiri::XML::Node.new("span", @doc)
|
|
663
|
-
|
|
664
|
-
container.add_child(link.children.first)
|
|
665
|
-
end
|
|
659
|
+
container.add_child(link.children)
|
|
666
660
|
link.replace(container)
|
|
667
661
|
end
|
|
668
662
|
else
|
|
@@ -734,7 +728,7 @@ module Readability
|
|
|
734
728
|
node["class"] = class_name
|
|
735
729
|
end
|
|
736
730
|
|
|
737
|
-
child = node.
|
|
731
|
+
child = node.first_element_child
|
|
738
732
|
while child
|
|
739
733
|
clean_classes(child)
|
|
740
734
|
child = child.next_element
|
data/lib/readability/document.rb
CHANGED
|
@@ -21,6 +21,7 @@ module Readability
|
|
|
21
21
|
@metadata = {}
|
|
22
22
|
@candidates = {}
|
|
23
23
|
@data_tables = Set.new
|
|
24
|
+
@xpath_cache = {}
|
|
24
25
|
|
|
25
26
|
# Options — from JS constructor lines 49-66
|
|
26
27
|
@debug = !!options[:debug]
|
|
@@ -74,17 +75,15 @@ module Readability
|
|
|
74
75
|
article_content = grab_article
|
|
75
76
|
return nil unless article_content
|
|
76
77
|
|
|
77
|
-
log("Grabbed: #{article_content.inner_html}")
|
|
78
|
+
log("Grabbed: #{article_content.inner_html}") if @debug
|
|
78
79
|
|
|
79
80
|
post_process_content(article_content)
|
|
80
81
|
|
|
81
82
|
# If we haven't found an excerpt in the article's metadata, use the article's
|
|
82
83
|
# first paragraph as the excerpt.
|
|
83
84
|
if !metadata["excerpt"] || metadata["excerpt"].empty?
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
metadata["excerpt"] = paragraphs[0].text.strip
|
|
87
|
-
end
|
|
85
|
+
first_paragraph = article_content.at_css("p")
|
|
86
|
+
metadata["excerpt"] = first_paragraph.text.strip if first_paragraph
|
|
88
87
|
end
|
|
89
88
|
|
|
90
89
|
text_content = article_content.text
|
|
@@ -216,38 +215,27 @@ module Readability
|
|
|
216
215
|
# Turn all divs that don't have children block level elements into p's
|
|
217
216
|
if node.name == "div"
|
|
218
217
|
# Put phrasing content into paragraphs.
|
|
219
|
-
child_node = node.
|
|
218
|
+
child_node = node.child
|
|
220
219
|
while child_node
|
|
221
220
|
next_sibling = child_node.next_sibling
|
|
222
221
|
if is_phrasing_content?(child_node)
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
child_node = next_sibling
|
|
229
|
-
break unless child_node && is_phrasing_content?(child_node)
|
|
222
|
+
# Collect all consecutive phrasing content.
|
|
223
|
+
phrasing = []
|
|
224
|
+
while child_node && is_phrasing_content?(child_node)
|
|
225
|
+
phrasing << child_node
|
|
226
|
+
child_node = child_node.next_sibling
|
|
230
227
|
end
|
|
228
|
+
next_sibling = child_node
|
|
231
229
|
|
|
232
|
-
# Trim leading
|
|
233
|
-
while
|
|
234
|
-
|
|
235
|
-
end
|
|
236
|
-
# Trim trailing whitespace from the fragment.
|
|
237
|
-
while fragment.children.last && is_whitespace?(fragment.children.last)
|
|
238
|
-
fragment.children.last.unlink
|
|
239
|
-
end
|
|
230
|
+
# Trim leading and trailing whitespace.
|
|
231
|
+
phrasing.shift.unlink while phrasing.any? && is_whitespace?(phrasing.first)
|
|
232
|
+
phrasing.pop.unlink while phrasing.any? && is_whitespace?(phrasing.last)
|
|
240
233
|
|
|
241
|
-
# If
|
|
242
|
-
|
|
243
|
-
if fragment.children.first
|
|
234
|
+
# If anything is left, wrap it in a paragraph in its place.
|
|
235
|
+
if phrasing.any?
|
|
244
236
|
p_node = Nokogiri::XML::Node.new("p", @doc)
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
next_sibling.add_previous_sibling(p_node)
|
|
248
|
-
else
|
|
249
|
-
node.add_child(p_node)
|
|
250
|
-
end
|
|
237
|
+
phrasing.first.add_previous_sibling(p_node)
|
|
238
|
+
phrasing.each { |phrasing_node| p_node.add_child(phrasing_node) }
|
|
251
239
|
end
|
|
252
240
|
end
|
|
253
241
|
child_node = next_sibling
|
|
@@ -350,9 +338,9 @@ module Readability
|
|
|
350
338
|
top_candidate = Nokogiri::XML::Node.new("div", @doc)
|
|
351
339
|
needed_to_create_top_candidate = true
|
|
352
340
|
# Move everything (not just elements, also text nodes etc.) into the container
|
|
353
|
-
|
|
354
|
-
log("Moving child out:",
|
|
355
|
-
top_candidate.add_child(
|
|
341
|
+
page.children.each do |child|
|
|
342
|
+
log("Moving child out:", child)
|
|
343
|
+
top_candidate.add_child(child)
|
|
356
344
|
end
|
|
357
345
|
|
|
358
346
|
page.add_child(top_candidate)
|
|
@@ -493,9 +481,7 @@ module Readability
|
|
|
493
481
|
div = Nokogiri::XML::Node.new("div", @doc)
|
|
494
482
|
div["id"] = "readability-page-1"
|
|
495
483
|
div["class"] = "page"
|
|
496
|
-
|
|
497
|
-
div.add_child(article_content.children.first)
|
|
498
|
-
end
|
|
484
|
+
div.add_child(article_content.children)
|
|
499
485
|
article_content.add_child(div)
|
|
500
486
|
end
|
|
501
487
|
|
|
@@ -504,7 +490,7 @@ module Readability
|
|
|
504
490
|
parse_successful = true
|
|
505
491
|
|
|
506
492
|
# Check to see if we got any meaningful content.
|
|
507
|
-
text_length =
|
|
493
|
+
text_length = inner_text_length(article_content)
|
|
508
494
|
if text_length < @char_threshold
|
|
509
495
|
parse_successful = false
|
|
510
496
|
|
|
@@ -35,7 +35,8 @@ module Readability
|
|
|
35
35
|
|
|
36
36
|
match_string = "#{node['class']} #{node['id']}"
|
|
37
37
|
next false if UNLIKELY_CANDIDATES.match?(match_string) && !OK_MAYBE_CANDIDATE.match?(match_string)
|
|
38
|
-
|
|
38
|
+
# Same as node.matches?("li p"), which searches the whole document for every node.
|
|
39
|
+
next false if node.name == "p" && node.ancestors.any? { |ancestor| ancestor.name == "li" }
|
|
39
40
|
|
|
40
41
|
text_length = node.text.strip.length
|
|
41
42
|
next false if text_length < min_content_length
|
data/lib/readability/regexps.rb
CHANGED
|
@@ -50,6 +50,11 @@ module Readability
|
|
|
50
50
|
|
|
51
51
|
HAS_CONTENT = /[^[:space:]\uFEFF]\z/
|
|
52
52
|
|
|
53
|
+
NON_SPACE = /[^[:space:]\uFEFF]/
|
|
54
|
+
|
|
55
|
+
# The characters in NORMALIZE's class, as a String#tr_s character set
|
|
56
|
+
SPACE_CHARS = "\t\n\v\f\r \u0085\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF"
|
|
57
|
+
|
|
53
58
|
HASH_URL = /\A#.+/
|
|
54
59
|
|
|
55
60
|
SRCSET_URL = /(\S+)(\s+[\d.]+[xw])?(\s*(?:,|$))/
|
|
@@ -72,13 +77,18 @@ module Readability
|
|
|
72
77
|
|
|
73
78
|
DIV_TO_P_ELEMS = Set.new(%w[blockquote dl div img ol p pre table ul]).freeze
|
|
74
79
|
|
|
80
|
+
HEADING_TAGS = %w[h1 h2 h3 h4 h5 h6].freeze
|
|
81
|
+
|
|
82
|
+
# Tags whose text counts towards text density in _cleanConditionally
|
|
83
|
+
TEXTISH_TAGS = (%w[span li td] + DIV_TO_P_ELEMS.to_a).freeze
|
|
84
|
+
|
|
75
85
|
ALTER_TO_DIV_EXCEPTIONS = %w[div article section p ol ul].freeze
|
|
76
86
|
|
|
77
87
|
PRESENTATIONAL_ATTRIBUTES = %w[align background bgcolor border cellpadding cellspacing frame hspace rules style valign vspace].freeze
|
|
78
88
|
|
|
79
89
|
DEPRECATED_SIZE_ATTRIBUTE_ELEMS = %w[table th td hr pre].freeze
|
|
80
90
|
|
|
81
|
-
PHRASING_ELEMS = %w[abbr audio b bdo br button cite code data datalist dfn em embed i img input kbd label mark math meter noscript object output progress q ruby samp script select small span strong sub sup textarea time var wbr].freeze
|
|
91
|
+
PHRASING_ELEMS = Set.new(%w[abbr audio b bdo br button cite code data datalist dfn em embed i img input kbd label mark math meter noscript object output progress q ruby samp script select small span strong sub sup textarea time var wbr]).freeze
|
|
82
92
|
|
|
83
93
|
CLASSES_TO_PRESERVE = %w[page].freeze
|
|
84
94
|
|
data/lib/readability/scoring.rb
CHANGED
|
@@ -51,15 +51,15 @@ module Readability
|
|
|
51
51
|
# Returns the ratio of anchor text length to total text length.
|
|
52
52
|
# Fragment-only links (#...) count at 0.3 coefficient.
|
|
53
53
|
def get_link_density(element, text_length: nil)
|
|
54
|
-
text_length ||=
|
|
54
|
+
text_length ||= inner_text_length(element)
|
|
55
55
|
return 0 if text_length == 0
|
|
56
56
|
|
|
57
57
|
link_length = 0.0
|
|
58
58
|
|
|
59
|
-
element
|
|
59
|
+
css_query(element, "a").each do |link_node|
|
|
60
60
|
href = link_node["href"]
|
|
61
61
|
coefficient = href && HASH_URL.match?(href) ? 0.3 : 1.0
|
|
62
|
-
link_length +=
|
|
62
|
+
link_length += inner_text_length(link_node) * coefficient
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
link_length / text_length
|
|
@@ -67,13 +67,13 @@ module Readability
|
|
|
67
67
|
|
|
68
68
|
# Port of _getTextDensity (JS line 2440)
|
|
69
69
|
# Returns the ratio of text inside elements matching +tags+ to total text in element.
|
|
70
|
-
def get_text_density(element, tags)
|
|
71
|
-
text_length
|
|
70
|
+
def get_text_density(element, tags, text_length: nil)
|
|
71
|
+
text_length ||= inner_text_length(element)
|
|
72
72
|
return 0 if text_length == 0
|
|
73
73
|
|
|
74
74
|
children_length = 0
|
|
75
75
|
get_all_nodes_with_tag(element, tags).each do |child|
|
|
76
|
-
children_length +=
|
|
76
|
+
children_length += inner_text_length(child)
|
|
77
77
|
end
|
|
78
78
|
|
|
79
79
|
children_length.to_f / text_length
|
data/lib/readability/utils.rb
CHANGED
|
@@ -29,14 +29,43 @@ module Readability
|
|
|
29
29
|
|
|
30
30
|
# Port of _getAllNodesWithTag (JS line 397)
|
|
31
31
|
def get_all_nodes_with_tag(node, tag_names)
|
|
32
|
-
node
|
|
32
|
+
css_query(node, tag_names.join(","))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Same as node.css(selector). Node#css translates the selector to XPath and
|
|
36
|
+
# sets up namespaces on every call, which costs more than running the query
|
|
37
|
+
# on the small subtrees scoring and cleaning look at. This translates each
|
|
38
|
+
# selector once per document type, the way Node#css does, and runs the
|
|
39
|
+
# XPath directly. A Document searches from "//" rather than ".//", so it
|
|
40
|
+
# keeps using css.
|
|
41
|
+
def css_query(node, selector)
|
|
42
|
+
return node.css(selector) if node.document?
|
|
43
|
+
|
|
44
|
+
doctype = node.document.xpath_doctype
|
|
45
|
+
xpath = (@xpath_cache[doctype] ||= {})[selector] ||= begin
|
|
46
|
+
visitor = Nokogiri::CSS::XPathVisitor.new(
|
|
47
|
+
builtins: Nokogiri::CSS::XPathVisitor::BuiltinsConfig::OPTIMAL,
|
|
48
|
+
doctype: doctype,
|
|
49
|
+
prefix: ".//",
|
|
50
|
+
)
|
|
51
|
+
Nokogiri::CSS.xpath_for(selector, visitor: visitor).join(" | ")
|
|
52
|
+
end
|
|
53
|
+
Nokogiri::XML::XPathContext.new(node).evaluate(xpath)
|
|
33
54
|
end
|
|
34
55
|
|
|
35
56
|
# JS-compatible trim that also strips Unicode whitespace like \u00A0 (NBSP)
|
|
36
57
|
# JS's String.prototype.trim() strips all Unicode whitespace and \uFEFF (BOM).
|
|
37
58
|
# Ruby's String#strip only removes ASCII whitespace.
|
|
59
|
+
#
|
|
60
|
+
# Finds the first and last non-space characters by byte offset (character
|
|
61
|
+
# offsets in non-ASCII strings cost a scan from the start) instead of using
|
|
62
|
+
# gsub(/\A\s+|\s+\z/), which rescans every interior whitespace run and is
|
|
63
|
+
# quadratic on the long text of large elements.
|
|
38
64
|
def js_trim(str)
|
|
39
|
-
str.
|
|
65
|
+
first = str.byteindex(NON_SPACE) or return ""
|
|
66
|
+
str.byterindex(NON_SPACE)
|
|
67
|
+
last = Regexp.last_match.byteoffset(0)[1]
|
|
68
|
+
str.byteslice(first, last - first)
|
|
40
69
|
end
|
|
41
70
|
|
|
42
71
|
# Port of _getInnerText (JS line 2084)
|
|
@@ -46,9 +75,16 @@ module Readability
|
|
|
46
75
|
text
|
|
47
76
|
end
|
|
48
77
|
|
|
78
|
+
# Same as get_inner_text(element).length, for callers that only need the
|
|
79
|
+
# length. tr_s collapses each whitespace run to one character, as NORMALIZE
|
|
80
|
+
# does, at less than half the cost of the regex.
|
|
81
|
+
def inner_text_length(element)
|
|
82
|
+
js_trim(element.text).tr_s(SPACE_CHARS, " ").length
|
|
83
|
+
end
|
|
84
|
+
|
|
49
85
|
# Port of _isWhitespace (JS line 2068)
|
|
50
86
|
def is_whitespace?(node)
|
|
51
|
-
(node.text? &&
|
|
87
|
+
(node.text? && WHITESPACE.match?(node.text)) ||
|
|
52
88
|
(node.element? && node.name == "br")
|
|
53
89
|
end
|
|
54
90
|
|
|
@@ -72,11 +108,12 @@ module Readability
|
|
|
72
108
|
|
|
73
109
|
# Port of _isElementWithoutContent (JS line 2028)
|
|
74
110
|
def is_element_without_content?(node)
|
|
75
|
-
node.element?
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
111
|
+
return false unless node.element?
|
|
112
|
+
|
|
113
|
+
# Check the children first: reading the text walks the whole subtree.
|
|
114
|
+
children = node.element_children
|
|
115
|
+
(children.empty? || children.length == css_query(node, "br, hr").length) &&
|
|
116
|
+
WHITESPACE.match?(node.text)
|
|
80
117
|
end
|
|
81
118
|
|
|
82
119
|
# Port of _hasChildBlockElement (JS line 2044)
|
|
@@ -129,12 +166,13 @@ module Readability
|
|
|
129
166
|
# Depth-first traversal.
|
|
130
167
|
def get_next_node(node, ignore_self_and_kids = false)
|
|
131
168
|
# First check for kids if those aren't being ignored
|
|
132
|
-
if !ignore_self_and_kids && node.
|
|
133
|
-
return
|
|
169
|
+
if !ignore_self_and_kids && (first_child = node.first_element_child)
|
|
170
|
+
return first_child
|
|
134
171
|
end
|
|
135
172
|
|
|
136
173
|
# Then for siblings...
|
|
137
|
-
|
|
174
|
+
next_element = node.next_element
|
|
175
|
+
return next_element if next_element
|
|
138
176
|
|
|
139
177
|
# And finally, move up the parent chain *and* find a sibling
|
|
140
178
|
current = node
|
|
@@ -190,24 +228,24 @@ module Readability
|
|
|
190
228
|
current = node
|
|
191
229
|
while current
|
|
192
230
|
return true if current.name == "img"
|
|
193
|
-
|
|
231
|
+
children = current.element_children
|
|
232
|
+
return false if children.length != 1 || !WHITESPACE.match?(current.text)
|
|
194
233
|
|
|
195
|
-
current =
|
|
234
|
+
current = children[0]
|
|
196
235
|
end
|
|
197
236
|
false
|
|
198
237
|
end
|
|
199
238
|
|
|
200
239
|
# Port of _isValidByline (JS line 1005)
|
|
201
240
|
def is_valid_byline?(node, match_string)
|
|
202
|
-
rel = node["rel"]
|
|
203
241
|
itemprop = node["itemprop"]
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
return false if byline_text.empty? || byline_text.length >= 100
|
|
207
|
-
|
|
208
|
-
rel == "author" ||
|
|
242
|
+
# Check the attributes before the text, which walks the whole subtree.
|
|
243
|
+
return false unless node["rel"] == "author" ||
|
|
209
244
|
(itemprop && itemprop.include?("author")) ||
|
|
210
245
|
BYLINE.match?(match_string)
|
|
246
|
+
|
|
247
|
+
byline_text = js_trim(node.text)
|
|
248
|
+
!byline_text.empty? && byline_text.length < 100
|
|
211
249
|
end
|
|
212
250
|
|
|
213
251
|
# Port of _isUrl (JS line 442)
|
data/lib/readability/version.rb
CHANGED
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.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andy Croll
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - "~>"
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: '1.
|
|
18
|
+
version: '1.17'
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - "~>"
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: '1.
|
|
25
|
+
version: '1.17'
|
|
26
26
|
description: Ruby port of Mozilla Readability.js - extracts the main content from
|
|
27
27
|
web pages, like Firefox Reader View
|
|
28
28
|
email:
|