pypublib.edit module
- pypublib.edit.clean_unused_styles(content_dir)[source]
Clean all unused CSS selectors from CSS files in the content directory.
Recursively processes all CSS files in a directory, identifying and removing selectors that are not used in any HTML/XHTML file.
- Parameters:
content_dir (str) – Path to the directory containing content files.
- pypublib.edit.collect_used_selectors(content_dir)[source]
Collect all used CSS class and id selectors from XHTML/HTML files.
Scans all HTML/XHTML files in a directory tree and extracts all class and id selectors that are actually used in the markup.
- Parameters:
content_dir (str) – Path to the directory containing content files.
- Returns:
Set of used CSS selectors (e.g., {‘.classname’, ‘#idname’}).
- Return type:
set
- pypublib.edit.edit_all_chapters(book, replacements)[source]
Edit all chapters of a book with the given replacements.
Applies string replacements to the content of all chapters in the book.
- Parameters:
book (Book) – Book object with ‘chapters’ attribute (dictionary of Chapter objects).
replacements (list) – List of strings in format ‘old=new’.
- Return type:
None
Example
>>> edit_all_chapters(book, ["Chapter=Section", "Old=New"])
- pypublib.edit.edit_chapter(chapter, replacements)[source]
Replace all occurrences in chapter content according to replacements.
- Parameters:
chapter (Chapter) – The chapter to edit.
replacements (list) – List of strings in format ‘old=new’.
- Return type:
None
Example
>>> edit_chapter(chapter, ["Hello=Hi", "world=Earth"])
- pypublib.edit.edit_chapter_tag(chapter, tag, element, old_value, value)[source]
Replace all occurrences of a value in a specific tag’s element/attribute.
Finds all instances of a tag and replaces text or attribute values.
- Parameters:
chapter (Chapter) – The chapter to edit.
tag (str) – Tag name to find (e.g., ‘img’, ‘a’, ‘p’).
element (str) – Attribute or element name (e.g., ‘src’, ‘href’, or ‘text’).
old_value (str) – Value to be replaced.
value (str) – New value.
- Return type:
None
Example
>>> edit_chapter_tag(chapter, 'img', 'src', 'old.jpg', 'new.jpg')
- pypublib.edit.edit_chapter_tags(chapter, replacements)[source]
Replace values in specific tags in chapter content using regex patterns.
- Parameters:
chapter (Chapter) – Chapter object with ‘html’ attribute.
replacements (list) – List of strings in format ‘tag=old=new’.
- Returns:
The modified chapter.
- Return type:
Example
>>> edit_chapter_tags(chapter, ['p=old text=new text', 'strong=bad=good'])
- pypublib.edit.edit_chapters(chapter, replacements)[source]
Apply replacements to a list of chapters.
- Parameters:
chapter (list[Chapter]) – List of Chapter instances to edit.
replacements (list) – List of strings in format ‘old=new’.
- Return type:
None
See also
edit_chapter()for single chapter editing.
- pypublib.edit.process_css_file(css_path, used_selectors)[source]
Remove unused CSS selectors from a CSS file.
Reads a CSS file, identifies rules with selectors not in the used set, and removes them, writing the cleaned CSS back to the file.
- Parameters:
css_path (str) – Path to the CSS file to process.
used_selectors (set) – Set of CSS selectors that are actually used.
- Returns:
List of removed selectors.
- Return type:
list
- pypublib.edit.remove_unnecessary_files(book)[source]
Remove unreferenced image and CSS files from the book.
Keeps resources that are referenced by at least one chapter via stylesheet links or image tags. The cover image is always preserved if set.
- pypublib.edit.remove_unused_styles(book)[source]
Remove unused CSS selectors from the book’s styles.
Analyzes which CSS selectors are actually used in the book’s chapters and removes all unused selectors from the stylesheets. This reduces file size and improves EPUB efficiency.
- Parameters:
book (Book) – Book object with ‘styles’ attribute (dictionary of CSS content).
- Returns:
The book with cleaned stylesheets.
- Return type:
Note
This function creates temporary files during processing. Any style files that become empty after cleaning are removed from the book.