Browse Source

Optimized typographer replacements

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
e0765a9d04
  1. 28
      lib/rules_core/replacements.js

28
lib/rules_core/replacements.js

@ -6,9 +6,28 @@
// - 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 COPY_RE = /\((c|tm|r|p)\)/i;
var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/; var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/;
var SCOPED_ABBR_RE = /\((c|tm|r|p)\)/ig;
var SCOPED_ABBR = {
'c': '©',
'r': '®',
'p': '§',
'tm': '™'
};
function replaceScopedAbbr(str) {
if (str.indexOf('(') < 0) { return str; }
return str.replace(SCOPED_ABBR_RE, function(match, name) {
if (SCOPED_ABBR.hasOwnProperty(name.toLowerCase())) {
return SCOPED_ABBR[name.toLowerCase()];
}
return match;
});
}
module.exports = function replace(state) { module.exports = function replace(state) {
var i, token, text, inlineTokens, blkIdx; var i, token, text, inlineTokens, blkIdx;
@ -25,12 +44,7 @@ module.exports = function replace(state) {
if (token.type === 'text') { if (token.type === 'text') {
text = token.content; text = token.content;
if (COPY_RE.test(text)) { text = replaceScopedAbbr(text);
text = text.replace(/\(c\)/gi, '©')
.replace(/\(tm\)/gi, '™')
.replace(/\(r\)/gi, '®')
.replace(/\(p\)/gi, '§');
}
if (RARE_RE.test(text)) { if (RARE_RE.test(text)) {
text = text.replace(/\+-/g, '±') text = text.replace(/\+-/g, '±')

Loading…
Cancel
Save