Commit 09c537d1 authored by Fred Chasen's avatar Fred Chasen

Change location of hypenated file

parent 4725df86
......@@ -67,6 +67,7 @@ if (typeof(program.output) === "string") {
output = './' + replaceExt(path.basename(input), '.pdf');
}
if (program.hyphenate) {
hyphenateOptions = {
ignore: program.hypher_ignore || undefined,
......@@ -74,9 +75,10 @@ if (program.hyphenate) {
encoding: program.encoding || undefined
}
// tmpPath = replaceExt(relativePath, ".hyphenated.html");
tmpFile = temp.openSync({suffix: '.html'});
tmpPath = tmpFile.path;
tmpPath = replaceExt(relativePath, ".hyphenated.html");
// tmpFile = temp.openSync({suffix: '.html'});
// tmpPath = tmpFile.path;
// Create a new Hyphenator, with passed language
hyphenator = new Hyphenator(program.hyphenate);
hyphenator.process(relativePath, tmpPath, hyphenateOptions);
......@@ -98,12 +100,16 @@ renderer.process(tmpPath || input, output)
}
if (output) {
msg += " to " + ouput;
msg += " to " + output;
}
console.log(msg);
if (tmpPath && !program.debug) {
fs.unlinkSync(tmpPath);
}
}, function (err) {
console.error(err);
// temp.cleanupSync();
}).catch(function (err) {
console.error(err);
});
......@@ -16,6 +16,10 @@ 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 IGNORE = 'head, code, pre, script, style, [class^="pull-"], [class^="push-"], .small-caps';
......@@ -44,7 +48,12 @@ Hyphenator.prototype.hyphenateText = function (text) {
Hyphenator.prototype.process = function (input, output, encoding, options) {
var contents = _fs2.default.readFileSync(input, options && options.encoding || 'utf8');
var hyphenated = this.walk(contents, this.hyphenateText.bind(this), options);
var $doc = this.toDocument(contents);
var hyphenated;
// this.baseTag($doc, input, output);
hyphenated = this.walk($doc, this.hyphenateText.bind(this), options);
if (output) {
this.output(contents, output, encoding);
......@@ -77,19 +86,45 @@ Hyphenator.prototype.findTextNodes = function ($, node, doThis, ignore) {
});
};
Hyphenator.prototype.walk = function (html, doThis, options) {
var ignore = IGNORE;
var only = options && options.only || ':root';
var hyphenator = this;
if (options && options.ignore) ignore += ', ' + options.ignore;
Hyphenator.prototype.toDocument = function (html) {
var $ = _cheerio2.default.load(html, {
decodeEntities: false,
xmlMode: true
});
var processedText = $(only).each(function () {
return $;
};
Hyphenator.prototype.baseTag = function ($, inputPath, outputPath) {
var $base = $('base');
var dir, resolved;
if (!outputPath) {
return;
}
dir = _path2.default.dirname(inputPath);
resolved = _path2.default.resolve(dir);
console.log("resolved", dir, resolved);
// Add Base Tag
if ($base.length === 0) {
$base = $('<base/>');
$('head').append($base);
}
$base.attr("href", "file://" + resolved + "/");
};
Hyphenator.prototype.walk = function ($, doThis, options) {
var ignore = IGNORE;
var only = options && options.only || ':root';
var hyphenator = this;
var processedText;
if (options && options.ignore) ignore += ', ' + options.ignore;
processedText = $(only).each(function () {
hyphenator.findTextNodes.call(hyphenator, $, this, doThis, ignore);
});
......
{
"name": "vivliostyle-electron",
"version": "0.0.5",
"version": "0.0.6",
"description": "Render Html to PDFs using Vivliostyle.js and Electron",
"repository": {
"type": "git",
......
import cheerio from 'cheerio';
import hypher from 'hypher';
import fs from 'fs';
import path from 'path';
const IGNORE = 'head, code, pre, script, style, [class^="pull-"], [class^="push-"], .small-caps';
......@@ -28,7 +29,12 @@ Hyphenator.prototype.hyphenateText = function (text) {
Hyphenator.prototype.process = function (input, output, encoding, options) {
var contents = fs.readFileSync(input, (options && options.encoding) || 'utf8');
var hyphenated = this.walk(contents, this.hyphenateText.bind(this), options);
var $doc = this.toDocument(contents);
var hyphenated;
// this.baseTag($doc, input, output);
hyphenated = this.walk($doc, this.hyphenateText.bind(this), options);
if (output) {
this.output(contents, output, encoding);
......@@ -63,22 +69,50 @@ Hyphenator.prototype.findTextNodes = function ($, node, doThis, ignore) {
}
Hyphenator.prototype.walk = function(html, doThis, options) {
var ignore = IGNORE;
var only = (options && options.only) || ':root';
var hyphenator = this;
if (options && options.ignore) ignore += ', ' + options.ignore;
Hyphenator.prototype.toDocument = function(html) {
var $ = cheerio.load(html, {
decodeEntities: false,
xmlMode: true
});
var processedText = $(only).each(function(){
return $
};
Hyphenator.prototype.baseTag = function($, inputPath, outputPath) {
var $base = $('base');
var dir, resolved;
if (!outputPath) {
return;
}
dir = path.dirname(inputPath);
resolved = path.resolve(dir);
console.log("resolved", dir, resolved);
// Add Base Tag
if($base.length === 0){
$base = $('<base/>');
$('head').append($base);
}
$base.attr("href", "file://" + resolved + "/");
};
Hyphenator.prototype.walk = function($, doThis, options) {
var ignore = IGNORE;
var only = (options && options.only) || ':root';
var hyphenator = this;
var processedText;
if (options && options.ignore) ignore += ', ' + options.ignore;
processedText = $(only).each(function(){
hyphenator.findTextNodes.call(hyphenator, $, this, doThis, ignore);
});
return $.html({
decodeEntities: false,
xmlMode: true
......
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