diff --git a/benchmark/profile.js b/benchmark/profile.js index 5c0db03..176122a 100755 --- a/benchmark/profile.js +++ b/benchmark/profile.js @@ -13,7 +13,7 @@ var md = new Remarkable({ langPrefix: 'language-' }); -var data = fs.readFileSync(path.join(__dirname, '/samples/cdata.md'), 'utf8'); +var data = fs.readFileSync(path.join(__dirname, '/samples/lorem1.txt'), 'utf8'); for (var i = 0; i < 20000; i++) { md.render(data); diff --git a/lib/defaults.js b/lib/defaults.js index db9f5dd..fbd257f 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -9,5 +9,6 @@ module.exports = { breaks: false, maxLevel: 20, langPrefix: 'language-', + typograph: false, highlight: function (/*str*/) { return ''; } }; diff --git a/lib/defaults_typographer.js b/lib/defaults_typographer.js new file mode 100644 index 0000000..b4d7963 --- /dev/null +++ b/lib/defaults_typographer.js @@ -0,0 +1,18 @@ +// Default typograph options + +'use strict'; + + +module.exports = { + singleQuotes: '‘’', + doubleQuotes: '“”', // «» - russian, „“ - deutch + copyright: true, + trademark: true, + registered: true, + plusminus: true, + paragraph: true, + ellipsis: true, + dupes: true, + emDashes: true, + linkify: true +}; diff --git a/lib/typographer.js b/lib/typographer.js index f8020b1..a81ee1f 100644 --- a/lib/typographer.js +++ b/lib/typographer.js @@ -12,35 +12,55 @@ // - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾ // - miltiplication 2 x 4 -> 2 × 4 -var Ruler = require('./ruler'); +var assign = require('./common/utils').assign; +var defaults = require('./defaults_typographer'); +var Ruler = require('./ruler'); var rules = []; rules.push(function single(t, state) { - var i, token, text, tokens = state.tokens, - copyright = t.copyright, - trademark = t.trademark, - registered = t.registered, - plusminus = t.plusminus; - - for (i = state.tokens.length - 1; i >= 0; i--) { - if (tokens[i].type === 'text') { - token = tokens[i]; + var i, token, text, + tokens = state.tokens, + options = t.options; + + for (i = tokens.length - 1; i >= 0; i--) { + token = tokens[i]; + if (token.type === 'text') { text = token.content; - if (copyright && text.indexOf('(') >= 0) { - text = text.replace(/\(c\)/gi, '©'); + if (text.indexOf('(') >= 0) { + if (options.copyright) { + text = text.replace(/\(c\)/gi, '©'); + } + if (options.trademark) { + text = text.replace(/\(tm\)/gi, '™'); + } + if (options.registered) { + text = text.replace(/\(r\)/gi, '®'); + } + if (options.paragraph) { + text = text.replace(/\(p\)/gi, '§'); + } + } + + if (options.plusminus && text.indexOf('+-') >= 0) { + text = text.replace(/\+-/g, '±'); } - if (trademark && text.indexOf('(') >= 0) { - text = text.replace(/\(tm\)/gi, '™'); + if (options.ellipsis && text.indexOf('..') >= 0) { + // .., ..., ....... -> … + // but ?..... & !..... -> ?.. & !.. + text = text.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..'); } - if (registered && text.indexOf('(') >= 0) { - text = text.replace(/\(r\)/gi, '®'); + if (options.dupes && + (text.indexOf('????') >= 0 || + text.indexOf('!!!!') >= 0 || + text.indexOf(',,') >= 0)) { + text = text.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ','); } - if (plusminus && text.indexOf('+/-') >= 0) { - text = text.replace(/\+\/\-/g, '±'); + if (options.emDashes && text.indexOf('--') >= 0) { + text = text.replace(/(^|\s)--(\s|$)/mg, '$1—$2'); } token.content = text; @@ -50,12 +70,7 @@ rules.push(function single(t, state) { function Typographer() { - this.singleQuotes = '‘’'; - this.doubleQuotes = '“”'; // «» - russian, „“ - deutch - this.copyright = true; - this.trademark = true; - this.registered = true; - this.plusminus = true; + this.options = assign({}, defaults); this.ruler = new Ruler(this.rulesUpdate.bind(this)); @@ -70,6 +85,11 @@ Typographer.prototype.rulesUpdate = function () { }; +Typographer.prototype.set = function (options) { + assign(this.options, options); +}; + + Typographer.prototype.process = function (state) { var i, l, rules; diff --git a/test/fixtures/remarkable/typographer.txt b/test/fixtures/remarkable/typographer.txt index f21b03d..1afa319 100644 --- a/test/fixtures/remarkable/typographer.txt +++ b/test/fixtures/remarkable/typographer.txt @@ -22,9 +22,47 @@ trademark . +paragraph +. +(p) (P) +. +
§ §
+. + + plus-minus . -+/-5 ++-5 .±5
. + + +ellipsis +. +test.. test... test..... test?..... test!.... +. +test… test… test… test?.. test!..
+. + + +dupes +. +!!!!!! ???? ,, +. +!!! ??? ,
+. + + +em-dashes +. +-- remarkable -- super -- +. +— remarkable — super —
+. + + + +TODO: + +- "Monitor 21"" \ No newline at end of file