Browse Source

Typographer: enchanced plain rules

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
1dcdd61850
  1. 2
      benchmark/profile.js
  2. 1
      lib/defaults.js
  3. 18
      lib/defaults_typographer.js
  4. 58
      lib/typographer.js
  5. 40
      test/fixtures/remarkable/typographer.txt

2
benchmark/profile.js

@ -13,7 +13,7 @@ var md = new Remarkable({
langPrefix: 'language-' 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++) { for (var i = 0; i < 20000; i++) {
md.render(data); md.render(data);

1
lib/defaults.js

@ -9,5 +9,6 @@ module.exports = {
breaks: false, breaks: false,
maxLevel: 20, maxLevel: 20,
langPrefix: 'language-', langPrefix: 'language-',
typograph: false,
highlight: function (/*str*/) { return ''; } highlight: function (/*str*/) { return ''; }
}; };

18
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
};

58
lib/typographer.js

@ -12,6 +12,8 @@
// - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾ // - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
// - miltiplication 2 x 4 -> 2 × 4 // - miltiplication 2 x 4 -> 2 × 4
var assign = require('./common/utils').assign;
var defaults = require('./defaults_typographer');
var Ruler = require('./ruler'); var Ruler = require('./ruler');
@ -19,28 +21,46 @@ var rules = [];
rules.push(function single(t, state) { rules.push(function single(t, state) {
var i, token, text, tokens = state.tokens, var i, token, text,
copyright = t.copyright, tokens = state.tokens,
trademark = t.trademark, options = t.options;
registered = t.registered,
plusminus = t.plusminus; for (i = tokens.length - 1; i >= 0; i--) {
for (i = state.tokens.length - 1; i >= 0; i--) {
if (tokens[i].type === 'text') {
token = tokens[i]; token = tokens[i];
if (token.type === 'text') {
text = token.content; text = token.content;
if (copyright && text.indexOf('(') >= 0) { if (text.indexOf('(') >= 0) {
if (options.copyright) {
text = text.replace(/\(c\)/gi, '©'); text = text.replace(/\(c\)/gi, '©');
} }
if (trademark && text.indexOf('(') >= 0) { if (options.trademark) {
text = text.replace(/\(tm\)/gi, '™'); text = text.replace(/\(tm\)/gi, '™');
} }
if (registered && text.indexOf('(') >= 0) { if (options.registered) {
text = text.replace(/\(r\)/gi, '®'); text = text.replace(/\(r\)/gi, '®');
} }
if (plusminus && text.indexOf('+/-') >= 0) { if (options.paragraph) {
text = text.replace(/\+\/\-/g, '±'); text = text.replace(/\(p\)/gi, '§');
}
}
if (options.plusminus && text.indexOf('+-') >= 0) {
text = text.replace(/\+-/g, '±');
}
if (options.ellipsis && text.indexOf('..') >= 0) {
// .., ..., ....... -> …
// but ?..... & !..... -> ?.. & !..
text = text.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..');
}
if (options.dupes &&
(text.indexOf('????') >= 0 ||
text.indexOf('!!!!') >= 0 ||
text.indexOf(',,') >= 0)) {
text = text.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',');
}
if (options.emDashes && text.indexOf('--') >= 0) {
text = text.replace(/(^|\s)--(\s|$)/mg, '$1—$2');
} }
token.content = text; token.content = text;
@ -50,12 +70,7 @@ rules.push(function single(t, state) {
function Typographer() { function Typographer() {
this.singleQuotes = '‘’'; this.options = assign({}, defaults);
this.doubleQuotes = '“”'; // «» - russian, „“ - deutch
this.copyright = true;
this.trademark = true;
this.registered = true;
this.plusminus = true;
this.ruler = new Ruler(this.rulesUpdate.bind(this)); 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) { Typographer.prototype.process = function (state) {
var i, l, rules; var i, l, rules;

40
test/fixtures/remarkable/typographer.txt

@ -22,9 +22,47 @@ trademark
. .
paragraph
.
(p) (P)
.
<p>§ §</p>
.
plus-minus plus-minus
. .
+/-5 +-5
. .
<p>±5</p> <p>±5</p>
. .
ellipsis
.
test.. test... test..... test?..... test!....
.
<p>test… test… test… test?.. test!..</p>
.
dupes
.
!!!!!! ???? ,,
.
<p>!!! ??? ,</p>
.
em-dashes
.
-- remarkable -- super --
.
<p>— remarkable — super —</p>
.
TODO:
- "Monitor 21""
Loading…
Cancel
Save