Commit 5a35f073 authored by Fred Chasen's avatar Fred Chasen
Browse files

Apply print styles

parent 246d0ab5
......@@ -120,7 +120,7 @@ To test the pdf output of specs, you'll need to install ghostscript locally.
```bash
brew install ghostscript
npm install ghostscript4js
npm install ghostscript4js --no-save
```
Then run the jest tests in puppeteer.
......
<!DOCTYPE html PUBLIC>
<html lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>
The Project Gutenberg eBook of Auroræ: Their Characters and Spectra, by J. Rand Capron.
</title>
<link rel="coverpage" href="images/cover.jpg" />
<script src="../../../dist/paged.polyfill.js"></script>
<style>
@page {
size: 'letter';
margin: 1.2in;
}
@media screen {
.page {
box-shadow: 0 0 0 1px rgba(0, 0,0,0.2);
}
}
section {
color: red;
}
@media print {
section {
color: green;
}
}
</style>
</head>
<body>
<section class="body-rw Chapter-rw" epub:type="bodymatter chapter">
<header>
<h1><span class="audio" id="c001s0000">Chapter 1. Loomings.</span></h1></header>
<p><span class="audio" id="c001s0001">Call me Ishmael.</span> <span class="audio" id="c001s0002">Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.</span> <span class="audio" id="c001s0003">It is a way I have of driving off the spleen and regulating the circulation.</span> <span class="audio" id="c001s0004">Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people’s hats off—then, I account it high time to get to sea as soon as I can.</span> <span class="audio" id="c001s0005">This is my substitute for pistol and ball.</span> <span class="audio" id="c001s0006">With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship.</span> <span class="audio" id="c001s0007">There is nothing surprising in this.</span> <span class="audio" id="c001s0008">If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.</span></p>
<p><span class="audio" id="c001p0002">There now is your insular city of the Manhattoes, belted round by wharves as Indian isles by coral reefs—commerce surrounds it with her surf. Right and left, the streets take you waterward. Its extreme downtown is the battery, where that noble mole is washed by waves, and cooled by breezes, which a few hours previous were out of sight of land. Look at the crowds of water-gazers there.</span></p>
<p><span class="audio" id="c001p0003">Circumambulate the city of a dreamy Sabbath afternoon. Go from Corlears Hook to Coenties Slip, and from thence, by Whitehall, northward. What do you see?—Posted like silent sentinels all around the town, stand thousands upon thousands of mortal men fixed in ocean reveries. Some leaning against the spiles; some seated upon the pier-heads; some looking over the bulwarks of ships from China; some high aloft in the rigging, as if striving to get a still better seaward peep. But these are all landsmen; of week days pent up in lath and plaster—tied to counters, nailed to benches, clinched to desks. How then is this? Are the green fields gone? What do they here?</span></p>
<p><span class="audio" id="c001p0004">But look! here come more crowds, pacing straight for the water, and seemingly bound for a dive. Strange! Nothing will content them but the extremest limit of the land; loitering under the shady lee of yonder warehouses will not suffice. No. They must get just as nigh the water as they possibly can without falling in. And there they stand—miles of them—leagues. Inlanders all, they come from lanes and alleys, streets and avenues—north, east, south, and west. Yet here they all unite. Tell me, does the magnetic virtue of the needles of the compasses of all those ships attract them thither?</span></p>
</section>
</body>
</html>
const TIMEOUT = 10000; // Some book might take longer than this to renderer
describe('default', async () => {
let page;
let rendered;
beforeAll(async () => {
page = await loadPage('media/print/print.html')
return page.rendered;
}, TIMEOUT)
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
})
it('should render green text', async () => {
let textColor = await page.$eval("h1", (h1) => window.getComputedStyle(h1).color);
expect(textColor).toContain('rgb(0, 128, 0)'); // green
})
if (!DEBUG) {
it('should create a pdf', async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(1);
})
}
}
)
import AtPage from './atpage';
import Breaks from './breaks';
import PrintMedia from './print-media'
export default [
AtPage,
Breaks
Breaks,
PrintMedia
]
import Handler from "../handler";
import csstree from 'css-tree';
class PrintMedia extends Handler {
constructor(chunker, polisher, caller) {
super(chunker, polisher, caller);
}
onAtMedia(node, item, list) {
let media = this.getMediaName(node);
let rules;
if (media === "print") {
rules = node.block.children;
// Remove rules from the @media block
node.block.children = new csstree.List();
// Append rules to the end of main rules list
list.appendList(rules);
}
}
getMediaName(node) {
let media = '';
if (typeof node.prelude === "undefined" ||
node.prelude.type !== "AtrulePrelude" ) {
return;
}
csstree.walk(node.prelude, {
visit: 'Identifier',
enter: (identNode, iItem, iList) => {
media = identNode.name;
}
});
return media;
}
}
export default PrintMedia;
......@@ -11,6 +11,7 @@ class Polisher {
this.hooks = {};
this.hooks.onUrl = new Hook(this);
this.hooks.onAtPage = new Hook(this);
this.hooks.onAtMedia = new Hook(this);
this.hooks.onRule = new Hook(this);
this.hooks.onDeclaration = new Hook(this);
this.hooks.onContent = new Hook(this);
......
......@@ -11,6 +11,7 @@ class Sheet {
this.hooks = {};
this.hooks.onUrl = new Hook(this);
this.hooks.onAtPage = new Hook(this);
this.hooks.onAtMedia = new Hook(this);
this.hooks.onRule = new Hook(this);
this.hooks.onDeclaration = new Hook(this);
this.hooks.onContent = new Hook(this);
......@@ -45,7 +46,7 @@ class Sheet {
// Trigger Hooks
this.urls(this.ast);
this.rules(this.ast);
this.pages(this.ast);
this.atrules(this.ast);
this.hooks.afterTreeWalk.trigger(this.ast, this);
}
......@@ -74,7 +75,7 @@ class Sheet {
});
}
pages(ast) {
atrules(ast) {
csstree.walk(ast, {
visit: 'Atrule',
enter: (node, item, list) => {
......@@ -84,10 +85,16 @@ class Sheet {
this.hooks.onAtPage.trigger(node, item, list);
this.declarations(node, item, list);
}
if (basename === "media") {
this.hooks.onAtMedia.trigger(node, item, list);
this.declarations(node, item, list);
}
}
});
}
rules(ast) {
let parsed = {};
csstree.walk(ast, {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment