Skip to content

Commit b820c8b

Browse files
authored
Merge pull request #12502 from quarto-dev/bugfix/11948
epub - do not use css that amazon doesn't like
2 parents 9b7f5df + 8e8a8da commit b820c8b

File tree

4 files changed

+101
-6
lines changed

4 files changed

+101
-6
lines changed

src/resources/formats/epub/styles.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<style type="text/css">
2-
32
/* Figure formatting */
43
.quarto-layout-panel>figure>figcaption,
54
.quarto-layout-panel>.panel-caption {
@@ -37,10 +36,6 @@
3736
margin: 0.2em;
3837
}
3938

40-
.quarto-layout-cell img {
41-
max-width: 100%;
42-
}
43-
4439
.quarto-layout-cell .html-widget {
4540
width: 100% !important;
4641
}
@@ -110,7 +105,6 @@
110105
}
111106

112107
.quarto-cover-image {
113-
max-width: 35%;
114108
float: right;
115109
margin-left: 30px;
116110
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
format: epub
3+
_quarto:
4+
tests:
5+
epub:
6+
ensureEpubFileRegexMatches:
7+
- path: EPUB/nav.xhtml
8+
regexes:
9+
- []
10+
- ["max-width", "max-height"]
11+
---
12+
13+
## Hello
14+
15+
Amazon epub doesn't allow some CSS attributes, so we remove them from the epub-specific styles we produce.
16+
17+
See https://kdp.amazon.com/en_US/help/topic/GG5R7N649LECKP7U#css_support

tests/smoke/smoke-all.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { breakQuartoMd } from "../../src/core/lib/break-quarto-md.ts";
1616
import { parse } from "../../src/core/yaml.ts";
1717
import { cleanoutput } from "./render/render.ts";
1818
import {
19+
ensureEpubFileRegexMatches,
1920
ensureDocxRegexMatches,
2021
ensureDocxXpath,
2122
ensureFileRegexMatches,
@@ -125,6 +126,7 @@ function resolveTestSpecs(
125126
const result = [];
126127
// deno-lint-ignore no-explicit-any
127128
const verifyMap: Record<string, any> = {
129+
ensureEpubFileRegexMatches,
128130
ensureHtmlElements,
129131
ensureHtmlElementContents,
130132
ensureFileRegexMatches,
@@ -202,6 +204,10 @@ function resolveTestSpecs(
202204
}
203205
} else if (key === "printsMessage") {
204206
verifyFns.push(verifyMap[key](value));
207+
} else if (key === "ensureEpubFileRegexMatches") {
208+
// this ensure function is special because it takes an array of path + regex specifiers,
209+
// so we should never use the spread operator
210+
verifyFns.push(verifyMap[key](outputFile.outputPath, value));
205211
} else if (verifyMap[key]) {
206212
// FIXME: We should find another way that having this requirement of keep-* in the metadata
207213
if (key === "ensureTypstFileRegexMatches") {

tests/verify.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ export const withDocxContent = async <T>(
4444
}
4545
};
4646

47+
export const withEpubDirectory = async <T>(
48+
file: string,
49+
k: (path: string) => Promise<T>
50+
) => {
51+
const [_dir, stem] = dirAndStem(file);
52+
const temp = await Deno.makeTempDir();
53+
try {
54+
// Move the docx to a temp dir and unzip it
55+
const zipFile = join(temp, stem + ".zip");
56+
await Deno.copyFile(file, zipFile);
57+
await unzip(zipFile);
58+
59+
// Open the core xml document and match the matches
60+
const result = await k(temp);
61+
return result;
62+
} finally {
63+
await Deno.remove(temp, { recursive: true });
64+
}
65+
};
66+
4767
export const withPptxContent = async <T>(
4868
file: string,
4969
slideNumber: number,
@@ -569,6 +589,18 @@ export const verifyDocXDocument = (
569589
});
570590
};
571591

592+
export const verifyEpubDocument = (
593+
callback: (path: string) => Promise<void>,
594+
name?: string,
595+
): (file: string) => Verify => {
596+
return (file: string) => ({
597+
name: name ?? "Inspecting Epub",
598+
verify: async (_output: ExecuteOutput[]) => {
599+
return await withEpubDirectory(file, callback);
600+
},
601+
});
602+
}
603+
572604
export const verifyPptxDocument = (
573605
callback: (doc: string, docFile: string) => Promise<void>,
574606
name?: string,
@@ -730,6 +762,52 @@ export const ensureDocxRegexMatches = (
730762
}, "Inspecting Docx for Regex matches")(file);
731763
};
732764

765+
export const ensureEpubFileRegexMatches = (
766+
epubFile: string,
767+
pathsAndRegexes: {
768+
path: string;
769+
regexes: (string | RegExp)[][];
770+
}[]
771+
): Verify => {
772+
return verifyEpubDocument(async (epubDir) => {
773+
for (const { path, regexes } of pathsAndRegexes) {
774+
const file = join(epubDir, path);
775+
assert(
776+
existsSync(file),
777+
`File ${file} doesn't exist in Epub`,
778+
);
779+
const content = await Deno.readTextFile(file);
780+
const mustMatch: (RegExp | string)[] = [];
781+
const mustNotMatch: (RegExp | string)[] = [];
782+
if (regexes.length) {
783+
mustMatch.push(...regexes[0]);
784+
}
785+
if (regexes.length > 1) {
786+
mustNotMatch.push(...regexes[1]);
787+
}
788+
789+
mustMatch.forEach((regex) => {
790+
if (typeof regex === "string") {
791+
regex = new RegExp(regex);
792+
}
793+
assert(
794+
regex.test(content),
795+
`Required match ${String(regex)} is missing from file ${file}.`,
796+
);
797+
});
798+
mustNotMatch.forEach((regex) => {
799+
if (typeof regex === "string") {
800+
regex = new RegExp(regex);
801+
}
802+
assert(
803+
!regex.test(content),
804+
`Illegal match ${String(regex)} was found in file ${file}.`,
805+
);
806+
});
807+
}
808+
}, "Inspecting Epub for Regex matches")(epubFile);
809+
}
810+
733811
// export const ensureDocxRegexMatches = (
734812
// file: string,
735813
// regexes: (string | RegExp)[],

0 commit comments

Comments
 (0)