4. Retrieval of extra resources from CSS

The CSSList encapsulates a collection of external references that are extracted from CSS files, references that are supposed to be downloaded and added to the final book, as well as added to the package file. Typically, this means handling the CSS import statements (i.e., importing other CSS files) as well as various URL references, e.g., when setting the content or the background of an element using an image.

Some CSS files may need to be changed on the fly. The typical case is when a background image is set through the CSS statement of the form:

background: url(//www.w3.org/StyleSheet/TR/logo);

(Which is the trick used to help in the HTTP vs. HTTPS negotiations in some of the W3C CSS files.) The URL reference must be changed, in this case, to a local, relative URL. These required cases are gathered by the process and the upper layers use it to make a simple string “replace” on the fly when the CSS files are copied to the book.

4.1. Module content

class rp2epub.cssurls.CSSList(base)[source]

List of CSSReference instances. This is, initially, built up from the document.Document class; when the final information is requested, a recursion is done on the collected CSS file references to collect all outstanding resources.

Parameters:base (str) – the base URL for the whole book
add_css(origin_url, is_file=True, content=None)[source]

Add a new CSS, ie, add a new CSSReference to the internal array of references

Parameters:
  • origin_url (str) – URL of the CSS file (if any, otherwise value is ignored)
  • is_file (boolean) – whether the CSS is to be retrieved via the URL or whether it was embedded
  • content (str) – in case the CSS was embedded, the full content of the CSS
change_patterns

Array of (from,to) pairs used to replace strings in CSS files when copying into the book

get_download_list()[source]

Return all the list of resources that must be downloaded and added to the book. These include those explicitly added via add_css(), plus those retrieved recursively.

Returns:List of (local_name, absolute_url) pairs.
class rp2epub.cssurls.CSSReference(base, url, is_file=True, content=None)[source]

Wrapper around the information related to one CSS reference.

Parameters:
  • base (str) – Base URI of the overall book. Important to generate proper local name for a resource when retrieved.
  • url (str) – URL of the CSS file (if any, otherwise value is ignored). This is an absolute URL; in practice it is based on the book URL or www.w3.org
  • is_file (boolean) – whether the CSS is to be retrieved via the URL or whether it was embedded in HTML
  • content (str) – in case the CSS was embedded, the full content of the CSS as retrieved from the DOM
_collect_imports()[source]

Collect the resources to be imported. The CSS content is parsed, and the import_css and import_misc sets are filled with content. This method is called at initialization time.

change_patterns

Array of (from,to) pairs used to replace strings in CSS files when copying into the book

empty

A boolean value whether the CSS content is empty (in which case it can be ignored) or not

import_css

Set of URL-s for additional CSS files, ie, values of @import rules

import_misc

Set of _URLPair instances for resources that were found in the CSS content

class rp2epub.cssurls._URLPair(url, name)[source]

A simple wrapper around a pair of (absolute) url, and a local name. The values can then be accessed via property names (and not via array/dictionary syntax).

Parameters:
  • url (str) – Absolute URL of the resource
  • name (str) – Local name of the resource
name

The local name of the resource

url

The absolute URL of the resource