Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions bookworm/document/formats/epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def get_storage_content(self):
return self._storage_text

def _parse_html_content(self, *, include_images=True):
normalized_html_content = StructuredHtmlParser.preprocess_html_string(self.html_content)
normalized_html_content = self.html_content
self._legacy_content_cache_key = (
f"legacy-text-v1:{hashlib.sha256(normalized_html_content.encode('utf-8')).hexdigest()}"
)
Expand Down Expand Up @@ -456,7 +456,7 @@ def add_toc_entry(self, entries, parent):

@cached_property
def html_content(self):
cache_key = self.uri.to_uri_string()
cache_key = f"preprocessed-html-v1:{self.uri.to_uri_string()}"
document_path = self.get_file_system_path()
try:
with Cache(
Expand All @@ -473,7 +473,9 @@ def html_content(self):
for filename, html_content in html_content_gen:
buf.write(self.prefix_html_ids(filename, html_content))
buf.write("\n<br/>\n")
html_content = self.build_html(title=self.epub.title, body_content=buf.getvalue())
html_content = StructuredHtmlParser.preprocess_html_string(
self.build_html(title=self.epub.title, body_content=buf.getvalue())
)
try:
with Cache(
self._get_cache_directory(), eviction_policy="least-frequently-used"
Expand Down
16 changes: 16 additions & 0 deletions tests/test_epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bookworm.document import cache_utils
from bookworm.document.formats.epub import EpubDocument
from bookworm.document.uri import DocumentUri
from bookworm.structured_text.structured_html_parser import StructuredHtmlParser


def temp_book(title: str = "Sample book") -> epub.EpubBook:
Expand Down Expand Up @@ -211,6 +212,21 @@ def test_legacy_content_uses_disk_cache(asset, tmp_path, monkeypatch):
assert second_document.get_legacy_content() == legacy_content


def test_epub_html_cache_reuses_preprocessed_content(asset, tmp_path, monkeypatch):
monkeypatch.setattr(EpubDocument, "_get_cache_directory", lambda _: tmp_path / "cache")
preprocess = Mock(wraps=StructuredHtmlParser.preprocess_html_string)
monkeypatch.setattr(StructuredHtmlParser, "preprocess_html_string", preprocess)
uri = DocumentUri.from_filename(asset("The Diary of a Nobody.epub"))

first_document = EpubDocument(uri)
first_document.read()
second_document = EpubDocument(uri)
second_document.read()

assert second_document.get_content() == first_document.get_content()
assert preprocess.call_count == 1


def test_legacy_cache_uses_the_html_that_was_parsed(asset, tmp_path, monkeypatch):
monkeypatch.setattr(EpubDocument, "_get_cache_directory", lambda _: tmp_path / "cache")
epub_path = tmp_path / "book.epub"
Expand Down
Loading