Commit b13155f8 authored by Fred Chasen's avatar Fred Chasen

Hyphenator fixes

parent e6786f47
#!/usr/bin/env node
var Renderer = require("./lib/index.js");
var Hyphenate = require("./lib/hyphenator.js");
var Hyphenator = require("./lib/hyphenator.js");
var program = require('commander');
var path = require('path');
var fs = require('fs');
......@@ -43,7 +43,7 @@ var renderer = new Renderer(program);
var dir = process.cwd();
var relativePath = path.resolve(dir, input);
var output;
var tmpPath;
var tmpFile, tmpPath;
var hyphenator;
var hyphenateOptions;
......@@ -75,14 +75,17 @@ if (program.hyphenate) {
}
// tmpPath = replaceExt(relativePath, ".hyphenated.html");
tmpPath = temp.openSync({suffix: '.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);
console.log("Hyphenated for",
typeof(program.hyphenate) === "string" ? program.hyphenate : "en-us");
if (program.debug && tmpPath) {
conole.log("Hyphenated at: ", tmpPath);
console.log("Hyphenated file at:", tmpPath);
}
}
......
......@@ -20,8 +20,14 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
var IGNORE = 'head, code, pre, script, style, [class^="pull-"], [class^="push-"], .small-caps';
function Hyphenator(lang) {
this.lang = lang || "en-us";
function Hyphenator(_lang) {
var lang;
if (typeof _lang === "string") {
lang = _lang;
} else {
lang = "en-us";
}
this.lang = lang;
this.pattern = require("hyphenation." + this.lang);
this.h = new _hypher2.default(this.pattern);
}
......@@ -41,7 +47,7 @@ Hyphenator.prototype.process = function (input, output, encoding, options) {
var hyphenated = this.walk(contents, this.hyphenateText.bind(this), options);
if (output) {
this.ouput(contents, output, encoding);
this.output(contents, output, encoding);
}
return hyphenated;
......@@ -51,7 +57,7 @@ Hyphenator.prototype.output = function (contents, outputPath, encoding) {
_fs2.default.writeFileSync(outputPath, contents, encoding || 'utf8', encoding);
};
Hyphenator.prototype.findTextNodes = function (node, doThis, ignore) {
Hyphenator.prototype.findTextNodes = function ($, node, doThis, ignore) {
var hyphenator = this;
if ($(node).is(ignore)) return false;
......@@ -66,7 +72,7 @@ Hyphenator.prototype.findTextNodes = function (node, doThis, ignore) {
if (childNode.type === 'text' || !childNode.type) {
childNode.data = doThis(childNode.data);
} else {
hyphenator.findTextNodes(childNode, doThis, hyphenator);
hyphenator.findTextNodes.call(hyphenator, $, this, doThis, ignore);
}
});
};
......@@ -84,7 +90,7 @@ Hyphenator.prototype.walk = function (html, doThis, options) {
});
var processedText = $(only).each(function () {
hyphenator.findTextNodes(this, doThis, ignore).bind(hyphenator);
hyphenator.findTextNodes.call(hyphenator, $, this, doThis, ignore);
});
return $.html({
......
......@@ -4,8 +4,14 @@ import fs from 'fs';
const IGNORE = 'head, code, pre, script, style, [class^="pull-"], [class^="push-"], .small-caps';
function Hyphenator(lang) {
this.lang = lang || "en-us";
function Hyphenator(_lang) {
var lang;
if (typeof(_lang) === "string") {
lang = _lang;
} else {
lang = "en-us";
}
this.lang = lang;
this.pattern = require("hyphenation."+this.lang);
this.h = new hypher(this.pattern);
}
......@@ -25,7 +31,7 @@ Hyphenator.prototype.process = function (input, output, encoding, options) {
var hyphenated = this.walk(contents, this.hyphenateText.bind(this), options);
if (output) {
this.ouput(contents, output, encoding);
this.output(contents, output, encoding);
}
return hyphenated;
......@@ -36,7 +42,7 @@ Hyphenator.prototype.output = function (contents, outputPath, encoding) {
};
Hyphenator.prototype.findTextNodes = function (node, doThis, ignore) {
Hyphenator.prototype.findTextNodes = function ($, node, doThis, ignore) {
var hyphenator = this;
if ($(node).is(ignore)) return false;
......@@ -51,7 +57,7 @@ Hyphenator.prototype.findTextNodes = function (node, doThis, ignore) {
if (childNode.type === 'text' || !childNode.type) {
childNode.data = doThis(childNode.data);
} else {
hyphenator.findTextNodes(childNode, doThis, hyphenator);
hyphenator.findTextNodes.call(hyphenator, $, this, doThis, ignore);
}
});
......@@ -70,7 +76,7 @@ Hyphenator.prototype.walk = function(html, doThis, options) {
});
var processedText = $(only).each(function(){
hyphenator.findTextNodes(this, doThis, ignore).bind(hyphenator);
hyphenator.findTextNodes.call(hyphenator, $, this, doThis, ignore);
});
return $.html({
......
......@@ -3,6 +3,7 @@ var Renderer = require("../lib/index.js");
var spawn = require('child_process').spawn;
var path = __dirname.replace("test", '') + "cli.js";
var fs = require('fs');
describe('CLI', function() {
......@@ -81,6 +82,34 @@ describe('CLI', function() {
it('should output to a given path', function(done) {
var cli = spawn(path, ["./test/samples/wood/index.html", "--output", "./test/output/wood.pdf"])
var exists = false;
cli.on('close', function(code){
fs.access("./test/output/wood.pdf", fs.F_OK, function(err) {
if (err) {
exists = false
} else {
exists = true;
}
assert.equal(exists, true);
done();
});
});
});
it('should Hyphenate when giving the --hyphenate option', function(done) {
var cli = spawn(path, ["./test/samples/wood/index.html", "--hyphenate"])
cli.stdout.on('data', function(data) {
var dataStr = data.toString();
console.log(dataStr);
assert.equal(dataStr, "Hyphenated for en-us\n");
});
cli.on('close', function(code){
assert.equal(code, 0);
......
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