Skip to content

Commit

Permalink
extended options
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmerfield committed Aug 17, 2015
1 parent 2d134db commit f16a5e6
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 81 deletions.
5 changes: 4 additions & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ function build () {
var $ = cheerio.load(html, {decodeEntities: false});
$('#panel-2').html($('#panel-1').html());

var options = {};
var options = {
ignore: '.ts-ignore'
};

html = typeset($.html(), options);
// html = typeset(html, options);

if (html) fs.writeFileSync(OUTPUT, minify(html, minifyOpts));
console.log('DONE!');
Expand Down
7 changes: 3 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ <h2 style=max-width:80%><span class=pull-A>A</span> ty­po­graphic pre-proces­
<div class=clear></div>

<div class="small-caps nav tabs" data-directive=tabs>
<a href=#panel-1 data-behaviour=tab><span class=pull-A>A</span>f­ter<span class=push-T></span> <span class=pull-T>T</span>ype­set.js</a>
<a href=#panel-2 data-behaviour=tab>Be­fore</a>
<a href=#panel-1 data-behaviour=tab>After Typeset.js</a>
<a href=#panel-2 data-behaviour=tab>Before</a>
</div>
<div class=demo>



<div id=panel-1>
<p><span class=pull-Y>Y</span>jarni Sigurðardót­tir spoke to <span class=small-caps>NATO</span> from Ice­land yes­ter­day:<span class=push-double></span> <span class=pull-double></span>Light<span class=push-o></span> <span class=pull-o>o</span>f my life, fire<span class=push-o></span> <span class=pull-o>o</span>f my florins&thinsp;&mdash;&thinsp;my sin, my soul.<span class=push-T></span> <span class=pull-T>T</span>he tip<span class=push-o></span> <span class=pull-o>o</span>f the tongue tak­ing a trip to 118° 19′ 43.5″.”</p>

<p><span class=pull-double></span>She’s faster than a 120′ 4″<span class=push-w></span> <span class=pull-w>w</span>hale.” <em>Piña<span class=push-c></span> <span class=pull-c>c</span>o­ladas</em> <span class=pull-w>w</span>ere<span class=push-w></span> <span class=pull-w>w</span>idely<span class=push-c></span> <span class=pull-c>c</span>on­sumed in Göt­ter­däm­merung from 1880–1912. For the low price<span class=push-o></span> <span class=pull-o>o</span>f $20&hairsp;/&hairsp;year from Ex­hi­bits<span class=push-A></span> <span class=pull-A>A</span>–E&hellip;<span class=push-T></span> <span class=pull-T>T</span>hen the <em>du­plex</em> <span class=pull-c>c</span>ame for­ward.<span class=push-double></span> <span class=pull-double></span>Thrice the tower, he mounted the round gun­rest,<span class=push-single></span> <span class=pull-single></span>awak­ing’ <span class=small-caps>HTML</span>. He<span class=push-c></span> <span class=pull-c>c</span>an print a fixed num­ber<span class=push-o></span> <span class=pull-o>o</span>f dots in a square inch (for in­stance, 600&hairsp;×&hairsp;600).”
<p><span class=pull-double></span>She’s faster than a 120′ 4″<span class=push-w></span> <span class=pull-w>w</span>hale.” <em>Piña<span class=push-c></span> <span class=pull-c>c</span>o­ladas</em> <span class=pull-w>w</span>ere<span class=push-w></span> <span class=pull-w>w</span>idely<span class=push-c></span> <span class=pull-c>c</span>on­sumed in Göt­ter­däm­merung from 1880–1912. For the low price<span class=push-o></span> <span class=pull-o>o</span>f $20 / year from Ex­hi­bits<span class=push-A></span> <span class=pull-A>A</span>–E…<span class=push-T></span> <span class=pull-T>T</span>hen the <em>du­plex</em> <span class=pull-c>c</span>ame for­ward.<span class=push-double></span> <span class=pull-double></span>Thrice the tower, he mounted the round gun­rest,<span class=push-single></span> <span class=pull-single></span>awak­ing’ <span class=small-caps>HTML</span>. He<span class=push-c></span> <span class=pull-c>c</span>an print a fixed num­ber<span class=push-o></span> <span class=pull-o>o</span>f dots in a square inch (for in­stance, 600 × 600).”
</p>

</div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typeset",
"version": "0.1.2",
"version": "0.1.3",
"description": "Typesetting for the web",
"main": "src/index.js",
"scripts": {
Expand Down
45 changes: 22 additions & 23 deletions src/eachTextNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,38 @@ var cheerio = require('cheerio');
// doThis accepts a text string of a text node's content
// and returns the modified string.

var IGNORE = 'head, code, pre, script, style, .ts-ignore';
var IGNORE = 'head, code, pre, script, style, [class^="pull-"], [class^="push-"], .small-caps';

module.exports = function forEachTextNode (html, doThis) {
module.exports = function forEachTextNode (html, doThis, options) {

var $ = cheerio.load(html, {decodeEntities: false});

$(":root").each(function(){
var ignore = IGNORE;
var only = options.only || ':root';

findTextNodes(this);
if (options.ignore) ignore += ', ' + options.ignore;

function findTextNodes(node) {
var $ = cheerio.load(html, {decodeEntities: false});

if ($(node).is(IGNORE)) return false;
$(only).each(function(){findTextNodes(this);});

if (IGNORE.indexOf() > -1) return false;
function findTextNodes(node) {

$(node).contents().each(function(){
if ($(node).is(ignore)) return false;

var childNode = $(this)[0];
$(node).contents().each(function(){

// We've made it to a text node!
// apply the function which transforms
// its text content (childNode.data)
if (childNode.type === 'text') {
childNode.data = doThis(childNode.data, childNode);
} else {
findTextNodes(childNode, doThis);
}
});
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.data = doThis(childNode.data, childNode);
} else {
findTextNodes(childNode, doThis);
}
});

});
}

return $.html();
}
};
35 changes: 18 additions & 17 deletions src/hangingPunctuation.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
var eachTextNode = require('./eachTextNode');
var cheerio = require('cheerio');

module.exports = function smallCaps (html, options) {

function pull (className, content) {
return '<span class="pull-' + className +'">' + (content || '') + '</span>';
}

function pull (className, content) {
return '<span class="pull-' + className +'">' + (content || '') + '</span>';
}

function push (className, content) {
return '<span class="push-' + className +'">' + (content || '') + '</span>';
}
function push (className, content) {
return '<span class="push-' + className +'">' + (content || '') + '</span>';
}

var doubleWidth = ['&quot;', '"', "“", "„", "”", "&ldquo;", "&OpenCurlyDoubleQuote;", "&#8220;", "&#x0201C;", "&rdquor;", "&rdquo;", '&CloseCurlyDoubleQuote;', '&#8221;', '&ldquor;', '&bdquo;', '&#8222;'];
var singleWidth = ["'", '&prime;', '&apos;', '&lsquo;', '&rsquo;', '‘', '’'];

var alignMe = "CcOoYTAVvWwY".split('');

html = eachTextNode(html, function(text, node){
html = eachTextNode(html, function(text){

if (text.length < 2) return text;

// Remove consecutive double spaces then create
// array of distinct words.
var words = text.split(' ').join(' ').split(' ');

for (var i in words) {

for (var j in alignMe) {
for (var a in alignMe) {

var align = alignMe[j];
var align = alignMe[a];

if (words[i].slice(0,align.length) === align) {
words[i] = pull(align, align) + words[i].slice(align.length);
Expand All @@ -38,9 +38,9 @@ function push (className, content) {
}
}

for (var j in singleWidth) {
for (var b in singleWidth) {

var punctuation = singleWidth[j];
var punctuation = singleWidth[b];

if (words[i].slice(0,punctuation.length) === punctuation) {

Expand All @@ -52,9 +52,9 @@ function push (className, content) {
}
}

for (var j in doubleWidth) {
for (var c in doubleWidth) {

var punctuation = doubleWidth[j];
var punctuation = doubleWidth[c];

if (words[i].slice(0,punctuation.length) === punctuation) {

Expand All @@ -70,7 +70,8 @@ function push (className, content) {
text = words.join(' ');

return text;
});

}, options);

return html;
}
};
6 changes: 3 additions & 3 deletions src/hyphenate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ var Hypher = require('hypher'),

module.exports = function smallCaps (html, options) {

html = eachTextNode(html, function(text, node){
html = eachTextNode(html, function(text){
return h.hyphenateText(text);
});
}, options);

return html;
}
};
7 changes: 2 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var cheerio = require('cheerio');
var modules = {
quotes: require('./quotes'),
hyphenate: require('./hyphenate'),
Expand All @@ -7,17 +6,15 @@ var modules = {
punctuation: require('./punctuation'),
hangingPunctuation: require('./hangingPunctuation'),
spaces: require('./spaces')

};

module.exports = function typeset (html, options) {

// Create a backup
var _html = html;
options = options || {};

// Pass the HTML to each module
for (var i in modules)
html = modules[i](html, options);

return html;
}
};
9 changes: 3 additions & 6 deletions src/ligatures.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
var eachTextNode = require('./eachTextNode');
var cheerio = require('cheerio');

module.exports = function smallCaps (html, options) {

html = eachTextNode(html, function(text, node){
html = eachTextNode(html, function(text){

text = text.split('fi').join('fi');
text = text.split('fl').join('fl');

return text;
});
}, options);

return html;
}


};
9 changes: 4 additions & 5 deletions src/punctuation.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
var eachTextNode = require('./eachTextNode');
var cheerio = require('cheerio');

module.exports = function smallCaps (html, options) {

html = eachTextNode(html, function(text, node){
html = eachTextNode(html, function(text){

// Dashes
text = text.split('--').join('–');
text = text.split(' – ').join('&thinsp;&mdash;&thinsp;');

// Elipses
text = text.split('...').join("&hellip;");
text = text.split('...').join('…');

// Nbsp for punc with spaces
var NBSP = '&nbsp;';
Expand All @@ -21,7 +20,7 @@ module.exports = function smallCaps (html, options) {
.replace(NBSP_PUNCTUATION_END, NBSP + '$1');

return text;
});
}, options);

return html;
}
};
9 changes: 4 additions & 5 deletions src/quotes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
var eachTextNode = require('./eachTextNode');
var cheerio = require('cheerio');

module.exports = function smallCaps (html, options) {

html = eachTextNode(html, function(text, node){
html = eachTextNode(html, function(text){

// Revert encoded chars so the regex mystery
// below works properly
text = text.split('&#39;').join("'");
text = text.split('&quot;').join('"');

text = text
.replace(/(\W|^)"(\S)/g, '$1\u201c$2') // beginning "
.replace(/(\W|^)"(\S)/g, '$1\u201c$2') // beginning "
.replace(/(\u201c[^"]*)"([^"]*$|[^\u201c"]*\u201c)/g, '$1\u201d$2') // ending "
.replace(/([^0-9])"/g,'$1\u201d') // remaining " at end of word
.replace(/(\W|^)'(\S)/g, '$1\u2018$2') // beginning '
Expand All @@ -29,7 +28,7 @@ module.exports = function smallCaps (html, options) {
text = text.split('\\\’').join('\'');
text = text.split('\\\‘').join('\'');
return text;
});
}, options);

return html;
}
};
6 changes: 3 additions & 3 deletions src/smallCaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function removeCruft (word) {

module.exports = function smallCaps (html, options) {

html = eachTextNode(html, function(text, node){
html = eachTextNode(html, function(text){

var wordList = text.split(' ');

Expand All @@ -81,7 +81,7 @@ module.exports = function smallCaps (html, options) {
}

return wordList.join(' ');
});
}, options);

return html;
}
};
13 changes: 6 additions & 7 deletions src/spaces.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
var eachTextNode = require('./eachTextNode');
var cheerio = require('cheerio');

module.exports = function smallCaps (html, options) {

html = eachTextNode(html, function(text, node){
html = eachTextNode(html, function(text){

text = text.split(' × ').join('&hairsp;×&hairsp;');

text = text.split(' / ').join('&hairsp;/&hairsp;');
// replaces wide spaces with hair spaces
text = text.split(' × ').join(' × ');
text = text.split(' / ').join(' / ');

return text;
});
}, options);

return html;
}
};
9 changes: 9 additions & 0 deletions tests/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var typeset = require('../src');
var assert = require('assert');
var fs = require('fs');

var html = fs.readFileSync(__dirname + '/index-src.html', 'utf-8');


console.log(typeset(html));
console.log(typeset(typeset(typeset(html))));
1 change: 0 additions & 1 deletion tests/index-src.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ <h2 style="max-width:80%">A typographic pre-processor for your HTML which uses <
<div class="demo">



<div id="panel-1">
<p>Yjarni Sigurðardóttir spoke to NATO from Iceland yesterday: "Light of my life, fire of my florins -- my sin, my soul. The tip of the tongue taking a trip to 118° 19' 43.5"."</p>

Expand Down

0 comments on commit f16a5e6

Please sign in to comment.