Commit a51e3e0d authored by Fred Chasen's avatar Fred Chasen

Updated hyphenation to export xhtml, update vivliostyle version

parent f2c431b9
Pipeline #720 skipped
......@@ -67,9 +67,17 @@ renderer = new Renderer(tmpPath || input, output, {
debug: program.debug,
timeout: program.timeout
}).then(function (result) {
if (tmpPath && tmpPath != relativePath) {
if (!program.debug && tmpPath && tmpPath != relativePath) {
fs.unlinkSync(tmpPath);
}
}).catch(function (err) {
console.error(err);
})
// if (program.display) {
// var childProcess = require("child_process");
// var electron = require('electron-prebuilt');
// var cwd = (process && process.cwd()) || __dirname;
//
// child = childProcess.spawn(electron, ["--path", "visual.js", "--url", ( tmpPath || relativePath)], { _showOutput: true });
// }
......@@ -12,16 +12,14 @@ 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 }; }
var IGNORE = 'head, code, pre, script, style, [class^="pull-"], [class^="push-"], .small-caps';
function Hyphenate(lang) {
var pattern = require("hyphenation." + lang);
this.h = new _hypher2.default(pattern);
......@@ -37,9 +35,9 @@ Hyphenate.prototype.hyphenateText = function (text) {
}).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));
Hyphenate.prototype.process = function (input, output, encoding, options) {
var contents = _fs2.default.readFileSync(input, options && options.encoding || 'utf8');
var hyphenated = this.eachTextNode(contents, this.hyphenateText.bind(this), options);
if (output) {
_fs2.default.writeFileSync(output, hyphenated, encoding || 'utf8');
......@@ -48,5 +46,45 @@ Hyphenate.prototype.process = function (input, output, encoding) {
return hyphenated;
};
Hyphenate.prototype.eachTextNode = function (html, doThis, options) {
var ignore = IGNORE;
var only = options && options.only || ':root';
if (options && options.ignore) ignore += ', ' + options.ignore;
var $ = _cheerio2.default.load(html, {
decodeEntities: false,
xmlMode: true
});
var processedText = $(only).each(function () {
findTextNodes(this);
});
function findTextNodes(node) {
if ($(node).is(ignore)) return false;
$(node).contents().each(function () {
var childNode = $(this)[0];
// We've made it to a text node!
// apply the function which transforms
// its text content (childNode.data)
if (childNode.type === 'text' || !childNode.type) {
childNode.data = doThis(childNode.data);
} else {
findTextNodes(childNode, doThis);
}
});
}
return $.html({
decodeEntities: false,
xmlMode: true
});
};
exports.default = Hyphenate;
module.exports = exports['default'];
\ No newline at end of file
......@@ -18,15 +18,15 @@ 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: 'dedicated-process' // 'electron-ipc | electron-server | dedicated-process'
});
function Renderer(inputPath, outputPath, _options) {
var conversion = (0, _electronHtmlTo2.default)({
converterPath: _electronHtmlTo2.default.converters.PDF,
allowLocalFilesAccess: true,
timeout: 60000 * 5, // Allow five minutes to process
strategy: 'dedicated-process' // 'electron-ipc | electron-server | dedicated-process'
});
var directoryPath = __dirname.replace('lib', '');
var basePath = directoryPath + 'renderer/';
var relativePath = _path2.default.relative(basePath, inputPath);
......
......@@ -16,6 +16,18 @@
<link rel="stylesheet" href="css/vivliostyle-viewer.css"/>
<style id="vivliostyle-page-rules"></style>
<style>
/* Uncomment for visual check of renderered pages */
/*
#vivliostyle-viewer-viewport [data-vivliostyle-outer-zoom-box],
#vivliostyle-viewer-viewport [data-vivliostyle-spread-container] {
width: auto !important;
}
#vivliostyle-viewer-viewport [data-vivliostyle-page-container] {
display: block !important;
}*/
</style>
</head>
<body data-vivliostyle-viewer-status="loading">
<div id="vivliostyle-viewer-viewport"></div>
......
import cheerio from 'cheerio';
import hypher from 'hypher';
import eachTextNode from 'gulp-hypher/src/eachTextNode.js';
import fs from 'fs';
const IGNORE = 'head, code, pre, script, style, [class^="pull-"], [class^="push-"], .small-caps';
function Hyphenate(lang) {
var pattern = require("hyphenation."+lang);
this.h = new hypher(pattern);
......@@ -19,8 +20,8 @@ Hyphenate.prototype.hyphenateText = function (text) {
};
Hyphenate.prototype.process = function (input, output, encoding, options) {
var contents = fs.readFileSync(input, options.encoding || 'utf8');
var hyphenated = eachTextNode(contents, this.hyphenateText.bind(this), options);
var contents = fs.readFileSync(input, (options && options.encoding) || 'utf8');
var hyphenated = this.eachTextNode(contents, this.hyphenateText.bind(this), options);
if (output) {
fs.writeFileSync(output, hyphenated, encoding || 'utf8');
......@@ -29,4 +30,43 @@ Hyphenate.prototype.process = function (input, output, encoding, options) {
return hyphenated;
};
Hyphenate.prototype.eachTextNode = function(html, doThis, options) {
var ignore = IGNORE;
var only = (options && options.only) || ':root';
if (options && options.ignore) ignore += ', ' + options.ignore;
var $ = cheerio.load(html, {
decodeEntities: false,
xmlMode: true
});
var processedText = $(only).each(function(){findTextNodes(this);});
function findTextNodes(node) {
if ($(node).is(ignore)) return false;
$(node).contents().each(function(){
var childNode = $(this)[0];
// We've made it to a text node!
// apply the function which transforms
// its text content (childNode.data)
if (childNode.type === 'text' || !childNode.type) {
childNode.data = doThis(childNode.data);
} else {
findTextNodes(childNode, doThis);
}
});
}
return $.html({
decodeEntities: false,
xmlMode: true
});
};
export default Hyphenate;
......@@ -2,16 +2,15 @@ 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: 'dedicated-process' // 'electron-ipc | electron-server | dedicated-process'
});
function Renderer(inputPath, outputPath, _options) {
var conversion = convertFactory({
converterPath: convertFactory.converters.PDF,
allowLocalFilesAccess: true,
timeout: 60000 * 5, // Allow five minutes to process
strategy: 'dedicated-process' // 'electron-ipc | electron-server | dedicated-process'
});
var directoryPath = __dirname.replace('lib', '');
var basePath = directoryPath + 'renderer/';
var relativePath = path.relative(basePath, inputPath);
......
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