Skip to content

Commit

Permalink
create-draft: Fix various template issues with --white-label option f…
Browse files Browse the repository at this point in the history
…or better ereader compatibility
  • Loading branch information
acabal committed Oct 12, 2023
1 parent 7457f15 commit 46665b0
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 30 deletions.
3 changes: 2 additions & 1 deletion se/commands/create_draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,14 +767,14 @@ def _create_draft(args: Namespace):
_copy_template_file("container.xml", content_path / "META-INF")
_copy_template_file("mimetype", content_path)
_copy_template_file("onix.xml", content_path / "epub")
_copy_template_file("toc.xhtml", content_path / "epub")
_copy_template_file("core.css", content_path / "epub" / "css")

if args.white_label:
_copy_template_file("content-white-label.opf", content_path / "epub" / "content.opf")
_copy_template_file("titlepage-white-label.xhtml", content_path / "epub" / "text" / "titlepage.xhtml")
_copy_template_file("cover.jpg", content_path / "epub" / "images")
_copy_template_file("local-white-label.css", content_path / "epub" / "css" / "local.css")
_copy_template_file("toc-white-label.xhtml", content_path / "epub" / "toc.xhtml")

else:
_copy_template_file("cover.jpg", repo_path / "images")
Expand All @@ -788,6 +788,7 @@ def _create_draft(args: Namespace):
_copy_template_file("uncopyright.xhtml", content_path / "epub" / "text")
_copy_template_file("titlepage.xhtml", content_path / "epub" / "text")
_copy_template_file("content.opf", content_path / "epub")
_copy_template_file("toc.xhtml", content_path / "epub")
_copy_template_file("LICENSE.md", repo_path)

# Try to find Wikipedia links if possible
Expand Down
52 changes: 52 additions & 0 deletions se/data/templates/compatibility-white-label.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* For legacy reading systems */
article,
aside,
figure,
figcaption,
footer,
header,
main,
nav,
section{
display: block;
}

/* Give PNGs a white background for night mode compatibility... */
img[epub|type~="se:image.color-depth.black-on-transparent"]{
background: #fff !important; /* `!important` is required to make sure readers respect the BG color */
}

/* ...unless the device supports prefers-color-scheme. We’ll invert the image in core.css. RMSDK requires a target media as well as a state. */
@media all and (prefers-color-scheme){
img[epub|type~="se:image.color-depth.black-on-transparent"]:not([epub|type~="se:image.style.realistic"]){
background: transparent !important;
}
}

/* Vertical align any MathML images we might have */
img.mathml{
vertical-align: middle;
}

/* As of 2020-09 hanging punctuation only works on iBooks. However, when it hangs over the page margin, it's
not rendered. So here we simply remove it for iBooks, unless it's on an element that already has a margin
like <blockquote> */
:root[__ibooks_internal_theme] blockquote + p,
:root[__ibooks_internal_theme] h2 + p,
:root[__ibooks_internal_theme] h3 + p,
:root[__ibooks_internal_theme] h4 + p,
:root[__ibooks_internal_theme] h5 + p,
:root[__ibooks_internal_theme] h6 + p,
:root[__ibooks_internal_theme] header + p,
:root[__ibooks_internal_theme] hr + p,
:root[__ibooks_internal_theme] ol + p,
:root[__ibooks_internal_theme] ul + p,
:root[__ibooks_internal_theme] table + p,
:root[__ibooks_internal_theme] hgroup + p,
:root[__ibooks_internal_theme] p:first-child{
hanging-punctuation: none !important;
}

:root[__ibooks_internal_theme] blockquote p:first-child{
hanging-punctuation: first last !important;
}
6 changes: 3 additions & 3 deletions se/data/templates/compatibility.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ section[epub|type~="imprint"] h2{
display: none;
}

