Skip to main content

Render website to ebook to make it easier to read on devices

Project description

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

After generating the configuration file, all we need to do is adding (blog posts, articles, webs)' urls to urls field in the configuration file.

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.

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:

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.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colusa-0.16.0.tar.gz (39.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

colusa-0.16.0-py3-none-any.whl (48.0 kB view details)

Uploaded Python 3

File details

Details for the file colusa-0.16.0.tar.gz.

File metadata

  • Download URL: colusa-0.16.0.tar.gz
  • Upload date:
  • Size: 39.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for colusa-0.16.0.tar.gz
Algorithm Hash digest
SHA256 aaa1b603ffea9516995c4e3594e9ed634216e1b7da79c7079797e2f438a71e21
MD5 ff004785d3689e5d346526c100fab41f
BLAKE2b-256 cd7eba01c370739f4704257753b278ced5fbeaabc27b519c55de83dc763f219a

See more details on using hashes here.

File details

Details for the file colusa-0.16.0-py3-none-any.whl.

File metadata

  • Download URL: colusa-0.16.0-py3-none-any.whl
  • Upload date:
  • Size: 48.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for colusa-0.16.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c19523565c2c70d4339568bb3a16874f0e9b887f0516be7ea386b724a8b5b445
MD5 93d96e4339916451d40eaf768fe364cc
BLAKE2b-256 6ba82c4178042f90690524c23326ec093f78c11b0a68bf6b568c6a748faf51e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page