Inline css for footer/header templates

This commit is contained in:
Samy Pesse
2015-10-03 20:24:56 +02:00
committed by Samy Pessé
parent eef82c18c2
commit 33593c13ae
8 changed files with 126 additions and 88 deletions
+2 -17
View File
@@ -277,7 +277,6 @@ Configuration.DEFAULT = {
// Margin (in pts)
// Note: 72 pts equals 1 inch
<<<<<<< HEAD
'margin': {
'right': 62,
'left': 62,
@@ -286,24 +285,10 @@ Configuration.DEFAULT = {
},
// Header HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_.
'headerTemplate': '',
'headerTemplate': null,
// Footer HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_.
'footerTemplate': ''
=======
"margin": {
"right": 62,
"left": 62,
"top": 62,
"bottom": 62
},
//Header HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_.
"headerTemplate": fs.readFileSync(path.resolve(__dirname, '../theme/templates/ebook/header.html'), { encoding: 'utf-8' }),
//Footer HTML template. Available variables: _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_.
"footerTemplate": fs.readFileSync(path.resolve(__dirname, '../theme/templates/ebook/footer.html'), { encoding: 'utf-8' })
>>>>>>> Add css to footer/header for pdf
'footerTemplate': null
}
};
+34 -2
View File
@@ -2,6 +2,7 @@ var util = require("util");
var path = require("path");
var Q = require("q");
var _ = require("lodash");
var juice = require("juice");
var exec = require("child_process").exec;
var fs = require("../utils/fs");
@@ -41,6 +42,37 @@ Generator.prototype.writeSummary = function() {
return this._writeTemplate(this.templates.summary, {}, path.join(this.options.output, "SUMMARY.html"));
};
// Return template for footer/header with inlined css
Generator.prototype.getPDFTemplate = function(id) {
var tpl = this.options.pdf[id+'Template'];
var defaultTpl = path.resolve(this.options.theme, 'templates/ebook/'+id+'.html');
var defaultCSS = path.resolve(this.options.theme, 'assets/print.css');
// Default template from theme
if (!tpl && fs.existsSync(defaultTpl)) {
tpl = fs.readFileSync(defaultTpl, { encoding: 'utf-8' });
}
// Inline CSS using juice
var stylesheets = [];
// From theme
if (fs.existsSync(defaultCSS)) {
stylesheets.push(fs.readFileSync(defaultCSS, { encoding: 'utf-8' }));
}
// Custom PDF style
if (this.styles.pdf) {
stylesheets.push(fs.readFileSync(this.book.resolve(this.styles.pdf), { encoding: 'utf-8' }));
}
tpl = juice(tpl, {
extraCss: stylesheets.concat('/n')
});
return tpl;
};
Generator.prototype.finish = function() {
var that = this;
@@ -90,8 +122,8 @@ Generator.prototype.finish = function() {
"--pdf-mono-font-size": String(pdfOptions.fontSize),
"--paper-size": String(pdfOptions.paperSize),
"--pdf-page-numbers": Boolean(pdfOptions.pageNumbers),
"--pdf-header-template": String(pdfOptions.headerTemplate) || "<p class='header'><span>"+that.options.title+"</span></p>",
"--pdf-footer-template": String(pdfOptions.footerTemplate) || "<p class='footer'><span>_SECTION_</span> <span style='float:right;'>_PAGENUM_</span></p>"
"--pdf-header-template": that.getPDFTemplate('header'),
"--pdf-footer-template": that.getPDFTemplate('footer')
});
} else if (that.ebookFormat == "epub") {
_.extend(_options, {
+2 -1
View File
@@ -35,7 +35,8 @@
"npm": "2.4.1",
"dom-serializer": "0.1.0",
"spawn-cmd": "0.0.2",
"escape-string-regexp": "1.0.3"
"escape-string-regexp": "1.0.3",
"juice": "1.5.0"
},
"devDependencies": {
"eslint": "1.5.0",
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+71 -51
View File
@@ -3,67 +3,87 @@
@import "ebook/variables.less";
body {
.page {
font-family: sans-serif;
.markdown-content(#000, 1.6);
font-family: sans-serif;
}
pre, code, blockquote, tr, img {
page-break-inside: avoid;
.pdf-footer {
margin-top: 20px;
padding-top: 10px;
border-top: 1px solid #eee;
color:#aaa;
.footer-pages-count {
float:right;
}
}
.pdf-header {
margin-top: 20px;
padding-bottom: 10px;
border-bottom: 1px solid #eee;
color: #aaa;
}
.page {
.markdown-content(#000, 1.6);
pre, code, blockquote, tr, img {
page-break-inside: avoid;
}
p,
h2,
h3 {
orphans: 3;
widows: 3;
}
h1,
h2,
h3 {
page-break-after: avoid;
}
// Section and first titles
.section {
> h1, > h2, > h3 {
margin-top: 0px;
}
}
p,
h2,
h3 {
orphans: 3;
widows: 3;
}
// Hide the title (only used to build PDF table of content)
.book-chapter {
display: none;
}
// First page: table of contents
&.page-toc {
h1,
h2,
h3 {
page-break-after: avoid;
}
}
// Section and first titles
.section {
> h1, > h2, > h3 {
margin-top: 0px;
}
}
// Glossary page
&.page-glossary {
.glossary {
margin-bottom: 40px;
// Hide the title (only used to build PDF table of content)
.book-chapter {
display: none;
}
// First page: table of contents
&.page-toc {
}
// Glossary page
&.page-glossary {
.glossary {
margin-bottom: 40px;
h2 {
a, a:hover {
color: inherit;
text-decoration: none;
}
h2 {
a, a:hover {
color: inherit;
text-decoration: none;
}
}
.glossary-index {
list-style: none;
margin: 0px;
padding: 0px;
.glossary-index {
list-style: none;
margin: 0px;
padding: 0px;
li {
display: inline;
margin: 0px 8px;
white-space: nowrap;
}
li {
display: inline;
margin: 0px 8px;
white-space: nowrap;
}
}
}
+8 -4
View File
@@ -1,4 +1,8 @@
<div style="font-family: sans-serif;margin-top: 20px;padding-top: 10px;border-top: 1px solid #eee;color:#aaa;">
<span>_SECTION_</span>
<span style="float:right;">_PAGENUM_</span>
</div>
<html>
<body>
<div class="pdf-footer">
<span>_SECTION_</span>
<span class="footer-pages-count">_PAGENUM_</span>
</div>
</body>
</html>
+7 -3
View File
@@ -1,3 +1,7 @@
<div style="font-family: sans-serif;margin-top: 20px;padding-bottom: 10px;border-bottom: 1px solid #eee;color: #aaa;">
<span>_TITLE_</span>
</div>
<html>
<body>
<div class="pdf-header">
<span>_TITLE_</span>
</div>
</body>
</html>