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 -> ½, ¼, ¾
// - miltiplication 2 x 4 -> 2 × 4
var COPY_RE = /\((c|tm|r|p)\)/i;
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) {
var i, token, text, inlineTokens, blkIdx;
@ -25,12 +44,7 @@ module.exports = function replace(state) {
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, '§');
}
text = replaceScopedAbbr(text);
if (RARE_RE.test(text)) {
text = text.replace(/\+-/g, '±')

Loading…
Cancel
Save