colusa
Render website to ebook to make it easier to read on devices.
Installation
pip install colusa
For development:
pip install -e ".[all]"
Usage
Start from scratch
First of all, we need to generate a configuration file for colusa to work on.
colusa has builtin command to generate a template configuration as starter.
Run following command to generate configuration file:
$ colusa init new_ebook.json
colusa will generate a configuration file as below:
{
"title": "__fill the title__",
"author": "__fill the author__",
"version": "v1.0",
"homepage": "__fill url to home page__",
"output_dir": "__fill output dir__",
"urls": []
}
We have to modify the configuration file to fill up valid information.
Add content to ebook
You can add URLs to the config manually, or use the add-url command to append them from the terminal:
# Append a plain URL
$ colusa add-url new_ebook.json https://fs.blog/2018/04/first-principles/
# Auto-fetch the page title
$ colusa add-url new_ebook.json https://fs.blog/2018/04/first-principles/ --fetch-title
# Supply metadata explicitly
$ colusa add-url new_ebook.json https://fs.blog/2018/04/first-principles/ \
--title "First Principles" --author "Farnam Street" --published "2018-04-01"
# For multi-part books, specify the target part
$ colusa add-url new_ebook.json https://fs.blog/2018/04/first-principles/ --part "Part 1"
Alternatively, edit the config file directly and add URLs to the urls field.
Example for final configuration:
{
"title": "The Great Mental Models",
"author": "Farnam Street Media Inc",
"version": "v1.0",
"homepage": "https://fs.blog",
"output_dir": "fsblog",
"urls": [
"https://fs.blog/2018/04/first-principles/",
"https://fs.blog/2016/04/second-order-thinking/",
"https://fs.blog/2017/06/thought-experiment/",
"https://fs.blog/2018/05/probabilistic-thinking/",
"https://fs.blog/2019/12/survivorship-bias/"
]
}
Update ebook content
We can update ebook content by modifying the urls, by adding or removing url in the urls, the result ebook will be changed.
Generate ebook content
After adding or removing url in urls, we need to invoke colusa to have it regenerate ebook content. Run following command at terminal:
$ colusa generate new_ebook.json
By invoking above command, colusa will download webpages (specified in urls), parse, transform them to asciidoc format, and save them to output_dir. colusa also create a neccessary information for ebook compilating at later steps.
Compile directly from colusa
If asciidoctor tools are installed, you can compile the ebook in one step:
# Generate AsciiDoc and immediately compile to EPUB
$ colusa generate new_ebook.json --build epub
# Build multiple formats at once
$ colusa generate new_ebook.json --build epub --build html
# Compile an already-generated book (without re-downloading)
$ colusa build new_ebook.json --format epub
$ colusa build new_ebook.json # builds html, epub, and pdf
colusa uses asciidoctor, asciidoctor-epub3, and asciidoctor-pdf. Install them from:
- HTML: https://asciidoctor.org
- EPUB: https://asciidoctor.org/docs/asciidoctor-epub3/
- PDF: https://asciidoctor.org/docs/asciidoctor-pdf/
Preview before generating
Use --dry-run to preview which extractor and transformer would be selected for each URL, without downloading anything or writing any files:
$ colusa generate new_ebook.json --dry-run
Example output:
[dry-run] Config: new_ebook.json
[dry-run] Output dir: fsblog/
[dry-run] Total URLs: 3
[1/3] https://staffeng.com/guides/overview
Extractor : StaffEng (plugin)
Transformer: StaffEng (plugin)
[2/3] https://medium.com/@user/some-article
Extractor : Medium (plugin)
Transformer: Transformer (base)
[3/3] https://unknown-site.com/article
Extractor : Extractor (base)
Transformer: Transformer (base)
Supporting Unsupported Websites
If a website is not in the supported list, you can define CSS-selector-based parsing rules directly in your book config without touching colusa's code. Dynamic rules are evaluated before built-in plugins; the first matching rule wins.
Add a site_rules list to your config:
{
"title": "My Ebook",
"author": "Me",
"version": "v1.0",
"homepage": "https://example.com",
"output_dir": "my_ebook",
"site_rules": [
{
"pattern": "//example.com",
"content": "article.post-body",
"title": "h1.article-title",
"author": ".author-name",
"published": "time.publish-date",
"cleanup": ["div.ads", "nav.sidebar"]
}
],
"urls": [
"https://example.com/some-article"
]
}
| Field | Description |
|---|---|
pattern |
Regex matched against the full URL (e.g. //example.com) |
content |
CSS selector for the article body. Falls back to built-in detection if omitted or not found |
title |
CSS selector for the article title. Falls back to built-in defaults if omitted or not found |
author |
CSS selector for the author name. Falls back to built-in defaults if omitted or not found |
published |
CSS selector for the publish date. Falls back to built-in defaults if omitted or not found |
cleanup |
List of CSS selectors — matching elements are removed from the extracted content |
External rules file
Rules can also be kept in a separate YAML or JSON file and shared across multiple book configs:
{
"site_rules_file": "./my-sites.yml"
}
my-sites.yml:
- pattern: "//example.com"
content: article.post-body
title: h1.article-title
cleanup:
- div.ads
- nav.sidebar
Inline site_rules and site_rules_file are merged; inline rules are checked first. Relative paths in site_rules_file are resolved from the directory containing the config file.
Compile ebook for consuming purpose
Prerequisites
Before generating ebook, we need to install asciidoctor tools. Follow install guideline on following websites:
- for generating html: https://asciidoctor.org
- for generating epub: https://asciidoctor.org/docs/asciidoctor-epub3/
- for generating pdf: https://asciidoctor.org/docs/asciidoctor-pdf/
Generating ebooks
To help with generating ebook, colusa also create a Makefile in the root folder of the ebook. In the Makefile, there are three common targets that we can use to generate ebook in html, epub, pdf formats.
# to generate html
$ make html
# to generate epub
$ make epub
# to generate pdf
$ make pdf
Generated ebooks will be saved to ./output folder.
user:output/ $ ls [10:55:55]
total 3056
drwxr-xr-x 10 320B images
-rw-r--r-- 1 699K index.epub
-rw-r--r-- 1 131K index.html
-rw-r--r--@ 1 694K index.pdf
List of Supported Websites
Currently colusa has built-in support for the following websites. Any other site can be handled using dynamic site rules.
- https://untools.co
- https://unintendedconsequenc.es
- https://blog.acolyer.org
- https://fs.blog
- https://increment.com
- https://slack.engineering
- https://medium.com
- https://www.cs.rutgers.edu/~pxk/
- https://www.preethikasireddy.com
- https://engineering.atspotify.com
- https://truyenfull.vn
- https://avikdas.com
- https://www.infoq.com
Contribution
Contribution is welcome. You can open issues to request for supporting more websites, open PR to help with those issues, or anything else like documentation, code contribution.
Metadata
Release files for colusa 0.19.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| colusa-0.19.1.tar.gz | 52.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| colusa-0.19.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 105.3 kB
Release files / colusa-0.19.1.tar.gz
| Download URL | colusa-0.19.1.tar.gz |
|---|---|
| Size | 52.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e4156a2b546cdc9672c8e1fcdca0b80c642b727b34a41b312db96ac19fd7e7f
|
|
BLAKE2b-256 checksum How to use checksums |
72ef4dc1a361885f8c5a63368387b58e796ea27671555fc089235fd16b06025b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / colusa-0.19.1-py3-none-any.whl
| Download URL | colusa-0.19.1-py3-none-any.whl |
|---|---|
| Size | 52.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
547114b489d1901385188a28720723c25765cf767cdb5eecc339118cc5f45e25
|
|
BLAKE2b-256 checksum How to use checksums |
f11ab77d36b512894182d8f97689fd4461850873ab1b0383fed6a13a64f8139f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Security for colusa 0.19.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.