Skip to content

Commit

Permalink
build: Only include Kobo/Kindle se.css compatiblity fixes in those pa…
Browse files Browse the repository at this point in the history
…rticular builds, not all builds
  • Loading branch information
acabal committed Jul 15, 2024
1 parent 6e5f74e commit bf3151c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion se/data/templates/kindle.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* kindle compatibility CSS */
/* Kindle compatibility CSS */

.endnotes p[id],
.footnotes p[id]{
margin-top: 1em;
Expand Down
8 changes: 8 additions & 0 deletions se/data/templates/kobo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Kobo compatibility CSS */

section[epub|type~="titlepage"] h1,
section[epub|type~="titlepage"] p,
section[epub|type~="colophon"] h2,
section[epub|type~="imprint"] h2{
font-size: 0; /* Required for Kobo not to add an extra page to the title */
}
8 changes: 8 additions & 0 deletions se/data/templates/se-kindle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Kindle compatibility CSS */

section[epub|type~="titlepage"] h1,
section[epub|type~="titlepage"] p,
section[epub|type~="colophon"] h2,
section[epub|type~="imprint"] h2{
position: absolute;
}
8 changes: 8 additions & 0 deletions se/data/templates/se-kobo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* Kobo compatibility CSS */

section[epub|type~="titlepage"] h1,
section[epub|type~="titlepage"] p,
section[epub|type~="colophon"] h2,
section[epub|type~="imprint"] h2{
font-size: 0; /* Required for Kobo not to add an extra page to the title */
}
2 changes: 0 additions & 2 deletions se/data/templates/se.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ section[epub|type~="titlepage"] h1,
section[epub|type~="titlepage"] p,
section[epub|type~="colophon"] h2,
section[epub|type~="imprint"] h2{
font-size: 0; /* Required for Kobo not to add an extra page to the title */
left: -999em;
position: absolute; /* Required for Kindle */
}

section[epub|type~="titlepage"] img{
Expand Down
14 changes: 11 additions & 3 deletions se/se_epub_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ def build(self, run_epubcheck: bool, check_only: bool, build_kobo: bool, build_k
if not self.metadata_dom.xpath("//dc:identifier[starts-with(., 'url:https://standardebooks.org')]"):
compatibility_css_filename = "compatibility-white-label.css"

with open(work_compatible_epub_dir / "epub" / "css" / "core.css", "a", encoding="utf-8") as core_css_file:
with open(work_compatible_epub_dir / "epub" / "css" / "core.css", "a", encoding="utf-8") as css_file:
with importlib.resources.files("se.data.templates").joinpath(compatibility_css_filename).open("r", encoding="utf-8") as compatibility_css_file:
core_css_file.write("\n" + compatibility_css_file.read())
css_file.write("\n\n" + compatibility_css_file.read())

# Simplify CSS and tags
total_css = ""
Expand Down Expand Up @@ -678,6 +678,10 @@ def build(self, run_epubcheck: bool, check_only: bool, build_kobo: bool, build_k
work_kepub_dir = Path(work_dir / (work_compatible_epub_dir.name + ".kepub"))
shutil.copytree(work_compatible_epub_dir, str(work_kepub_dir), dirs_exist_ok=True)

with open(work_kepub_dir / "epub" / "css" / "se.css", "a", encoding="utf-8") as css_file:
with importlib.resources.files("se.data.templates").joinpath("se-kobo.css").open("r", encoding="utf-8") as compatibility_css_file:
css_file.write("\n\n" + compatibility_css_file.read())

for file_path in work_kepub_dir.glob("**/*"):
# Add a note to the metadata file indicating this is a transform build
if file_path.name == self.metadata_file_path.name:
Expand Down Expand Up @@ -1299,6 +1303,10 @@ def build(self, run_epubcheck: bool, check_only: bool, build_kobo: bool, build_k
return

if build_kindle:
with open(work_compatible_epub_dir / "epub" / "css" / "se.css", "a", encoding="utf-8") as css_file:
with importlib.resources.files("se.data.templates").joinpath("se-kindle.css").open("r", encoding="utf-8") as compatibility_css_file:
css_file.write("\n\n" + compatibility_css_file.read())

# Kindle doesn't go more than 2 levels deep for ToC, so flatten it here.
with open(work_compatible_epub_dir / "epub" / toc_filename, "r+", encoding="utf-8") as file:
dom = se.easy_xml.EasyXmlTree(file.read())
Expand Down Expand Up @@ -1406,7 +1414,7 @@ def build(self, run_epubcheck: bool, check_only: bool, build_kobo: bool, build_k
# Include compatibility CSS
with open(work_compatible_epub_dir / "epub" / "css" / "core.css", "a", encoding="utf-8") as core_css_file:
with importlib.resources.files("se.data.templates").joinpath("kindle.css").open("r", encoding="utf-8") as compatibility_css_file:
core_css_file.write("\n" + compatibility_css_file.read())
core_css_file.write("\n\n" + compatibility_css_file.read())

# Build an epub file we can send to Calibre
se.epub.write_epub(work_compatible_epub_dir, work_dir / compatible_epub_output_filename)
Expand Down

0 comments on commit bf3151c

Please sign in to comment.