Skip to content

Commit

Permalink
Mutualize code between CSS stylesheets
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Oct 22, 2024
1 parent 86ba7eb commit 7c0ad2a
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions scraper/src/libretexts2zim/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,45 +312,26 @@ def run(self) -> Path:
add_item_for(creator, "content/logo.png", content=welcome_image.getvalue())
del welcome_image

items_to_download: dict[ZimPath, HttpUrl] = {}
screen_css = BytesIO()
stream_file(home.screen_css_url, byte_stream=screen_css)
url_rewriter = ArticleUrlRewriter(
article_url=HttpUrl(home.screen_css_url),
article_path=ZimPath("screen.css"),
self.items_to_download: dict[ZimPath, HttpUrl] = {}
self._process_css(

Check warning on line 316 in scraper/src/libretexts2zim/processor.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/processor.py#L315-L316

Added lines #L315 - L316 were not covered by tests
css_location=home.screen_css_url,
target_filename="screen.css",
creator=creator,
)
css_rewriter = CssRewriter(url_rewriter=url_rewriter, base_href=None)
result = css_rewriter.rewrite(content=screen_css.getvalue())
items_to_download = {**items_to_download, **url_rewriter.items_to_download}
add_item_for(creator, "content/screen.css", content=result)
del screen_css
del css_rewriter
del url_rewriter

print_css = BytesIO()
stream_file(home.print_css_url, byte_stream=print_css)
url_rewriter = ArticleUrlRewriter(
article_url=HttpUrl(home.print_css_url),
article_path=ZimPath("print.css"),
self._process_css(

Check warning on line 321 in scraper/src/libretexts2zim/processor.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/processor.py#L321

Added line #L321 was not covered by tests
css_location=home.print_css_url,
target_filename="print.css",
creator=creator,
)
css_rewriter = CssRewriter(url_rewriter=url_rewriter, base_href=None)
result = css_rewriter.rewrite(content=print_css.getvalue())
items_to_download = {**items_to_download, **url_rewriter.items_to_download}
add_item_for(creator, "content/print.css", content=result)
del print_css
del css_rewriter
del url_rewriter

url_rewriter = ArticleUrlRewriter(
article_url=HttpUrl(home.home_url), article_path=ZimPath("inline.css")
self._process_css(

Check warning on line 326 in scraper/src/libretexts2zim/processor.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/processor.py#L326

Added line #L326 was not covered by tests
css_location=home.home_url,
css_content="\n".join(home.inline_css),
target_filename="inline.css",
creator=creator,
)
css_rewriter = CssRewriter(url_rewriter=url_rewriter, base_href=None)
result = css_rewriter.rewrite(content=("\n".join(home.inline_css)))
items_to_download = {**items_to_download, **url_rewriter.items_to_download}
add_item_for(creator, "content/inline.css", content=result)

logger.info(f" Retrieving {len(items_to_download)} CSS assets...")
for asset_path, asset_url in items_to_download.items():
logger.info(f" Retrieving {len(self.items_to_download)} CSS assets...")

Check warning on line 333 in scraper/src/libretexts2zim/processor.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/processor.py#L333

Added line #L333 was not covered by tests
for asset_path, asset_url in self.items_to_download.items():
try:
css_asset = BytesIO()
stream_file(asset_url.value, byte_stream=css_asset)
Expand Down Expand Up @@ -402,3 +383,31 @@ def run(self) -> Path:
)

return zim_path

def _process_css(
self,
creator: Creator,
target_filename: str,
css_location: str,
css_content: str | bytes | None = None,
):
"""Process a given CSS stylesheet
Download content if necessary, rewrite CSS and add CSS to ZIM
"""
if not css_location:
raise ValueError(f"Cannot process empty css_location for {target_filename}")

Check warning on line 398 in scraper/src/libretexts2zim/processor.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/processor.py#L398

Added line #L398 was not covered by tests
if not css_content:
css_buffer = BytesIO()
stream_file(css_location, byte_stream=css_buffer)
css_content = css_buffer.getvalue()
url_rewriter = ArticleUrlRewriter(

Check warning on line 403 in scraper/src/libretexts2zim/processor.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/processor.py#L400-L403

Added lines #L400 - L403 were not covered by tests
article_url=HttpUrl(css_location),
article_path=ZimPath(target_filename),
)
css_rewriter = CssRewriter(url_rewriter=url_rewriter, base_href=None)
result = css_rewriter.rewrite(content=css_content)
self.items_to_download = {

Check warning on line 409 in scraper/src/libretexts2zim/processor.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/processor.py#L407-L409

Added lines #L407 - L409 were not covered by tests
**self.items_to_download,
**url_rewriter.items_to_download,
}
add_item_for(creator, f"content/{target_filename}", content=result)

Check warning on line 413 in scraper/src/libretexts2zim/processor.py

View check run for this annotation

Codecov / codecov/patch

scraper/src/libretexts2zim/processor.py#L413

Added line #L413 was not covered by tests

0 comments on commit 7c0ad2a

Please sign in to comment.