Commit 8db4547e authored by Fred Chasen's avatar Fred Chasen

Add hyphenation

parent 04ee08f3
Pipeline #605 skipped
......@@ -4,17 +4,10 @@ Render Html to PDFs using [Vivliostyle.js](https://github.com/vivliostyle/vivlio
## Installation
### From NPM
```
npm install -g https://gitlab.coko.foundation/fchasen/vivliostyle-electron.git
```
### For development
```
npm install
npm link
```
## Generating a PDF
```
......@@ -24,28 +17,56 @@ vivliostyle-electron ./path/to/index.html -o result.pdf
## Options
```
-h, --help output usage information
-V, --version output the version number
-o, --output [output] Output
-d, --debug Debug
-l, --landscape Landscape printing
-s, --page-size [size] Print to Page Size [size]
-w, --width [size] Print to Page Width [width]
-h --height [size] Print to Page Height [weight]
-m, --page-margin [margin] Print with margin [margin]
-h, --help output usage information
-V, --version output the version number
-i, --inputs [inputs] Inputs
-o, --output [output] Output
-d, --debug Debug
-l, --landscape Landscape printing
-s, --page-size [size] Print to Page Size [size]
-w, --width [size] Print to Page Width [width]
-h --height [size] Print to Page Height [weight]
-m, --page-margin [margin] Print with margin [margin]
-n, --hyphenate [lang] Hyphenate with language [language] such as "en-us"
```
## Hyphenation
HTML can be pre-processed with soft hyphens by the [Hypher](https://github.com/bramstein/hypher) library.
Pass the abbreviation a language code (such as `en-us` or `de`) when calling the renderer. You can install languages beyond those included the package.json using npm.
```
vivliostyle-electron ./path/to/index.html --hyphenate hyphenation.en-us
```
## Running on Linux
Install Xvfb:
Install Xvfb
```
sudo apt-get install xvfb
```
Run Xvfb:
Run Xvfb
```
export DISPLAY=':99.0'
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
```
More details here: https://github.com/bjrmatos/electron-html-to/issues/87
## Development
Link and build the JS
```
npm install
npm link
npm -g gulp
gulp watch
```
Then you can run
```
DEBUG=* vivliostyle-electron ./path/to/index.html
```
#!/usr/bin/env node
var Renderer = require("./lib/index.js");
var Hyphenate = require("./lib/hyphenate.js");
var program = require('commander');
var path = require('path');
var fs = require('fs');
program
.version('0.0.1')
.arguments('[inputPath]')
// .option('-i, --inputs [inputs]', 'Inputs')
.option('-i, --inputs [inputs]', 'Inputs')
.option('-o, --output [output]', 'Output')
.option('-d, --debug', 'Debug')
.option('-l, --landscape', 'Landscape printing', false)
......@@ -15,16 +17,39 @@ program
.option('-w, --width [size]', 'Print to Page Width [width]')
.option('-h --height [size]', 'Print to Page Height [weight]')
.option('-m, --page-margin [margin]', 'Print with margin [margin]')
.option('-n, --hyphenate [lang]', 'Hyphenate with language [language] such as "en-us"')
.parse(process.argv);
var input = program.inputs || program.args[0];
var relativePath = path.relative(__dirname, input);
var output = program.output || './' + path.basename(input).replace('.xhtml', '.pdf').replace('.html', '.pdf');
var renderer = new Renderer(input, output, {
var render;
var hyphenate;
var tmpPath;
if (program.hyphenate) {
tmpPath = relativePath.replace(".xhtml", ".hyphenated.xhtml").replace(".html", ".hyphenated.html");
hyphenate = new Hyphenate(program.hyphenate);
hyphenate.process(relativePath, tmpPath);
}
renderer = new Renderer(tmpPath || input, output, {
pageSize: program.pageSize,
margin: program.pageMargin,
landscape: program.landscape,
width: program.width,
height: program.height,
debug: program.debug
});
}).then(function (result) {
if (tmpPath) {
fs.unlinkSync(tmpPath);
}
}).catch(function (err) {
console.error(err);
})
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _cheerio = require('cheerio');
var _cheerio2 = _interopRequireDefault(_cheerio);
var _hypher = require('hypher');
var _hypher2 = _interopRequireDefault(_hypher);
var _eachTextNode = require('gulp-hypher/src/eachTextNode.js');
var _eachTextNode2 = _interopRequireDefault(_eachTextNode);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function Hyphenate(lang) {
var pattern = require("hyphenation." + lang);
this.h = new _hypher2.default(pattern);
}
Hyphenate.prototype.hyphenateText = function (text) {
var _this = this;
// split the text in html entity and not entity
return text.split(/(!?&[a-zA-Z]*;)/).map(function (textPart) {
// immediately return html entities and hyphenate everything else
return textPart.match(/&[a-zA-Z]*;/) ? textPart : _this.h.hyphenateText(textPart);
}).join('');
};
Hyphenate.prototype.process = function (input, output, encoding) {
var contents = _fs2.default.readFileSync(input, encoding || 'utf8');
var hyphenated = (0, _eachTextNode2.default)(contents, this.hyphenateText.bind(this));
if (output) {
_fs2.default.writeFileSync(output, hyphenated, encoding || 'utf8');
}
return hyphenated;
};
exports.default = Hyphenate;
module.exports = exports['default'];
\ No newline at end of file
......@@ -12,19 +12,24 @@ var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var conversion = (0, _electronHtmlTo2.default)({
converterPath: _electronHtmlTo2.default.converters.PDF,
allowLocalFilesAccess: true,
timeout: 60000 * 5, // Allow five minutes to process
strategy: 'electron-ipc' // 'electron-ipc | electron-server | dedicated-process'
strategy: 'dedicated-process' // 'electron-ipc | electron-server | dedicated-process'
});
function Renderer(inputPath, outputPath, _options, cb) {
function Renderer(inputPath, outputPath, _options) {
var directoryUrl = 'file://' + __dirname.replace('lib', '');
var baseUrl = directoryUrl + 'renderer/renderer.html?url=';
var directoryPath = __dirname.replace('lib', '');
var basePath = directoryPath + 'renderer/';
var relativePath = _path2.default.relative(basePath, inputPath);
var options = _options || {};
var settings = {
......@@ -36,7 +41,7 @@ function Renderer(inputPath, outputPath, _options, cb) {
debug: options.debug || false
};
var url = baseUrl + encodeURIComponent(inputPath);
var url = 'file://' + basePath + "renderer.html?url=" + encodeURIComponent(relativePath);
var size = settings.pageSize;
......@@ -61,31 +66,36 @@ function Renderer(inputPath, outputPath, _options, cb) {
}
console.log("Processing", inputPath, url);
return new Promise(function (resolve, reject) {
conversion({
url: url,
waitForJS: true,
pdf: {
marginsType: settings.pageMargin,
pageSize: {
width: 9 * 25400,
height: 2 * 25400
},
printBackground: true,
landscape: settings.landscape
}
}, function (err, result) {
if (err) {
conversion.kill();
return reject(err);
}
console.log("Printed", result.numberOfPages, 'pages to', outputPath);
if (outputPath) {
result.stream.pipe(_fs2.default.createWriteStream(outputPath));
}
conversion({
url: url,
waitForJS: true,
pdf: {
marginsType: settings.pageMargin,
pageSize: {
width: 9 * 25400,
height: 2 * 25400
},
printBackground: true,
landscape: settings.landscape
}
}, function (err, result) {
if (err) {
conversion.kill();
return console.error(err);
}
console.log("Printed", result.numberOfPages, 'pages to', outputPath);
result.stream.pipe(_fs2.default.createWriteStream(outputPath));
conversion.kill();
cb && cb();
resolve(result);
});
});
}
......
......@@ -2,6 +2,10 @@
"name": "vivliostyle-electron",
"version": "0.0.1",
"description": "Render Html to PDFs using Vivliostyle.js and Electron",
"repository": {
"type": "git",
"url": "git@gitlab.coko.foundation:fchasen/vivliostyle-electron.git"
},
"main": "lib/index.js",
"scripts": {
"clean": "rimraf lib",
......@@ -17,11 +21,52 @@
"license": "MIT",
"dependencies": {
"babel": "^6.5.2",
"cheerio": "^0.22.0",
"commander": "^2.9.0",
"debug": "^2.2.0",
"electron-html-to": "^2.1.1",
"electron-prebuilt": "^1.3.5",
"eslint": "^3.4.0",
"gulp-hypher": "^1.0.3",
"hyphenation.en-us": "^0.2.1",
"hyphenation.be": "^0.2.1",
"hyphenation.bn": "^0.2.1",
"hyphenation.ca": "^0.2.1",
"hyphenation.cs": "^0.2.1",
"hyphenation.da": "^0.2.1",
"hyphenation.de": "^0.2.1",
"hyphenation.en-us": "^0.2.1",
"hyphenation.fi": "^0.2.1",
"hyphenation.fr": "^0.2.1",
"hyphenation.grc": "^0.2.1",
"hyphenation.gu": "^0.2.1",
"hyphenation.hi": "^0.2.1",
"hyphenation.hu": "^0.2.1",
"hyphenation.hy": "^0.2.1",
"hyphenation.it": "^0.2.1",
"hyphenation.kn": "^0.2.1",
"hyphenation.la": "^0.2.1",
"hyphenation.lt": "^0.2.1",
"hyphenation.lv": "^0.2.1",
"hyphenation.ml": "^0.2.1",
"hyphenation.nl": "^0.2.1",
"hyphenation.or": "^0.2.1",
"hyphenation.pa": "^0.2.1",
"hyphenation.pl": "^0.2.1",
"hyphenation.pt": "^0.2.1",
"hyphenation.ru": "^0.2.1",
"hyphenation.sk": "^0.2.1",
"hyphenation.sl": "^0.2.1",
"hyphenation.sv": "^0.2.1",
"hyphenation.ta": "^0.2.1",
"hyphenation.te": "^0.2.1",
"hyphenation.tr": "^0.2.1",
"hyphenation.uk": "^0.2.1",
"hyphenation.nb-no": "^0.2.1",
"hyphenation.en-gb": "^0.2.1",
"hyphenation.el-monoton": "^0.2.1",
"hyphenation.el-polyton": "^0.2.1",
"hypher": "^0.2.4",
"mocha": "^3.0.2",
"vivliostyle": "^2016.7.0"
},
......
import cheerio from 'cheerio';
import hypher from 'hypher';
import eachTextNode from 'gulp-hypher/src/eachTextNode.js';
import fs from 'fs';
function Hyphenate(lang) {
var pattern = require("hyphenation."+lang);
this.h = new hypher(pattern);
}
Hyphenate.prototype.hyphenateText = function (text) {
// split the text in html entity and not entity
return text.split(/(!?&[a-zA-Z]*;)/).map((textPart) => {
// immediately return html entities and hyphenate everything else
return textPart.match(/&[a-zA-Z]*;/)
? textPart
: this.h.hyphenateText(textPart);
}).join('');
};
Hyphenate.prototype.process = function (input, output, encoding) {
var contents = fs.readFileSync(input, encoding || 'utf8');
var hyphenated = eachTextNode(contents, this.hyphenateText.bind(this));
if (output) {
fs.writeFileSync(output, hyphenated, encoding || 'utf8');
}
return hyphenated;
};
export default Hyphenate;
import convertFactory from 'electron-html-to';
import fs from 'fs';
import path from 'path';
var conversion = convertFactory({
converterPath: convertFactory.converters.PDF,
allowLocalFilesAccess: true,
timeout: 60000 * 5, // Allow five minutes to process
strategy: 'electron-ipc' // 'electron-ipc | electron-server | dedicated-process'
strategy: 'dedicated-process' // 'electron-ipc | electron-server | dedicated-process'
});
function Renderer(inputPath, outputPath, _options, cb) {
function Renderer(inputPath, outputPath, _options) {
var directoryUrl = 'file://' + __dirname.replace('lib', '');
var baseUrl = directoryUrl + 'renderer/renderer.html?url=';
var directoryPath = __dirname.replace('lib', '');
var basePath = directoryPath + 'renderer/';
var relativePath = path.relative(basePath, inputPath);
var options = _options || {};
var settings = {
......@@ -24,7 +26,7 @@ function Renderer(inputPath, outputPath, _options, cb) {
debug: options.debug || false
};
var url = baseUrl + encodeURIComponent(inputPath);
var url = 'file://' + basePath + "renderer.html?url=" + encodeURIComponent(relativePath);
var size = settings.pageSize;
......@@ -49,34 +51,41 @@ function Renderer(inputPath, outputPath, _options, cb) {
}
console.log("Processing", inputPath, url);
return new Promise(function(resolve, reject) {
conversion({
url: url,
waitForJS: true,
pdf: {
marginsType: settings.pageMargin,
pageSize: {
width: 9 * 25400,
height: 2 * 25400
},
printBackground: true,
landscape: settings.landscape
}
}, function(err, result) {
if (err) {
conversion.kill();
return reject(err);
}
console.log("Printed", result.numberOfPages, 'pages to', outputPath);
conversion({
url: url,
waitForJS: true,
pdf: {
marginsType: settings.pageMargin,
pageSize: {
width: 9 * 25400,
height: 2 * 25400
},
printBackground: true,
landscape: settings.landscape
if (outputPath) {
result.stream.pipe(fs.createWriteStream(outputPath));
}
}, function(err, result) {
if (err) {
conversion.kill();
return console.error(err);
}
console.log("Printed", result.numberOfPages, 'pages to', outputPath);
conversion.kill();
result.stream.pipe(fs.createWriteStream(outputPath));
conversion.kill();
resolve(result);
cb && cb();
});
});
}
export default Renderer;
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