nextract
A Mozilla Readability-like content extraction library for Nim
Summary
| Latest Version | 0.1.0 |
|---|---|
| License | MIT |
| CI Status | Failing |
| Downloads | 0 |
| Last Indexed | 2026-09-04 07:26 |
Tags
Authors
- bung87
Installation
nimble install nextract
choosenim install nextract
git clone https://github.com/nim-community/nextract
OS Compatibility
| Platform | Linux | macOS | Windows | FreeBSD | OpenBSD | NetBSD | Android | iOS | WASM | Embedded |
|---|---|---|---|---|---|---|---|---|---|---|
| nextract | ✓ | ✓ | ✓ | - | - | - | - | - | - | - |
Dependencies
| Package | Version | Optional |
|---|---|---|
| nim >= | 1.6.18 | No |
| chame >= | 1.0.0 | No |
| chagashi >= | 0.7.0 | No |
Source
| Repository | https://github.com/nim-community/nextract |
|---|---|
| Homepage | https://github.com/nim-community/nextract |
| Registry Source | nimble_official |
README
nextract
A content extraction library for Nim.
This library extracts article content from HTML web pages, inspired by Mozilla Readability. It uses chame for HTML5 parsing and chagashi for character encoding support.
Features
- Extract main article content from HTML pages
- Remove navigation, ads, sidebars, and other clutter
- Extract metadata (title, author, excerpt, site name, etc.)
- Convert relative URLs to absolute
- Configurable extraction options
isProbablyReaderablequick check
Installation
# In your .nimble file
requires "nextract"
Or install directly:
nimble install nextract
Dependencies
nim >= 1.6.18- Nim compilerchame >= 1.0.0- HTML5 parserchagashi >= 0.7.0- Character encoding support
Nim Version Compatibility
| Nim Version | Status |
|---|---|
| 1.6.18+ | ✅ Supported |
| 2.0.x | ✅ Supported |
| 2.2.x | ✅ Supported |
| devel | ✅ Supported |
Usage
Basic Usage
import nextract
let html = readFile("article.html")
let extractor = newExtractor(html, "https://example.com")
let article = extractor.parse()
if article.isSome:
echo "Title: ", article.get.title
echo "Author: ", article.get.byline
echo "Content: ", article.get.content
echo "Text: ", article.get.textContent
echo "Length: ", article.get.length
Quick Check
import nextract
let extractor = newExtractor(html)
if extractor.isProbablyReaderable():
let article = extractor.parse()
# Process article...
Custom Options
import nextract
let options = initOptions(
charThreshold = 500, # Minimum characters for valid article
keepClasses = false, # Preserve CSS classes
classesToPreserve = @[] # Classes to preserve when cleaning
)
let extractor = newExtractor(html, "https://example.com", options)
let article = extractor.parse()
API Reference
newExtractor(html, baseUri, options)
Creates a new content extractor instance.
html: string- The HTML content to parsebaseUri: string- Base URL for resolving relative linksoptions: ExtractOptions- Configuration options
parse()
Parses the document and extracts article content.
Returns Option[Article] containing:
- title: string - Article title
- content: string - Clean HTML content
- textContent: string - Plain text content
- length: int - Character count
- excerpt: string - Short description
- byline: string - Author information
- dir: string - Text direction (ltr/rtl)
- siteName: string - Site name
- lang: string - Content language
- publishedTime: Option[DateTime] - Publication time
isProbablyReaderable(minContentLength, minScore)
Quick check if document is likely to contain readable content.
minContentLength: int = 140- Minimum node content lengthminScore: int = 20- Minimum cumulated score
initOptions(...)
Creates configuration options:
charThreshold: int = 500- Minimum characters for articleclassesToPreserve: seq[string] = @[]- Classes to keepkeepClasses: bool = false- Preserve all classes
Algorithm
The library implements a content extraction algorithm inspired by Mozilla Readability:
- Preprocessing - Remove scripts, styles, and normalize the DOM
- Scoring - Score paragraphs and containers based on:
- Text length and punctuation (commas indicate sentences)
- Class/id names (positive: article, content, entry; negative: sidebar, footer, ad)
- Link density (high density = navigation)
- Candidate Selection - Pick the container with highest score
- Cleanup - Remove remaining clutter and fix URLs
License
MIT License
Credits
- Mozilla Readability - Inspiration and algorithm reference
- chame - HTML5 parser by ~bptato
- chagashi - Character encoding support by ~bptato