Vitaly Puzrin
10 years ago
8 changed files with 166 additions and 139 deletions
@ -0,0 +1,41 @@ |
|||
// Class of top level (`core`) rules
|
|||
//
|
|||
'use strict'; |
|||
|
|||
|
|||
var Ruler = require('./ruler'); |
|||
|
|||
|
|||
var _rules = [ |
|||
[ 'block', require('./rules_core/block') ], |
|||
[ 'references', require('./rules_core/references') ], |
|||
[ 'inline', require('./rules_core/inline') ], |
|||
[ 'replacements', require('./rules_core/replacements') ], |
|||
[ 'smartquotes', require('./rules_core/smartquotes') ], |
|||
[ 'linkify', require('./rules_core/linkify') ] |
|||
]; |
|||
|
|||
|
|||
function Core() { |
|||
this.options = {}; |
|||
|
|||
this.ruler = new Ruler(); |
|||
|
|||
for (var i = 0; i < _rules.length; i++) { |
|||
this.ruler.push(_rules[i][0], _rules[i][1]); |
|||
} |
|||
} |
|||
|
|||
|
|||
Core.prototype.process = function (state) { |
|||
var i, l, rules; |
|||
|
|||
rules = this.ruler.getRules(''); |
|||
|
|||
for (i = 0, l = rules.length; i < l; i++) { |
|||
rules[i](state); |
|||
} |
|||
}; |
|||
|
|||
|
|||
module.exports = Core; |
@ -0,0 +1,52 @@ |
|||
// Simple typographyc replacements
|
|||
//
|
|||
'use strict'; |
|||
|
|||
// TODO:
|
|||
// - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
|
|||
// - miltiplication 2 x 4 -> 2 × 4
|
|||
|
|||
var COPY_RE = /\((c|tm|r|p)\)/i; |
|||
var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/; |
|||
|
|||
module.exports = function replace(state) { |
|||
var i, token, text, inlineTokens, blkIdx; |
|||
|
|||
if (!state.options.typographer) { return; } |
|||
|
|||
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) { |
|||
|
|||
if (state.tokens[blkIdx].type !== 'inline') { continue; } |
|||
|
|||
inlineTokens = state.tokens[blkIdx].children; |
|||
|
|||
for (i = inlineTokens.length - 1; i >= 0; i--) { |
|||
token = inlineTokens[i]; |
|||
if (token.type === 'text') { |
|||
text = token.content; |
|||
|
|||
if (COPY_RE.test(text)) { |
|||
text = text.replace(/\(c\)/gi, '©') |
|||
.replace(/\(tm\)/gi, '™') |
|||
.replace(/\(r\)/gi, '®') |
|||
.replace(/\(p\)/gi, '§'); |
|||
} |
|||
|
|||
if (RARE_RE.test(text)) { |
|||
text = text.replace(/\+-/g, '±') |
|||
// .., ..., ....... -> …
|
|||
// but ?..... & !..... -> ?.. & !..
|
|||
.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..') |
|||
.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',') |
|||
// em-dash
|
|||
.replace(/(^|[^-])---([^-]|$)/mg, '$1\u2014$2') |
|||
// en-dash
|
|||
.replace(/(^|\s)--(\s|$)/mg, '$1\u2013$2') |
|||
.replace(/(^|[^-\s])--([^-\s]|$)/mg, '$1\u2013$2'); |
|||
} |
|||
|
|||
token.content = text; |
|||
} |
|||
} |
|||
} |
|||
}; |
@ -1,66 +0,0 @@ |
|||
// Simple typographyc replacements
|
|||
//
|
|||
'use strict'; |
|||
|
|||
|
|||
var COPY_RE = /\((c|tm|r|p)\)/i; |
|||
var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/; |
|||
|
|||
module.exports = function replace(state) { |
|||
var i, token, text, inlineTokens, blkIdx, |
|||
typographer = state.typographer, |
|||
options = typographer.options; |
|||
|
|||
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) { |
|||
|
|||
if (state.tokens[blkIdx].type !== 'inline') { continue; } |
|||
|
|||
inlineTokens = state.tokens[blkIdx].children; |
|||
|
|||
for (i = inlineTokens.length - 1; i >= 0; i--) { |
|||
token = inlineTokens[i]; |
|||
if (token.type === 'text') { |
|||
text = token.content; |
|||
|
|||
if (COPY_RE.test(text)) { |
|||
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 (RARE_RE.test(text)) { |
|||
if (options.plusminus) { |
|||
text = text.replace(/\+-/g, '±'); |
|||
} |
|||
if (options.ellipsis) { |
|||
// .., ..., ....... -> …
|
|||
// but ?..... & !..... -> ?.. & !..
|
|||
text = text.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..'); |
|||
} |
|||
if (options.dupes) { |
|||
text = text.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ','); |
|||
} |
|||
if (options.dashes) { |
|||
text = text |
|||
// em-dash
|
|||
.replace(/(^|[^-])---([^-]|$)/mg, '$1\u2014$2') |
|||
// en-dash
|
|||
.replace(/(^|\s)--(\s|$)/mg, '$1\u2013$2') |
|||
.replace(/(^|[^-\s])--([^-\s]|$)/mg, '$1\u2013$2'); |
|||
} |
|||
} |
|||
|
|||
token.content = text; |
|||
} |
|||
} |
|||
} |
|||
}; |
Loading…
Reference in new issue