From f16a5e690a5a9591cb0eea848f9d0668d073b79e Mon Sep 17 00:00:00 2001 From: David Merfield Date: Mon, 17 Aug 2015 00:10:00 -0700 Subject: [PATCH] extended options --- build.js | 5 ++++- index.html | 7 +++--- package.json | 2 +- src/eachTextNode.js | 45 +++++++++++++++++++-------------------- src/hangingPunctuation.js | 35 +++++++++++++++--------------- src/hyphenate.js | 6 +++--- src/index.js | 7 ++---- src/ligatures.js | 9 +++----- src/punctuation.js | 9 ++++---- src/quotes.js | 9 ++++---- src/smallCaps.js | 6 +++--- src/spaces.js | 13 ++++++----- tests/assert.js | 9 ++++++++ tests/index-src.html | 1 - 14 files changed, 82 insertions(+), 81 deletions(-) create mode 100644 tests/assert.js diff --git a/build.js b/build.js index 62eef7f..7bcf6c1 100644 --- a/build.js +++ b/build.js @@ -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!'); diff --git a/index.html b/index.html index 60922c9..59bf5e3 100644 --- a/index.html +++ b/index.html @@ -31,17 +31,16 @@

A ty­po­graphic pre-proces­
-

Yjarni Sigurðardót­tir spoke to NATO from Ice­land yes­ter­day: Light of my life, fire of my florins — my sin, my soul. The tip of the tongue tak­ing a trip to 118° 19′ 43.5″.”

-

She’s faster than a 120′ 4″ whale.” Piña co­ladas were widely con­sumed in Göt­ter­däm­merung from 1880–1912. For the low price of $20 / year from Ex­hi­bits A–E… Then the du­plex came for­ward. Thrice the tower, he mounted the round gun­rest, awak­ing’ HTML. He can print a fixed num­ber of dots in a square inch (for in­stance, 600 × 600).” +

She’s faster than a 120′ 4″ whale.” Piña co­ladas were widely con­sumed in Göt­ter­däm­merung from 1880–1912. For the low price of $20 / year from Ex­hi­bits A–E… Then the du­plex came for­ward. Thrice the tower, he mounted the round gun­rest, awak­ing’ HTML. He can print a fixed num­ber of dots in a square inch (for in­stance, 600 × 600).”

diff --git a/package.json b/package.json index 4c71cab..964bae8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "typeset", - "version": "0.1.2", + "version": "0.1.3", "description": "Typesetting for the web", "main": "src/index.js", "scripts": { diff --git a/src/eachTextNode.js b/src/eachTextNode.js index 1c84218..5c7624f 100644 --- a/src/eachTextNode.js +++ b/src/eachTextNode.js @@ -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(); -} +}; \ No newline at end of file diff --git a/src/hangingPunctuation.js b/src/hangingPunctuation.js index 87ae46e..fa4b8ee 100644 --- a/src/hangingPunctuation.js +++ b/src/hangingPunctuation.js @@ -1,23 +1,23 @@ var eachTextNode = require('./eachTextNode'); -var cheerio = require('cheerio'); module.exports = function smallCaps (html, options) { + function pull (className, content) { + return '' + (content || '') + ''; + } -function pull (className, content) { - return '' + (content || '') + ''; -} - -function push (className, content) { - return '' + (content || '') + ''; -} + function push (className, content) { + return '' + (content || '') + ''; + } var doubleWidth = ['"', '"', "“", "„", "”", "“", "“", "“", "“", "”", "”", '”', '”', '„', '„', '„']; var singleWidth = ["'", '′', ''', '‘', '’', '‘', '’']; 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. @@ -25,9 +25,9 @@ function push (className, content) { 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); @@ -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) { @@ -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) { @@ -70,7 +70,8 @@ function push (className, content) { text = words.join(' '); return text; - }); + + }, options); return html; -} +}; \ No newline at end of file diff --git a/src/hyphenate.js b/src/hyphenate.js index e9055fa..c734fd3 100644 --- a/src/hyphenate.js +++ b/src/hyphenate.js @@ -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; -} +}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index d255de7..b8cc4fc 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,3 @@ -var cheerio = require('cheerio'); var modules = { quotes: require('./quotes'), hyphenate: require('./hyphenate'), @@ -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; -} +}; \ No newline at end of file diff --git a/src/ligatures.js b/src/ligatures.js index 7936f15..f1fc3ae 100644 --- a/src/ligatures.js +++ b/src/ligatures.js @@ -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; -} - - +}; \ No newline at end of file diff --git a/src/punctuation.js b/src/punctuation.js index b223f9f..f888fd5 100644 --- a/src/punctuation.js +++ b/src/punctuation.js @@ -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(' — '); // Elipses - text = text.split('...').join("…"); + text = text.split('...').join('…'); // Nbsp for punc with spaces var NBSP = ' '; @@ -21,7 +20,7 @@ module.exports = function smallCaps (html, options) { .replace(NBSP_PUNCTUATION_END, NBSP + '$1'); return text; - }); + }, options); return html; -} +}; \ No newline at end of file diff --git a/src/quotes.js b/src/quotes.js index 05668d2..c3f2c8b 100644 --- a/src/quotes.js +++ b/src/quotes.js @@ -1,9 +1,8 @@ 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 @@ -11,7 +10,7 @@ module.exports = function smallCaps (html, options) { text = text.split('"').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 ' @@ -29,7 +28,7 @@ module.exports = function smallCaps (html, options) { text = text.split('\\\’').join('\''); text = text.split('\\\‘').join('\''); return text; - }); + }, options); return html; -} +}; diff --git a/src/smallCaps.js b/src/smallCaps.js index 7183cb1..84b4564 100644 --- a/src/smallCaps.js +++ b/src/smallCaps.js @@ -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(' '); @@ -81,7 +81,7 @@ module.exports = function smallCaps (html, options) { } return wordList.join(' '); - }); + }, options); return html; -} +}; \ No newline at end of file diff --git a/src/spaces.js b/src/spaces.js index ca1cddb..8e16b57 100644 --- a/src/spaces.js +++ b/src/spaces.js @@ -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(' × '); - - text = text.split(' / ').join(' / '); + // replaces wide spaces with hair spaces + text = text.split(' × ').join(' × '); + text = text.split(' / ').join(' / '); return text; - }); + }, options); return html; -} +}; \ No newline at end of file diff --git a/tests/assert.js b/tests/assert.js new file mode 100644 index 0000000..37c1f34 --- /dev/null +++ b/tests/assert.js @@ -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)))); \ No newline at end of file diff --git a/tests/index-src.html b/tests/index-src.html index 23b2bd7..0f970a9 100644 --- a/tests/index-src.html +++ b/tests/index-src.html @@ -37,7 +37,6 @@

A typographic pre-processor for your HTML which uses <
-

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"."