Skip to content

Commit

Permalink
Added typographer draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Oct 8, 2014
1 parent 9942fd5 commit 1342db2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bin/remarkable.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ readFile(options.file, 'utf8', function (error, input) {

md = new Remarkable({
html: true,
xhtml: true
xhtml: true,
typographer: true
});

try {
Expand Down
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var assign = require('object-assign');
var Renderer = require('./renderer');
var ParserBlock = require('./parser_block');
var ParserInline = require('./parser_inline');
var Typographer = require('./typographer');
var defaults = require('./defaults');

// Main class
Expand All @@ -20,10 +21,11 @@ function Remarkable(options) {
this.inline = new ParserInline();
this.block = new ParserBlock();
this.renderer = new Renderer();
this.typographer = new Typographer();

// a bunch of cross-references between parsers
// used for link reference definitions
// Cross-references to simplify code (a bit dirty, but easy).
this.block.inline = this.inline;
this.inline.typographer = this.typographer;

if (options) { this.set(options); }
}
Expand Down
5 changes: 5 additions & 0 deletions lib/parser_inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,18 @@ ParserInline.prototype.tokenize = function (state) {
return state.tokens;
};


// Parse input string.
//
ParserInline.prototype.parse = function (str, options, env) {
var state = new StateInline(str, this, options, env);

this.tokenize(state);

if (options.typographer && this.typographer) {
this.typographer.process(state);
}

return state.tokens;
};

Expand Down
86 changes: 86 additions & 0 deletions lib/typographer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Class of typographic replacements rules
//
// - single quotes
// - double quotes
// - em-dashes
// - link patterns
// - email patterns
//
'use strict';

// TODO:
// - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
// - miltiplication 2 x 4 -> 2 × 4

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];
text = token.content;

if (copyright && text.indexOf('(') >= 0) {
text = text.replace(/\(c\)/gi, '©');
}
if (trademark && text.indexOf('(') >= 0) {
text = text.replace(/\(tm\)/gi, '™');
}
if (registered && text.indexOf('(') >= 0) {
text = text.replace(/\(r\)/gi, '®');
}
if (plusminus && text.indexOf('+/-') >= 0) {
text = text.replace(/\+\/\-/g, '±');
}

token.content = text;
}
}
});


function Typographer() {
this.singleQuotes = '‘’';
this.doubleQuotes = '“”'; // «» - russian, „“ - deutch
this.copyright = true;
this.trademark = true;
this.registered = true;
this.plusminus = true;

this.ruler = new Ruler(this.rulesUpdate.bind(this));

for (var i = 0; i < rules.length; i++) {
this.ruler.after(rules[i]);
}
}


Typographer.prototype.rulesUpdate = function () {
this._rules = this.ruler.getRules();
};


Typographer.prototype.process = function (state) {
var i, l, rules;

if (!state.options.typographer) { return; }

rules = this._rules;

for (i = 0, l = rules.length; i < l; i++) {
rules[i](this, state);
}
};


module.exports = Typographer;

0 comments on commit 1342db2

Please sign in to comment.