nokodown 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/CHANGELOG.md +12 -0
- data/LICENSE +202 -0
- data/NOTICE +10 -0
- data/README.md +246 -0
- data/lib/nokodown/boilerplate.rb +236 -0
- data/lib/nokodown/charset.rb +27 -0
- data/lib/nokodown/container_check.rb +174 -0
- data/lib/nokodown/content_locator.rb +118 -0
- data/lib/nokodown/dom.rb +140 -0
- data/lib/nokodown/extractor.rb +137 -0
- data/lib/nokodown/markdown/cleanup.rb +111 -0
- data/lib/nokodown/markdown/code_block.rb +71 -0
- data/lib/nokodown/markdown/image.rb +63 -0
- data/lib/nokodown/markdown/image_cleanup.rb +166 -0
- data/lib/nokodown/markdown/inline.rb +340 -0
- data/lib/nokodown/markdown/redundant_images.rb +142 -0
- data/lib/nokodown/markdown/renderer.rb +339 -0
- data/lib/nokodown/markdown/table.rb +93 -0
- data/lib/nokodown/markdown/text.rb +79 -0
- data/lib/nokodown/metadata.rb +316 -0
- data/lib/nokodown/patterns.rb +152 -0
- data/lib/nokodown/preprocessor.rb +226 -0
- data/lib/nokodown/product_reviews.rb +90 -0
- data/lib/nokodown/pruner.rb +337 -0
- data/lib/nokodown/result.rb +8 -0
- data/lib/nokodown/scorer.rb +230 -0
- data/lib/nokodown/stats.rb +79 -0
- data/lib/nokodown/tables.rb +62 -0
- data/lib/nokodown/url_resolver.rb +249 -0
- data/lib/nokodown/version.rb +5 -0
- data/lib/nokodown.rb +54 -0
- metadata +93 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3f7e9d1e1249f8e65e9402b110bdd9896558944949ed5d746b38c587c5ed3753
|
|
4
|
+
data.tar.gz: 2dba5af57c129f03a1915a29cd7f387917d115e0d430f72e5d81b41035eb006a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f3526fb214d5ae49f41b919a10712e35b7b931368fd2b2bf9aa7c37252b3b30e21b03709f16670c5cf1c60beba7d457d4aa95d5a65785b42098d9fd17aea9335
|
|
7
|
+
data.tar.gz: 2909458364fd34ad9bf42bacc19cc80a6c7a8f0b44688178bf0fdbe6826f00f38b664cbbf695784461358e9945ca57136945eed2f660ac76e2c2cf47b7f5ecb0
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 (2026-11-02)
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- `Nokodown.extract` returns the main content of a web page as Markdown,
|
|
8
|
+
with Readability-style metadata (title, byline, excerpt, site name,
|
|
9
|
+
published time, language).
|
|
10
|
+
- `Nokodown.to_markdown` returns only the Markdown.
|
|
11
|
+
- Works on articles and on home, listing, product, documentation, forum and
|
|
12
|
+
service pages.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/NOTICE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
nokodown
|
|
2
|
+
Copyright 2026 SerpApi
|
|
3
|
+
|
|
4
|
+
The content scoring, conditional cleaning, title and metadata heuristics are
|
|
5
|
+
adapted from Readability.js (https://github.com/mozilla/readability),
|
|
6
|
+
licensed under the Apache License, Version 2.0. Its NOTICE reads:
|
|
7
|
+
|
|
8
|
+
Readability
|
|
9
|
+
Copyright (c) 2010 Arc90 Inc
|
|
10
|
+
Copyright (c) 2010-2026 Mozilla and Contributors
|
data/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://serpapi.com">
|
|
3
|
+
<img src="https://serpapi.com/assets/media_kit/logo-with-wordmark-gradient.svg" alt="SerpApi" width="220">
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
# nokodown
|
|
8
|
+
|
|
9
|
+
Any web page to main-content Markdown, at Nokolexbor speed.
|
|
10
|
+
|
|
11
|
+
Extracts the main content of a web page as clean Markdown, together with the
|
|
12
|
+
metadata Readability returns (title, byline, excerpt, site name, publish date,
|
|
13
|
+
language). It works on articles and on everything else: home pages, search
|
|
14
|
+
results, product and pricing pages, documentation, forums.
|
|
15
|
+
|
|
16
|
+
Built on [Nokolexbor](https://github.com/serpapi/nokolexbor), for the
|
|
17
|
+
`include_markdown` option of SerpApi's [Extract API][extract]. The name is
|
|
18
|
+
Nokolexbor's "noko" plus Markdown's "down".
|
|
19
|
+
|
|
20
|
+
> **Don't want to fetch the pages yourself?** SerpApi's [Extract API][extract]
|
|
21
|
+
> takes a URL and returns the page's Markdown as JSON, along with its meta
|
|
22
|
+
> tags, structured data, links and images.
|
|
23
|
+
> [Try the Extract API][extract].
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
gem "nokodown"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Ruby 2.7 or newer, nokolexbor 0.6 or a later 0.x release. nokolexbor 0.7 and
|
|
32
|
+
later need Ruby 3.1. Bundler picks 0.6.4 on older Rubies, but `gem install`
|
|
33
|
+
does not, so install that version first:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
gem install nokolexbor -v 0.6.4
|
|
37
|
+
gem install nokodown
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
require "nokodown"
|
|
44
|
+
|
|
45
|
+
result = Nokodown.extract(html, url: "https://example.com/post")
|
|
46
|
+
|
|
47
|
+
result.markdown # "# Post title\n\nFirst paragraph..."
|
|
48
|
+
result.title # "Post title"
|
|
49
|
+
result.byline # "Jane Doe"
|
|
50
|
+
result.excerpt # "A one-line summary."
|
|
51
|
+
result.site_name # "Example"
|
|
52
|
+
result.published_time # "2026-09-30T08:00:00Z"
|
|
53
|
+
result.lang # "en"
|
|
54
|
+
result.mode # :article, :page, :full or :none
|
|
55
|
+
|
|
56
|
+
Nokodown.to_markdown(html, url: "https://example.com/post") # just the Markdown
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`html` is a string in any encoding. Binary strings are decoded using their
|
|
60
|
+
`<meta charset>`, and invalid bytes are dropped.
|
|
61
|
+
|
|
62
|
+
`Nokodown.extract`, `Nokodown.to_markdown` and `Nokodown::Result` are the
|
|
63
|
+
public API. Other classes are internal and can change in any release.
|
|
64
|
+
|
|
65
|
+
| Option | Default | |
|
|
66
|
+
| --- | --- | --- |
|
|
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, dropping only scripts, styles, hidden elements and form controls. |
|
|
69
|
+
| `title` | `true` | Start with the page title as a `#` heading when the content has no `<h1>`. 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
|
+
| `images` | `true` | Include images. Emoji images are always kept as their text. |
|
|
71
|
+
| `links` | `true` | Keep links. `false` keeps only their text. |
|
|
72
|
+
|
|
73
|
+
`mode` says how the content was chosen: `:article` when the page was narrowed
|
|
74
|
+
to one body of prose, `:page` when the whole main region was kept, `:full`
|
|
75
|
+
with `main_content: false`, `:none` for a document without a body.
|
|
76
|
+
|
|
77
|
+
## What it removes
|
|
78
|
+
|
|
79
|
+
- Site navigation, headers, footers, sidebars, breadcrumbs, pagination and
|
|
80
|
+
tables of contents, found through HTML semantics (`nav`, page-level `header`
|
|
81
|
+
and `footer`, ARIA roles, schema.org types) and class/id names.
|
|
82
|
+
- Cookie banners, ads, newsletter and sign-in prompts, share bars, modals,
|
|
83
|
+
chat widgets, "related", "recommended" and "most popular" boxes, and, next
|
|
84
|
+
to an article, its comment thread.
|
|
85
|
+
- On product pages, customer reviews and Q&A widgets, with their review
|
|
86
|
+
summaries and rating histograms. An inline rating badge ("212 reviews" next
|
|
87
|
+
to the price), testimonials and editorial reviews stay.
|
|
88
|
+
- Invisible elements (`hidden`, `aria-hidden`, inline `display: none`,
|
|
89
|
+
screen-reader-only and other hiding classes), keeping collapsed accordions,
|
|
90
|
+
tab panels and FAQ answers.
|
|
91
|
+
- Tracking pixels, spacers, icons, placeholders, README badges, click-tracking
|
|
92
|
+
URL parameters (`utm_*`, `gclid`, `fbclid`, ...), permalink anchors,
|
|
93
|
+
"Read more" links, code line numbers, and repeated responsive copies of the
|
|
94
|
+
same image.
|
|
95
|
+
- In the main content, images marked decorative (`role="presentation"` or
|
|
96
|
+
`role="none"`, and outside an `:article` also `alt=""`) unless they sit in a
|
|
97
|
+
figure, an image link or a text-free paragraph or table cell, or declare a
|
|
98
|
+
width or height of 300 pixels or more; and card thumbnails while the
|
|
99
|
+
Markdown still has a text link to the same page. "Read more" or
|
|
100
|
+
"3 comments" never names the page, and a label without letters ("$40") or
|
|
101
|
+
with only a date ("Oct 1, 2026") names it only for an image without alt
|
|
102
|
+
text. Any other text does, so in `:page` mode a thumbnail beside a
|
|
103
|
+
"Shop now" link goes too; in an `:article`, only one outside a figure whose
|
|
104
|
+
alt is a word or two, or is mostly the link text.
|
|
105
|
+
- Later copies of an image that stands alone on its line: always for an
|
|
106
|
+
image without alt text, and for one with alt text only when nothing but
|
|
107
|
+
images came since the last copy, as in a logo strip shown twice. A line
|
|
108
|
+
with several copies of one image, such as a row of rating stars, keeps
|
|
109
|
+
them all.
|
|
110
|
+
|
|
111
|
+
Every structural rule is guarded: an element that contains the `<main>`
|
|
112
|
+
region, or most of the page's text, is never removed because of its class name.
|
|
113
|
+
|
|
114
|
+
## How it works
|
|
115
|
+
|
|
116
|
+
1. **Charset**: transcode to UTF-8; Lexbor itself ignores `<meta charset>`.
|
|
117
|
+
2. **Metadata**: JSON-LD, Open Graph and other `<meta>` tags, then the
|
|
118
|
+
document, in Readability's order of preference.
|
|
119
|
+
3. **Preprocessor**: restore lazy-loaded images (`data-src`, `srcset`,
|
|
120
|
+
`<noscript>` copies), drop scripts, invisible elements and form controls.
|
|
121
|
+
4. **Boilerplate**: remove page chrome and noise widgets across the whole page.
|
|
122
|
+
5. **ContentLocator**: score paragraphs with Readability's algorithm. When one
|
|
123
|
+
candidate holds most of the page's prose the content is narrowed to it
|
|
124
|
+
(`:article`); otherwise the whole main region is kept (`:page`), because a
|
|
125
|
+
listing or a product page spreads its content over many small blocks. The
|
|
126
|
+
share required depends on the page type its metadata declares. On forum
|
|
127
|
+
threads (schema.org types or the forum software's generator tag) replies
|
|
128
|
+
are content even in comment-named containers, and on forums and Q&A pages
|
|
129
|
+
where no single post holds the page, the whole main region is cleaned as an
|
|
130
|
+
article.
|
|
131
|
+
6. **Pruner**: in articles, Readability's conditional cleaning plus removal of
|
|
132
|
+
bylines, datelines and inline boxes of links to other stories; on other
|
|
133
|
+
pages, removal of leftover link clusters, unless they hold most of the text
|
|
134
|
+
as on an index page. In both modes, short links and boxes styled as
|
|
135
|
+
buttons ("Add to cart", "Book a demo") are removed, except for disclosure
|
|
136
|
+
toggles, buttons inside a sentence and titles or labels styled as buttons.
|
|
137
|
+
If article cleaning removes almost everything, it is undone and light
|
|
138
|
+
cleaning is used instead.
|
|
139
|
+
7. **Markdown**: headings, emphasis, links, images, nested and ordered lists,
|
|
140
|
+
task lists, block quotes, fenced code with the language from the
|
|
141
|
+
highlighter's classes, GFM tables (rowspans and colspans filled in) for data
|
|
142
|
+
tables while layout tables are unwrapped, definition lists, details, TeX
|
|
143
|
+
from MathML, superscript and subscript digits as Unicode (10⁶, H₂O) and
|
|
144
|
+
other scripts as 2^(n-1) and a_n. Text is escaped only where it would
|
|
145
|
+
otherwise change meaning.
|
|
146
|
+
|
|
147
|
+
## Quality
|
|
148
|
+
|
|
149
|
+
### All kinds of pages: WCXB
|
|
150
|
+
|
|
151
|
+
[WCXB][wcxb] has 2,008 human-reviewed pages: 1,050 articles and 958 product,
|
|
152
|
+
collection, listing, forum, documentation and service (SaaS, pricing) pages,
|
|
153
|
+
split into a development set (1,497) and a test set (511). The test set was
|
|
154
|
+
not held out: some of its pages were studied while tuning nokodown, and 139
|
|
155
|
+
of them have the same HTML as a development page. Scores use
|
|
156
|
+
its metric: word-level F1 against the reference main content, plus the share
|
|
157
|
+
of "must include" snippets found and of boilerplate snippets leaked. All
|
|
158
|
+
systems ran on the same pages with links, images and tables on; their Markdown
|
|
159
|
+
was converted back to text for scoring, and tokens were counted on the Markdown
|
|
160
|
+
an LLM would read (o200k tokenizer). Word F1 is identical to WCXB's
|
|
161
|
+
`evaluate.py`. The snippet rates average only the pages that have snippets and
|
|
162
|
+
compare text with whitespace collapsed, while `evaluate.py` counts a page
|
|
163
|
+
without snippets as 100% and compares raw text, so its rates differ by about a
|
|
164
|
+
point.
|
|
165
|
+
|
|
166
|
+
| System | F1 (dev) | F1 (test) | Boilerplate leaked (test) | Content snippets found (test) | Tokens per page (test) | Tokens on URLs (test) |
|
|
167
|
+
| --- | --- | --- | --- | --- | --- | --- |
|
|
168
|
+
| nokodown | 0.836 | 0.874 | 8.3% | 83.6% | 2,718 | 21% |
|
|
169
|
+
| rs-trafilatura 0.1.1 | 0.838 | 0.873 | 12.7% | 87.7% | 2,664 | 18% |
|
|
170
|
+
| Trafilatura 2.3.1 | 0.816 | 0.858 | 8.1% | 81.8% | 2,552 | 17% |
|
|
171
|
+
| Readability.js 0.6 + Turndown | 0.770 | 0.830 | 10.0% | 76.5% | 2,899 | 23% |
|
|
172
|
+
| Main-region baseline | 0.782 | 0.802 | 43.2% | 90.1% | 4,356 | 35% |
|
|
173
|
+
|
|
174
|
+
F1 by page type on the test set (257 articles; 28 to 59 pages of each other type):
|
|
175
|
+
|
|
176
|
+
| System | Article | Product | Collection | Listing | Forum | Documentation | Service |
|
|
177
|
+
| --- | --- | --- | --- | --- | --- | --- | --- |
|
|
178
|
+
| nokodown | 0.949 | 0.806 | 0.736 | 0.724 | 0.783 | 0.922 | 0.807 |
|
|
179
|
+
| rs-trafilatura 0.1.1 | 0.942 | 0.740 | 0.732 | 0.714 | 0.829 | 0.953 | 0.801 |
|
|
180
|
+
| Trafilatura 2.3.1 | 0.934 | 0.719 | 0.589 | 0.757 | 0.786 | 0.941 | 0.817 |
|
|
181
|
+
| Readability.js 0.6 + Turndown | 0.931 | 0.634 | 0.614 | 0.637 | 0.711 | 0.882 | 0.805 |
|
|
182
|
+
| Main-region baseline | 0.874 | 0.666 | 0.647 | 0.727 | 0.679 | 0.906 | 0.727 |
|
|
183
|
+
|
|
184
|
+
"Main-region baseline" converts the page's `<main>`, else its only
|
|
185
|
+
`<article>`, else `<body>`, with nokodown's converter and no main-content
|
|
186
|
+
detection. See [benchmark/compare](benchmark/compare/README.md) to reproduce
|
|
187
|
+
the table.
|
|
188
|
+
|
|
189
|
+
```sh
|
|
190
|
+
bundle exec ruby benchmark/wcxb.rb tmp/corpora/wcxb test
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Articles
|
|
194
|
+
|
|
195
|
+
Scored with the [Scrapinghub article extraction benchmark][benchmark]'s metric
|
|
196
|
+
(precision and recall over 4-token shingles, averaged per page). The Markdown is
|
|
197
|
+
converted back to text and the leading title is removed, since both references
|
|
198
|
+
leave the headline out of the body.
|
|
199
|
+
|
|
200
|
+
| Corpus | Pages | F1 | Precision | Recall |
|
|
201
|
+
| --- | --- | --- | --- | --- |
|
|
202
|
+
| [Scrapinghub][benchmark], human-labelled article bodies | 181 | 0.945 | 0.915 | 0.978 |
|
|
203
|
+
| [Mozilla Readability test pages][mozilla], Readability.js output as reference | 130 | 0.941 | 0.953 | 0.929 |
|
|
204
|
+
|
|
205
|
+
Published results on the same Scrapinghub corpus, for comparison:
|
|
206
|
+
Trafilatura 0.958, newspaper4k 0.949, Readability.js 0.947, go-readability
|
|
207
|
+
0.934, readability-lxml 0.922, goose3 0.896, jusText 0.804, html2text 0.662.
|
|
208
|
+
|
|
209
|
+
```sh
|
|
210
|
+
bundle exec rake benchmark:quality # downloads the corpora at pinned commits into tmp/
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Speed
|
|
214
|
+
|
|
215
|
+
Per page over the Scrapinghub corpus (pages of 20 KB to 1.4 MB, median
|
|
216
|
+
111 KB), median of 5 runs, on an Apple Silicon laptop with nokolexbor 0.6.4:
|
|
217
|
+
|
|
218
|
+
| Ruby | Median | p90 | p99 | Max |
|
|
219
|
+
| --- | --- | --- | --- | --- |
|
|
220
|
+
| 2.7.8 | 6.4 ms | 11.6 ms | 30.3 ms | 59.8 ms |
|
|
221
|
+
| 3.4.5 | 7.0 ms | 12.8 ms | 34.4 ms | 63.1 ms |
|
|
222
|
+
|
|
223
|
+
```sh
|
|
224
|
+
bundle exec rake benchmark:speed
|
|
225
|
+
ruby benchmark/speed.rb DIRECTORY --per-page # any folder of .html or .html.gz files
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Development
|
|
229
|
+
|
|
230
|
+
```sh
|
|
231
|
+
bundle install
|
|
232
|
+
bundle exec rspec
|
|
233
|
+
NOKOLEXBOR_VERSION=0.6.4 bundle exec rspec # the last nokolexbor for Ruby 2.7
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Specs use RSpec's `should` syntax.
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
Apache License 2.0. The scoring and cleaning heuristics are adapted from
|
|
241
|
+
[Readability.js][mozilla]; see [NOTICE](NOTICE).
|
|
242
|
+
|
|
243
|
+
[benchmark]: https://github.com/scrapinghub/article-extraction-benchmark
|
|
244
|
+
[extract]: https://serpapi.com/extract-api
|
|
245
|
+
[mozilla]: https://github.com/mozilla/readability
|
|
246
|
+
[wcxb]: https://github.com/Murrough-Foley/web-content-extraction-benchmark
|