/* ...but enable them accessibility in all other ereaders */
/* ...but enable them for accessibility in all other ereaders */
@supports(font-size: 0){
section[epub|type~="titlepage"] h1,
section[epub|type~="titlepage"] p,
Expand All @@ -39,13 +39,13 @@ img[epub|type~="se:image.color-depth.black-on-transparent"]{
background: #fff !important; /* `!important` is required to make sure readers respect the BG color */
}

/* Except our house PNGs, which were rendered from SVGs with a white stroke... */
/* ...except our house PNGs, which were rendered from SVGs with a white stroke... */
[epub|type~="titlepage"] img[epub|type~="se:image.color-depth.black-on-transparent"],
img[epub|type~="se:image.color-depth.black-on-transparent"][epub|type~="z3998:publisher-logo"]{
background: transparent !important;
}

/* ..or if the device supports prefers-color-scheme. We’ll invert the image in core.css. RMSDK requires a target media as well as a state. */
/* ...or if the device supports prefers-color-scheme. We’ll invert the image in core.css. RMSDK requires a target media as well as a state. */
@media all and (prefers-color-scheme){
img[epub|type~="se:image.color-depth.black-on-transparent"]:not([epub|type~="se:image.style.realistic"]){
background: transparent !important;
Expand Down
7 changes: 1 addition & 6 deletions se/data/templates/local-white-label.css
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
@charset "utf-8";
@namespace epub "http://www.idpf.org/2007/ops";

h1{
margin-bottom: 1em;
}

#titlepage p{
font-variant: small-caps;
section[epub|type~="titlepage"] p{
text-align: center;
text-indent: 0;
}
9 changes: 3 additions & 6 deletions se/data/templates/titlepage-white-label.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
<body epub:type="frontmatter">
<section id="titlepage" epub:type="titlepage">
<h1 epub:type="title">TITLE</h1>
<p>By<br/>
AUTHOR</p>
<p>Translated by<br/>
TRANSLATOR</p>
<p>Illustrated by<br/>
ILLUSTRATOR</p>
<p>By AUTHOR.</p>
<p>Translated by TRANSLATOR.</p>
<p>Illustrated by ILLUSTRATOR.</p>
</section>
</body>
</html>
21 changes: 21 additions & 0 deletions se/data/templates/toc-white-label.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" epub:prefix="z3998: http://www.daisy.org/z3998/2012/vocab/structure/, se: https://standardebooks.org/vocab/1.0" xml:lang="en-US">
<head>
<title>Table of Contents</title>
</head>
<body epub:type="frontmatter">
<nav id="toc" epub:type="toc">
<h2 epub:type="title">Table of Contents</h2>
<ol>
<li>
<a href="text/titlepage.xhtml">Titlepage</a>
</li>
</ol>
</nav>
<nav id="landmarks" epub:type="landmarks">
<h2 epub:type="title">Landmarks</h2>
<ol>
</ol>
</nav>
</body>
</html>
12 changes: 0 additions & 12 deletions se/data/templates/toc.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,9 @@
<nav id="landmarks" epub:type="landmarks">
<h2 epub:type="title">Landmarks</h2>
<ol>
<li>
<a href="text/titlepage.xhtml" epub:type="frontmatter titlepage">Titlepage</a>
</li>
<li>
<a href="text/imprint.xhtml" epub:type="frontmatter imprint">Imprint</a>
</li>
<li>
<a href="text/chapter-1.xhtml" epub:type="bodymatter z3998:fiction">WORK_TITLE</a>
</li>
<li>
<a href="text/colophon.xhtml" epub:type="backmatter colophon">Colophon</a>
</li>
<li>
<a href="text/uncopyright.xhtml" epub:type="backmatter copyright-page">Uncopyright</a>
</li>
</ol>
</nav>
</body>
Expand Down
8 changes: 6 additions & 2 deletions se/se_epub_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,13 @@ def build(self, run_epubcheck: bool, check_only: bool, build_kobo: bool, build_k
# Now add compatibility fixes for older ereaders.

# Include compatibility CSS
compatibility_css_filename = "compatibility.css"
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 importlib_resources.open_text("se.data.templates", "compatibility.css", encoding="utf-8") as compatibility_css_file:
core_css_file.write(compatibility_css_file.read())
with importlib_resources.open_text("se.data.templates", compatibility_css_filename, encoding="utf-8") as compatibility_css_file:
core_css_file.write("\n" + compatibility_css_file.read())

# Simplify CSS and tags
total_css = ""
Expand Down

0 comments on commit 46665b0

Please sign in to comment.