nokodown 0.1.0 → 0.1.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 +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +26 -9
- data/lib/nokodown/boilerplate.rb +127 -25
- data/lib/nokodown/container_check.rb +63 -12
- data/lib/nokodown/content_locator.rb +27 -11
- data/lib/nokodown/dom.rb +242 -16
- data/lib/nokodown/extractor.rb +116 -19
- data/lib/nokodown/heading_anchors.rb +203 -0
- data/lib/nokodown/markdown/cleanup.rb +156 -4
- data/lib/nokodown/markdown/code_block.rb +2 -2
- data/lib/nokodown/markdown/image.rb +15 -5
- data/lib/nokodown/markdown/image_cleanup.rb +27 -7
- data/lib/nokodown/markdown/inline.rb +140 -15
- data/lib/nokodown/markdown/redundant_images.rb +2 -1
- data/lib/nokodown/markdown/renderer.rb +116 -34
- data/lib/nokodown/markdown/table.rb +19 -5
- data/lib/nokodown/markdown/text.rb +239 -16
- data/lib/nokodown/member_panel.rb +123 -0
- data/lib/nokodown/metadata.rb +1 -1
- data/lib/nokodown/patterns.rb +89 -4
- data/lib/nokodown/post_attribution.rb +454 -0
- data/lib/nokodown/preprocessor.rb +351 -32
- data/lib/nokodown/product_reviews.rb +1 -1
- data/lib/nokodown/pruner.rb +108 -29
- data/lib/nokodown/scorer.rb +77 -19
- data/lib/nokodown/stats.rb +288 -23
- data/lib/nokodown/tables.rb +6 -2
- data/lib/nokodown/url_resolver.rb +101 -24
- data/lib/nokodown/version.rb +1 -1
- data/lib/nokodown.rb +7 -1
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0314d870f66ddf9b41731c65aed7efa2e9bb84da8ffeb4a8e0c34693127f318c
|
|
4
|
+
data.tar.gz: 8c691c3ca81adbdc06598d5947f77f25f0ed7a14f812abe16a2727c3c7104b33
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b428e5114ef87fcb748e6f896256df3e0e12c8c6c01353f5f5516c721ff4a1fffaf5f380eb44840348372a3861d35d91ba1f34e87b2aff2fa97228756176e38c
|
|
7
|
+
data.tar.gz: 979c17e900dda0668660c0207ea0743173cb03f58077ac6da4d8bb0f5e7bb400299fc952098c67904a5b699aeff22b2df485f30d85f2e0f8dc0a87586a8d33c5
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,63 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.1 (2026-11-18)
|
|
4
|
+
|
|
5
|
+
- Elements nested more than 64 levels below `<body>` are converted as plain
|
|
6
|
+
text, so deeply nested markup no longer raises `SystemStackError`, also
|
|
7
|
+
inside a thread with its smaller stack.
|
|
8
|
+
- Native `<video>` and `<audio>` players become links to the file they play,
|
|
9
|
+
labelled with their title. Muted videos (autoplaying backgrounds) are
|
|
10
|
+
skipped, and a player inside a link is replaced by its thumbnail, if it
|
|
11
|
+
holds one and is not hidden.
|
|
12
|
+
- Links without `href` take their target from `data-href`, `data-url` and
|
|
13
|
+
the other attributes scripts keep it in.
|
|
14
|
+
- An image's `data-src` (and the other attributes only lazy loaders use) is
|
|
15
|
+
preferred over its `src`, also without a "lazy" class, so the real image
|
|
16
|
+
replaces a placeholder or a low-quality thumbnail. High-resolution
|
|
17
|
+
attributes come first, then the largest size in `data-a-dynamic-image`,
|
|
18
|
+
then the other loaders' attributes and a lazy `srcset`. A value that is
|
|
19
|
+
no image location ("false", a number, a word) never replaces `src`.
|
|
20
|
+
Spacer and placeholder files no longer count as a `src`.
|
|
21
|
+
- With `main_content: false`, everything visible is converted, so a
|
|
22
|
+
selection comes out whole: headings without a section, boilerplate lines
|
|
23
|
+
("Advertisement", "Share"), repeated headings, paragraphs and images,
|
|
24
|
+
quotes, rules, buttons, form text and open dialogs are kept. Only
|
|
25
|
+
scripts, styles, hidden elements, form inputs, closed dialogs and
|
|
26
|
+
punctuation-only lines are dropped.
|
|
27
|
+
- Links and images whose `javascript:` or `vbscript:` scheme is disguised
|
|
28
|
+
by tabs, line breaks or leading control characters (`java	script:`),
|
|
29
|
+
or that a `<base>` with such a scheme would produce, are dropped like
|
|
30
|
+
any other script link. A link's URL can no longer end its Markdown
|
|
31
|
+
destination early with a stray `)`, nor its text end the label early
|
|
32
|
+
with a stray `]` or a trailing backslash, and write a link of its own.
|
|
33
|
+
Link text holds no link, autolink or HTML tag of its own either, from
|
|
34
|
+
raw TeX or from a backtick run too long to fence code. Raw TeX, emoji
|
|
35
|
+
alt text and email addresses, whatever they start with, cannot start
|
|
36
|
+
Markdown that reaches past them; code with a backtick run too long to
|
|
37
|
+
fence is written as text; a backslash at the end of a text escapes
|
|
38
|
+
nothing after it; code spans side by side keep their own fences; and
|
|
39
|
+
text side by side cannot join into a link, an image, an autolink or a
|
|
40
|
+
tag.
|
|
41
|
+
Brackets and backticks in a URL are percent-encoded, so a URL read as
|
|
42
|
+
text holds no link either.
|
|
43
|
+
- Code inside link text keeps its brackets and backslashes as they are.
|
|
44
|
+
- `max_length:` returns exactly the first `max_length` characters of the
|
|
45
|
+
Markdown, and the same `excerpt`, and stops rendering once they are
|
|
46
|
+
known: at most one short render and one whole render. `nil`, the
|
|
47
|
+
default, changes nothing; a negative or non-Integer value raises
|
|
48
|
+
`ArgumentError`.
|
|
49
|
+
- A link whose `href` is blank takes its target from `data-href` and the
|
|
50
|
+
like too. A link made only of images keeps its target, as
|
|
51
|
+
`[](href)`.
|
|
52
|
+
- An iframe embed is linked only from an http(s) URL on the video host's
|
|
53
|
+
own domain, and one without a title is written as `[Video](url)`.
|
|
54
|
+
- A page made only of headings keeps all of them, a one-column table with
|
|
55
|
+
a header cell or a caption stays a table, and the H1 above an article
|
|
56
|
+
body chosen inside `<main>` opens the content.
|
|
57
|
+
- A table cell escapes every pipe, so a backslash before a pipe stays.
|
|
58
|
+
- Extraction is faster, with the same output: about 40% on Wikipedia's
|
|
59
|
+
Coffee article (364 to 214 ms on Ruby 2.7) and 27% on WCXB (ms per page).
|
|
60
|
+
|
|
3
61
|
## 0.1.0 (2026-11-02)
|
|
4
62
|
|
|
5
63
|
First release.
|
data/README.md
CHANGED
|
@@ -65,10 +65,11 @@ public API. Other classes are internal and can change in any release.
|
|
|
65
65
|
| Option | Default | |
|
|
66
66
|
| --- | --- | --- |
|
|
67
67
|
| `url` | `nil` | Page URL. Relative links and images are resolved against it (and `<base>`), and links to the page itself are reduced to their text. |
|
|
68
|
-
| `main_content` | `true` | Keep only the main content. `false` converts the whole page,
|
|
69
|
-
| `title` | `true` | Start with the page title as a `#` heading when the content has no `<h1
|
|
68
|
+
| `main_content` | `true` | Keep only the main content. `false` converts the whole page, or a selection of it, as it is: only scripts, styles, hidden elements, form inputs, closed dialogs and punctuation-only lines are dropped, and repeats, buttons, headings without a section and lines such as "Advertisement" or "Share" stay. |
|
|
69
|
+
| `title` | `true` | Start with the page title as a `#` heading when the content has no `<h1>`, or when its first `<h1>` follows the opening paragraphs and so heads a section. When no content is found at all (a page rendered by scripts, a bot wall), `markdown` is empty and only `result.title` has the title. |
|
|
70
70
|
| `images` | `true` | Include images. Emoji images are always kept as their text. |
|
|
71
71
|
| `links` | `true` | Keep links. `false` keeps only their text. |
|
|
72
|
+
| `max_length` | `nil` | Return at most this many characters of Markdown: exactly the first `max_length` characters of what `nil` gives, and the same `excerpt`. Rendering stops once the result is known, so a huge page costs less; when the start alone cannot settle it (an `<h1>` or a thumbnail further down can change it), the whole content is rendered once more. |
|
|
72
73
|
|
|
73
74
|
`mode` says how the content was chosen: `:article` when the page was narrowed
|
|
74
75
|
to one body of prose, `:page` when the whole main region was kept, `:full`
|
|
@@ -80,14 +81,23 @@ with `main_content: false`, `:none` for a document without a body.
|
|
|
80
81
|
tables of contents, found through HTML semantics (`nav`, page-level `header`
|
|
81
82
|
and `footer`, ARIA roles, schema.org types) and class/id names.
|
|
82
83
|
- Cookie banners, ads, newsletter and sign-in prompts, share bars, modals,
|
|
83
|
-
chat widgets, "related", "recommended" and "most popular" boxes,
|
|
84
|
-
|
|
84
|
+
chat widgets, "related", "recommended" and "most popular" boxes, legal
|
|
85
|
+
disclaimers and disclosures (but not a note continuing the paragraph before
|
|
86
|
+
it, or the footnote to a mark just above), and, next to an article, its
|
|
87
|
+
comment thread.
|
|
88
|
+
- On forums, the member panel and signature repeated beside every post. The
|
|
89
|
+
member panel leaves one line naming the poster, with the post's date when
|
|
90
|
+
the panel holds it. A quote that repeats a post already in the output is cut
|
|
91
|
+
to its opening sentence, so the reply still shows what it answers.
|
|
85
92
|
- On product pages, customer reviews and Q&A widgets, with their review
|
|
86
93
|
summaries and rating histograms. An inline rating badge ("212 reviews" next
|
|
87
94
|
to the price), testimonials and editorial reviews stay.
|
|
88
95
|
- Invisible elements (`hidden`, `aria-hidden`, inline `display: none`,
|
|
89
96
|
screen-reader-only and other hiding classes), keeping collapsed accordions,
|
|
90
|
-
tab panels and FAQ answers.
|
|
97
|
+
tab panels and FAQ answers. Content that React streams into hidden
|
|
98
|
+
containers at the end of the page is put back where the page shows it,
|
|
99
|
+
unless it is mostly links, as carousels of recommended products are.
|
|
100
|
+
- Sections rendered twice, such as a desktop and a mobile copy.
|
|
91
101
|
- Tracking pixels, spacers, icons, placeholders, README badges, click-tracking
|
|
92
102
|
URL parameters (`utm_*`, `gclid`, `fbclid`, ...), permalink anchors,
|
|
93
103
|
"Read more" links, code line numbers, and repeated responsive copies of the
|
|
@@ -117,7 +127,10 @@ region, or most of the page's text, is never removed because of its class name.
|
|
|
117
127
|
2. **Metadata**: JSON-LD, Open Graph and other `<meta>` tags, then the
|
|
118
128
|
document, in Readability's order of preference.
|
|
119
129
|
3. **Preprocessor**: restore lazy-loaded images (`data-src`, `srcset`,
|
|
120
|
-
`<noscript>` copies
|
|
130
|
+
`<noscript>` copies, preferred over a placeholder or thumbnail `src`) and
|
|
131
|
+
link targets kept in `data-href` and similar attributes, drop scripts,
|
|
132
|
+
invisible elements and form controls, and turn elements nested more than
|
|
133
|
+
64 levels deep into plain text.
|
|
121
134
|
4. **Boilerplate**: remove page chrome and noise widgets across the whole page.
|
|
122
135
|
5. **ContentLocator**: score paragraphs with Readability's algorithm. When one
|
|
123
136
|
candidate holds most of the page's prose the content is narrowed to it
|
|
@@ -127,7 +140,11 @@ region, or most of the page's text, is never removed because of its class name.
|
|
|
127
140
|
threads (schema.org types or the forum software's generator tag) replies
|
|
128
141
|
are content even in comment-named containers, and on forums and Q&A pages
|
|
129
142
|
where no single post holds the page, the whole main region is cleaned as an
|
|
130
|
-
article.
|
|
143
|
+
article. There, each post's author and date, read from its header before
|
|
144
|
+
boilerplate removal, become one line above the post's body
|
|
145
|
+
(`**JGD** · April 2, 2024`) in place of the header, the panel around the
|
|
146
|
+
name and repeated copies of the date. A post "by" Advertisement is an ad
|
|
147
|
+
slot and goes.
|
|
131
148
|
6. **Pruner**: in articles, Readability's conditional cleaning plus removal of
|
|
132
149
|
bylines, datelines and inline boxes of links to other stories; on other
|
|
133
150
|
pages, removal of leftover link clusters, unless they hold most of the text
|
|
@@ -141,8 +158,8 @@ region, or most of the page's text, is never removed because of its class name.
|
|
|
141
158
|
highlighter's classes, GFM tables (rowspans and colspans filled in) for data
|
|
142
159
|
tables while layout tables are unwrapped, definition lists, details, TeX
|
|
143
160
|
from MathML, superscript and subscript digits as Unicode (10⁶, H₂O) and
|
|
144
|
-
other scripts as 2^(n-1) and a_n
|
|
145
|
-
otherwise change meaning.
|
|
161
|
+
other scripts as 2^(n-1) and a_n, and video embeds and native players as
|
|
162
|
+
links. Text is escaped only where it would otherwise change meaning.
|
|
146
163
|
|
|
147
164
|
## Quality
|
|
148
165
|
|
data/lib/nokodown/boilerplate.rb
CHANGED
|
@@ -13,9 +13,24 @@ module Nokodown
|
|
|
13
13
|
SECTIONING = %w[article aside main nav section].freeze
|
|
14
14
|
PHRASING = Set.new(%w[a abbr b bdi bdo cite code data dfn em i kbd mark q s samp small span strong sub sup time u var]).freeze
|
|
15
15
|
AD_ATTRIBUTES = %w[data-ad-slot data-ad-unit data-adunit data-ad-client data-google-query-id data-ad-format].freeze
|
|
16
|
+
AD_ATTRIBUTE_SELECTORS = AD_ATTRIBUTES.map { |name| "[#{name}]" }.freeze
|
|
16
17
|
DRAWER = /(?<![a-z])drawer(?![a-z])/.freeze
|
|
18
|
+
# Terms WordPress (tag-*, category-*) and WooCommerce (product_cat-*,
|
|
19
|
+
# product_tag-*, product_brand-*) put on a post or product.
|
|
20
|
+
TAXONOMY = /(?<![\w-])(?:tag|category|has|product_(?:cat|tag|brand))-[\w-]+/.freeze
|
|
17
21
|
MIN_CARD_RUN = 3
|
|
22
|
+
MIN_LEFT = 200
|
|
18
23
|
CARD_RUN_SHARE = 0.2
|
|
24
|
+
# A note continues the prose only after a sentence or more of it, not after a line of links.
|
|
25
|
+
NOTE_PROSE = 80
|
|
26
|
+
FOOTNOTE_MARK = /\A[*†‡§]+/.freeze
|
|
27
|
+
# The word goes with its separators: "footer__disclaimer" without it is "footer", not "footer__".
|
|
28
|
+
DISCLAIMER_WORD = /[-_]*#{Patterns::DISCLAIMER.source}[-_]*/.freeze
|
|
29
|
+
SIGNATURE_WORD = /[-_]*#{Patterns::SIGNATURE.source}[-_]*/.freeze
|
|
30
|
+
|
|
31
|
+
# The counts of the page left, or nil when member panels were replaced
|
|
32
|
+
# with new elements.
|
|
33
|
+
attr_reader :stats
|
|
19
34
|
|
|
20
35
|
def initialize(body)
|
|
21
36
|
@body = body
|
|
@@ -25,12 +40,36 @@ module Nokodown
|
|
|
25
40
|
@stats = Stats.new(@body)
|
|
26
41
|
@total = @stats.text_chars(@body)
|
|
27
42
|
@shielded = shielded_keys
|
|
28
|
-
#
|
|
29
|
-
|
|
43
|
+
# Few pages carry ad attributes; asking the page once per name (it also
|
|
44
|
+
# reads templates, which the walk skips) spares asking every element for
|
|
45
|
+
# each. A selector list would number the page's nodes for sorting, and
|
|
46
|
+
# copies made later would then list nodes in another order.
|
|
47
|
+
@ad_attributes = AD_ATTRIBUTE_SELECTORS.any? { |selector| !@body.at_css(selector).nil? }
|
|
48
|
+
# Card grids repeat the same class list hundreds of times, and class
|
|
49
|
+
# names repeat across lists. No furniture pattern spans a space, so a
|
|
50
|
+
# list is furniture when one of its names is.
|
|
51
|
+
@furniture_tokens = Hash.new { |cache, token| cache[token] = Patterns.furniture?(token) }
|
|
52
|
+
@furniture = Hash.new { |cache, signature| cache[signature] = signature.split.any? { |token| @furniture_tokens[token] } }
|
|
53
|
+
@disclaimer_only = Hash.new { |cache, signature| cache[signature] = disclaimer_only?(signature) }
|
|
54
|
+
@signature_only = Hash.new { |cache, signature| cache[signature] = signature_only?(signature) }
|
|
55
|
+
@noise = Hash.new { |cache, signature| cache[signature] = signature.gsub(TAXONOMY, " ").match?(Patterns::NOISE) }
|
|
56
|
+
@notes = Set.new
|
|
30
57
|
@prose_blocks = Set.new
|
|
31
58
|
@card_classes = card_classes
|
|
32
59
|
@card_bases = @card_classes.map { |classes| base_classes(classes) }
|
|
33
|
-
|
|
60
|
+
removals = []
|
|
61
|
+
walk(@body, removals)
|
|
62
|
+
removals.delete(content_guess(removals)) if almost_empty_without?(removals)
|
|
63
|
+
if removals.none? { |_, kind| kind == :panel }
|
|
64
|
+
@stats.remove(removals.map(&:first))
|
|
65
|
+
return self
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
removals.each { |node, kind| kind == :panel ? MemberPanel.reduce(node) : node.remove }
|
|
69
|
+
# The lines panels leave are new elements, and later passes measure by key.
|
|
70
|
+
Dom.assign_keys(@body)
|
|
71
|
+
@stats = nil
|
|
72
|
+
self
|
|
34
73
|
end
|
|
35
74
|
|
|
36
75
|
private
|
|
@@ -43,29 +82,42 @@ module Nokodown
|
|
|
43
82
|
end
|
|
44
83
|
|
|
45
84
|
# Code is never furniture, whatever classes its highlighter gives tokens.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
child
|
|
52
|
-
|
|
53
|
-
walk(child)
|
|
85
|
+
# Nothing is removed during the walk, so the element pointers stay valid.
|
|
86
|
+
def walk(node, removals)
|
|
87
|
+
child = node.first_element_child
|
|
88
|
+
while child
|
|
89
|
+
unless Dom::CODE.include?(child.name)
|
|
90
|
+
kind = removal(child)
|
|
91
|
+
kind ? removals << [child, kind] : walk(child, removals)
|
|
54
92
|
end
|
|
93
|
+
child = child.next_element
|
|
55
94
|
end
|
|
56
95
|
end
|
|
57
96
|
|
|
97
|
+
def almost_empty_without?(removals)
|
|
98
|
+
@stats.chars(@body) - removals.sum { |node, _| @stats.chars(node) } < MIN_LEFT
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def content_guess(removals)
|
|
102
|
+
guess = removals.select { |_, kind| kind == :guess }.max_by { |node, _| @stats.chars(node) }
|
|
103
|
+
return unless guess && @stats.chars(guess.first) >= MIN_LEFT
|
|
104
|
+
|
|
105
|
+
signature = Dom.signature(guess.first)
|
|
106
|
+
guess unless signature.match?(Patterns::PLACE) || signature.match?(Patterns::OVERLAY)
|
|
107
|
+
end
|
|
108
|
+
|
|
58
109
|
# Semantic chrome (nav, a page-level footer) goes even when it outweighs
|
|
59
110
|
# the content, as legal footnotes often do; class-name guesses do not.
|
|
60
|
-
def
|
|
61
|
-
return
|
|
111
|
+
def removal(node)
|
|
112
|
+
return if @shielded.include?(Dom.key(node))
|
|
62
113
|
# Side notes inside an article are content whatever their class says.
|
|
63
|
-
return
|
|
64
|
-
return
|
|
65
|
-
return
|
|
66
|
-
return
|
|
114
|
+
return if node.name == "aside" && !aside_noise?(node)
|
|
115
|
+
return :chrome if chrome_element?(node) || chrome_role?(node)
|
|
116
|
+
return if dominant?(node)
|
|
117
|
+
return unless advertising?(node) || chrome_label?(node) || class_noise?(node)
|
|
118
|
+
return if inside_sentence?(node)
|
|
67
119
|
|
|
68
|
-
|
|
120
|
+
MemberPanel.panel?(node) ? :panel : :guess
|
|
69
121
|
end
|
|
70
122
|
|
|
71
123
|
# A link or span in the middle of running text is part of that text,
|
|
@@ -112,7 +164,7 @@ module Nokodown
|
|
|
112
164
|
return true if node.name == "ins" && node["class"].to_s.include?("adsbygoogle")
|
|
113
165
|
return true if node["id"].to_s.start_with?("div-gpt-ad", "google_ads")
|
|
114
166
|
|
|
115
|
-
AD_ATTRIBUTES.any? { |name| node.key?(name) }
|
|
167
|
+
@ad_attributes && AD_ATTRIBUTES.any? { |name| node.key?(name) }
|
|
116
168
|
end
|
|
117
169
|
|
|
118
170
|
def chrome_label?(node)
|
|
@@ -124,7 +176,55 @@ module Nokodown
|
|
|
124
176
|
def class_noise?(node)
|
|
125
177
|
return false if !@prose_blocks.empty? && prose_part?(node)
|
|
126
178
|
|
|
127
|
-
furniture_class?(node) && (@card_bases.empty? || !card?(node))
|
|
179
|
+
furniture_class?(node) && !content_note?(node) && (@card_bases.empty? || !card?(node))
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Fine print about the site is furniture, but a note on the content right
|
|
183
|
+
# before it belongs to that content: a paragraph continuing the prose, or
|
|
184
|
+
# the footnote to a mark in it ("40-46 states*"). Widgets nest the note's
|
|
185
|
+
# text in an element named like the note.
|
|
186
|
+
def content_note?(node)
|
|
187
|
+
return false unless @disclaimer_only[Dom.signature(node)]
|
|
188
|
+
|
|
189
|
+
note = @notes.include?(Dom.key(node.parent)) || annotates_previous?(node)
|
|
190
|
+
@notes << Dom.key(node) if note
|
|
191
|
+
note
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def annotates_previous?(node)
|
|
195
|
+
previous = node.previous_element
|
|
196
|
+
return false unless previous
|
|
197
|
+
|
|
198
|
+
prose_note?(node, previous) || footnote_to?(node, previous)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def prose_note?(node, previous)
|
|
202
|
+
return false unless node.name == "p" && previous.name == "p"
|
|
203
|
+
|
|
204
|
+
@stats.text_chars(previous) >= NOTE_PROSE && !@furniture[Dom.signature(previous)]
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def footnote_to?(node, previous)
|
|
208
|
+
marks = [node, *node.css("p, li")].map { |line| Dom.text(line)[FOOTNOTE_MARK] }.compact.uniq
|
|
209
|
+
marks.any? { |mark| previous.content.match?(/[[:alnum:]]#{Regexp.escape(mark)}/) }
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Only a signature that is furniture for its disclaimer name alone can be
|
|
213
|
+
# a note; "ad-disclaimer" or "footer__disclaimer" stays furniture.
|
|
214
|
+
def disclaimer_only?(signature)
|
|
215
|
+
signature.match?(Patterns::DISCLAIMER) && !Patterns.furniture?(signature.gsub(DISCLAIMER_WORD, " "))
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Furniture for its signature name alone ("signature", "member-signature").
|
|
219
|
+
def signature_only?(signature)
|
|
220
|
+
signature.match?(Patterns::SIGNATURE) && !Patterns.furniture?(signature.gsub(SIGNATURE_WORD, " "))
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# A member's signature is named after the post ("message-signature") or
|
|
224
|
+
# sits in one; a function's signature in API docs does neither.
|
|
225
|
+
def forum_signature?(node, signature)
|
|
226
|
+
signature.gsub(SIGNATURE_WORD, " ").match?(Patterns::POST) ||
|
|
227
|
+
node.ancestors.any? { |ancestor| ancestor.element? && Dom.signature(ancestor).match?(Patterns::POST) }
|
|
128
228
|
end
|
|
129
229
|
|
|
130
230
|
def card?(node)
|
|
@@ -145,10 +245,12 @@ module Nokodown
|
|
|
145
245
|
return false if signature.empty?
|
|
146
246
|
|
|
147
247
|
chrome = @furniture[signature]
|
|
148
|
-
return site_header?(node) if !chrome &&
|
|
149
|
-
return @stats.link_density(node) > 0.5 if !chrome &&
|
|
248
|
+
return site_header?(node) if !chrome && Patterns.site_header?(signature)
|
|
249
|
+
return @stats.link_density(node) > 0.5 if !chrome && Patterns.menu?(signature)
|
|
150
250
|
return false unless chrome
|
|
251
|
+
return false if @signature_only[signature] && !forum_signature?(node, signature)
|
|
151
252
|
return false if prose_section?(node, signature)
|
|
253
|
+
return true if @noise[signature]
|
|
152
254
|
|
|
153
255
|
!(signature.match?(Patterns::POSITIVE) && @stats.text_chars(node) >= 200)
|
|
154
256
|
end
|
|
@@ -162,7 +264,7 @@ module Nokodown
|
|
|
162
264
|
return false if signature.match?(Patterns::PLACE) || signature.gsub(DRAWER, " ").match?(Patterns::NOISE)
|
|
163
265
|
return false if node.css("p").size < 2
|
|
164
266
|
|
|
165
|
-
@prose_blocks.merge(node["class"].to_s.split.select { |token| token.downcase
|
|
267
|
+
@prose_blocks.merge(node["class"].to_s.split.select { |token| @furniture_tokens[token.downcase] })
|
|
166
268
|
true
|
|
167
269
|
end
|
|
168
270
|
|
|
@@ -172,7 +274,7 @@ module Nokodown
|
|
|
172
274
|
# is link text, so all characters count.
|
|
173
275
|
def card_classes
|
|
174
276
|
min_chars = @stats.chars(@body) * CARD_RUN_SHARE
|
|
175
|
-
runs = @body.
|
|
277
|
+
runs = Dom.elements(@body).select { |node| node.key?("class") }.group_by { |node| node["class"] }
|
|
176
278
|
runs.each_with_object(Set.new) do |(classes, nodes), cards|
|
|
177
279
|
cards << classes if card_run?(classes, nodes, min_chars)
|
|
178
280
|
end
|
|
@@ -191,7 +293,7 @@ module Nokodown
|
|
|
191
293
|
|
|
192
294
|
def furniture_name?(classes)
|
|
193
295
|
signature = Dom.signature_of("#{classes} ")
|
|
194
|
-
@furniture[signature] ||
|
|
296
|
+
@furniture[signature] || Patterns.site_header?(signature) || Patterns.menu?(signature)
|
|
195
297
|
end
|
|
196
298
|
|
|
197
299
|
# Menus repeat their items too, so a run named after a place is never
|
|
@@ -7,9 +7,13 @@ module Nokodown
|
|
|
7
7
|
class ContainerCheck
|
|
8
8
|
HEADINGS = "h1, h2, h3, h4, h5, h6"
|
|
9
9
|
TEXTISH = "span, li, td, blockquote, dl, div, img, ol, p, pre, table, ul"
|
|
10
|
+
TEXTISH_NAMES = Set.new(TEXTISH.split(", ")).freeze
|
|
10
11
|
AD_WORDS = /\A(?:ad(?:vertising|vertisement)?|pub(?:licité)?|werb(?:ung)?|广告|Реклама|Anuncio)\z/i.freeze
|
|
11
12
|
LOADING_WORDS = /\A(?:(?:loading|正在加载|Загрузка|chargement|cargando)(?:…|\.\.\.)?)\z/i.freeze
|
|
12
13
|
POST_BODY_SHARE = 0.8
|
|
14
|
+
PLAYER_CLASS = /media/.freeze
|
|
15
|
+
PLAYERS = "video, audio"
|
|
16
|
+
EMBEDS = "object, embed, iframe"
|
|
13
17
|
|
|
14
18
|
def initialize(node, content_length, paragraph_keys, memo = {})
|
|
15
19
|
@node = node
|
|
@@ -44,8 +48,10 @@ module Nokodown
|
|
|
44
48
|
@text.length > @content_length * 0.5
|
|
45
49
|
end
|
|
46
50
|
|
|
51
|
+
# "media" in a class is Readability's sign of social-media widgets, but on
|
|
52
|
+
# a container holding a player it names the player.
|
|
47
53
|
def weight
|
|
48
|
-
@weight ||= Scorer.cleaning_weight(@node)
|
|
54
|
+
@weight ||= Scorer.cleaning_weight(@node, ignore: (PLAYER_CLASS if player?))
|
|
49
55
|
end
|
|
50
56
|
|
|
51
57
|
def data_table_involved?(outer_tables)
|
|
@@ -83,9 +89,31 @@ module Nokodown
|
|
|
83
89
|
!post.nil? && @text.length >= post_length(post) * POST_BODY_SHARE
|
|
84
90
|
end
|
|
85
91
|
|
|
92
|
+
# Native players the preprocessor kept play a file, so they are content.
|
|
93
|
+
def player?
|
|
94
|
+
return @player if defined?(@player)
|
|
95
|
+
|
|
96
|
+
@player = !none_in_root?(PLAYERS) && !@node.at_css(PLAYERS).nil?
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Most roots hold no player, embed, input, image or list item. Asking the
|
|
100
|
+
# root once spares asking each container in it; removing containers
|
|
101
|
+
# never adds any. A check without a root asks its own node. Each tag is
|
|
102
|
+
# asked on its own: a selector list numbers the page's nodes, and later
|
|
103
|
+
# lists sort their results by those numbers.
|
|
104
|
+
def none_in_root?(selector)
|
|
105
|
+
root = @memo[:root]
|
|
106
|
+
return false unless root
|
|
107
|
+
|
|
108
|
+
absent = (@memo[:absent] ||= {})
|
|
109
|
+
absent.fetch(selector) { absent[selector] = selector.split(", ").all? { |name| root.at_css(name).nil? } }
|
|
110
|
+
end
|
|
111
|
+
|
|
86
112
|
def video_embed?
|
|
87
|
-
|
|
88
|
-
|
|
113
|
+
return true if player?
|
|
114
|
+
|
|
115
|
+
embeds.any? do |embed|
|
|
116
|
+
embed.attribute_nodes.any? { |attribute| Preprocessor.video_embed?(attribute.value) }
|
|
89
117
|
end
|
|
90
118
|
end
|
|
91
119
|
|
|
@@ -99,15 +127,18 @@ module Nokodown
|
|
|
99
127
|
end
|
|
100
128
|
|
|
101
129
|
def too_many_list_items?
|
|
102
|
-
!list? &&
|
|
130
|
+
list_items > 100 && !list? && list_items - 100 > paragraphs
|
|
103
131
|
end
|
|
104
132
|
|
|
105
133
|
def too_many_inputs?
|
|
106
|
-
|
|
134
|
+
return false if none_in_root?("input")
|
|
135
|
+
|
|
136
|
+
inputs = @node.css("input").size
|
|
137
|
+
inputs.positive? && inputs > paragraphs / 3
|
|
107
138
|
end
|
|
108
139
|
|
|
109
140
|
def short_with_links?
|
|
110
|
-
!list? && !figure_child? && density(HEADINGS) < 0.9 &&
|
|
141
|
+
@text.length < 25 && !list? && !figure_child? && density(HEADINGS) < 0.9 &&
|
|
111
142
|
(images.zero? || images > 2) && link_density.positive?
|
|
112
143
|
end
|
|
113
144
|
|
|
@@ -116,25 +147,45 @@ module Nokodown
|
|
|
116
147
|
end
|
|
117
148
|
|
|
118
149
|
def suspicious_embeds?
|
|
119
|
-
|
|
120
|
-
(
|
|
150
|
+
count = embeds.size
|
|
151
|
+
(count == 1 && @text.length < 75) || count > 1
|
|
121
152
|
end
|
|
122
153
|
|
|
154
|
+
# No text in the container's own runs or in any text container. A child
|
|
155
|
+
# holding text answers before the subtree is searched.
|
|
123
156
|
def no_text_containers?
|
|
124
|
-
|
|
157
|
+
return false unless images.zero?
|
|
158
|
+
return true if @text.empty?
|
|
159
|
+
return false unless own_text_length.zero?
|
|
160
|
+
|
|
161
|
+
child = @node.first_element_child
|
|
162
|
+
while child
|
|
163
|
+
return false if TEXTISH_NAMES.include?(child.name) && !Dom.text(child).empty?
|
|
164
|
+
|
|
165
|
+
child = child.next_element
|
|
166
|
+
end
|
|
167
|
+
@node.css(TEXTISH).all? { |container| Dom.text(container).empty? }
|
|
125
168
|
end
|
|
126
169
|
|
|
127
170
|
# Galleries survive as long as every list item is a single image.
|
|
128
171
|
def image_gallery?
|
|
129
|
-
list? && @node.element_children.none? { |item| item.element_children.size > 1 } && images ==
|
|
172
|
+
list? && @node.element_children.none? { |item| item.element_children.size > 1 } && images == list_items
|
|
130
173
|
end
|
|
131
174
|
|
|
132
175
|
def paragraphs
|
|
133
176
|
@paragraphs ||= @node.css("p").size + @node.css("div").count { |div| @paragraph_keys.include?(Dom.key(div)) }
|
|
134
177
|
end
|
|
135
178
|
|
|
179
|
+
def embeds
|
|
180
|
+
@embeds ||= none_in_root?(EMBEDS) ? [] : @node.css(EMBEDS)
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def list_items
|
|
184
|
+
@list_items ||= none_in_root?("li") ? 0 : @node.css("li").size
|
|
185
|
+
end
|
|
186
|
+
|
|
136
187
|
def images
|
|
137
|
-
@images ||= @node.css("img").size
|
|
188
|
+
@images ||= none_in_root?("img") ? 0 : @node.css("img").size
|
|
138
189
|
end
|
|
139
190
|
|
|
140
191
|
def list?
|
|
@@ -167,7 +218,7 @@ module Nokodown
|
|
|
167
218
|
|
|
168
219
|
# Readability wraps a container's loose text in <p>, which then counts as text.
|
|
169
220
|
def own_text_length
|
|
170
|
-
loose = @node.
|
|
221
|
+
loose = Dom.children(@node).reject { |child| child.element? && Dom::PARAGRAPH_BREAKERS.include?(child.name) }
|
|
171
222
|
Dom.squish(loose.map(&:content).join).length
|
|
172
223
|
end
|
|
173
224
|
end
|
|
@@ -18,6 +18,11 @@ module Nokodown
|
|
|
18
18
|
REQUIRED_SHARE = { article: 0.15, product: 0.9 }.freeze
|
|
19
19
|
DEFAULT_SHARE = 0.7
|
|
20
20
|
|
|
21
|
+
# Sibling sections extend an article whose top candidate holds the share.
|
|
22
|
+
# When only they reach it, they must hold nearly the whole page: page mode
|
|
23
|
+
# would keep every band, and article mode drops the bands they leave out.
|
|
24
|
+
WHOLE_PAGE_SHARE = 0.9
|
|
25
|
+
|
|
21
26
|
# Page types whose replies are content: forum threads and Q&A pages.
|
|
22
27
|
DISCUSSIONS = %i[forum qa].freeze
|
|
23
28
|
|
|
@@ -26,13 +31,15 @@ module Nokodown
|
|
|
26
31
|
THREAD_SHARE = 0.25
|
|
27
32
|
THREAD_RUN_SHARE = 0.5
|
|
28
33
|
|
|
29
|
-
|
|
34
|
+
# stats: the counts of body as it is, if already made.
|
|
35
|
+
def initialize(body, page_type: nil, stats: nil)
|
|
30
36
|
@body = body
|
|
31
37
|
@page_type = page_type
|
|
38
|
+
@stats = stats
|
|
32
39
|
end
|
|
33
40
|
|
|
34
41
|
def call
|
|
35
|
-
stats = Stats.new(@body)
|
|
42
|
+
stats = @stats || Stats.new(@body)
|
|
36
43
|
root = main_region(stats) || @body
|
|
37
44
|
mark_thread(root, stats) if @page_type == :forum
|
|
38
45
|
result = Scorer.new(root, stats).call
|
|
@@ -58,13 +65,21 @@ module Nokodown
|
|
|
58
65
|
end
|
|
59
66
|
|
|
60
67
|
def article?(result, root, stats)
|
|
61
|
-
|
|
68
|
+
required = REQUIRED_SHARE.fetch(@page_type, DEFAULT_SHARE)
|
|
69
|
+
return true if holds_share?(result.core, root, stats, required)
|
|
70
|
+
return false if result.nodes.size == result.core.size
|
|
71
|
+
|
|
72
|
+
holds_share?(result.nodes, root, stats, [required, WHOLE_PAGE_SHARE].max)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def holds_share?(nodes, root, stats, required)
|
|
76
|
+
article_text = nodes.sum { |node| stats.text_chars(node) }
|
|
62
77
|
return false if article_text < MIN_ARTICLE_CHARS
|
|
63
78
|
|
|
64
|
-
page_text = stats.text_chars(root) - secondary_text(root,
|
|
79
|
+
page_text = stats.text_chars(root) - secondary_text(root, nodes, stats)
|
|
65
80
|
return true if page_text <= 0
|
|
66
81
|
|
|
67
|
-
article_text / page_text >=
|
|
82
|
+
article_text / page_text >= required
|
|
68
83
|
end
|
|
69
84
|
|
|
70
85
|
# Comment threads and "most popular" rails beside an article do not count
|
|
@@ -73,16 +88,16 @@ module Nokodown
|
|
|
73
88
|
article_keys = article_nodes.map { |node| Dom.key(node) }
|
|
74
89
|
article_ancestor_keys = article_nodes.flat_map { |node| node.ancestors.map { |ancestor| Dom.key(ancestor) } }
|
|
75
90
|
total = 0.0
|
|
76
|
-
stack =
|
|
91
|
+
stack = Dom.element_children(root)
|
|
77
92
|
until stack.empty?
|
|
78
93
|
node = stack.pop
|
|
79
94
|
key = Dom.key(node)
|
|
80
95
|
next if article_keys.include?(key) || Dom.thread?(node)
|
|
81
96
|
|
|
82
|
-
if Dom.
|
|
97
|
+
if Dom.secondary?(node) && !article_ancestor_keys.include?(key)
|
|
83
98
|
total += stats.text_chars(node)
|
|
84
99
|
else
|
|
85
|
-
stack.concat(
|
|
100
|
+
stack.concat(Dom.element_children(node))
|
|
86
101
|
end
|
|
87
102
|
end
|
|
88
103
|
total
|
|
@@ -99,17 +114,18 @@ module Nokodown
|
|
|
99
114
|
regions.group_by { |node| "#{node.name} #{Dom.key(node.parent)}" }.values
|
|
100
115
|
runs.each { |run| thread.concat(run) if run.sum { |node| stats.text_chars(node) } >= total * THREAD_RUN_SHARE }
|
|
101
116
|
thread.each { |node| Dom.mark_as_thread(node) }
|
|
117
|
+
stats.forget_keyless unless thread.empty?
|
|
102
118
|
end
|
|
103
119
|
|
|
104
120
|
def outermost_secondary(root)
|
|
105
121
|
found = []
|
|
106
|
-
stack =
|
|
122
|
+
stack = Dom.element_children(root)
|
|
107
123
|
until stack.empty?
|
|
108
124
|
node = stack.pop
|
|
109
|
-
if Dom.
|
|
125
|
+
if Dom.secondary?(node)
|
|
110
126
|
found << node
|
|
111
127
|
else
|
|
112
|
-
stack.concat(
|
|
128
|
+
stack.concat(Dom.element_children(node))
|
|
113
129
|
end
|
|
114
130
|
end
|
|
115
131
|
found
|