|
|
|
// Simple typographyc replacements
|
|
|
|
//
|
|
|
|
// (c) (C) → ©
|
|
|
|
// (tm) (TM) → ™
|
|
|
|
// (r) (R) → ®
|
|
|
|
// +- → ±
|
|
|
|
// (p) (P) -> §
|
|
|
|
// ... → … (also ?.... → ?.., !.... → !..)
|
|
|
|
// ???????? → ???, !!!!! → !!!, `,,` → `,`
|
|
|
|
// -- → –, --- → —
|
|
|
|
//
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
|
|
|
|
// - miltiplication 2 x 4 -> 2 × 4
|
|
|
|
|
|
|
|
var RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/;
|
|
|
|
|
|
|
|
// Workaround for phantomjs - need regex without /g flag,
|
|
|
|
// or root check will fail every second time
|
|
|
|
var SCOPED_ABBR_TEST_RE = /\((c|tm|r|p)\)/i;
|
|
|
|
|
|
|
|
var SCOPED_ABBR_RE = /\((c|tm|r|p)\)/ig;
|
|
|
|
var SCOPED_ABBR = {
|
|
|
|
'c': '©',
|
|
|
|
'r': '®',
|
|
|
|
'p': '§',
|
|
|
|
'tm': '™'
|
|
|
|
};
|
|
|
|
|
|
|
|
function replaceFn(match, name) {
|
|
|
|
return SCOPED_ABBR[name.toLowerCase()];
|
|
|
|
}
|
|
|
|
|
|
|
|
function replace_scoped(inlineTokens) {
|
|
|
|
var i, token;
|
|
|
|
|
|
|
|
for (i = inlineTokens.length - 1; i >= 0; i--) {
|
|
|
|
token = inlineTokens[i];
|
|
|
|
if (token.type === 'text') {
|
|
|
|
token.content = token.content.replace(SCOPED_ABBR_RE, replaceFn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function replace_rare(inlineTokens) {
|
|
|
|
var i, token;
|
|
|
|
|
|
|
|
for (i = inlineTokens.length - 1; i >= 0; i--) {
|
|
|
|
token = inlineTokens[i];
|
|
|
|
if (token.type === 'text') {
|
|
|
|
if (RARE_RE.test(token.content)) {
|
|
|
|
token.content = token.content
|
|
|
|
.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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function replace(state) {
|
|
|
|
var blkIdx;
|
|
|
|
|
|
|
|
if (!state.md.options.typographer) { return; }
|
|
|
|
|
|
|
|
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
|
|
|
|
|
|
|
|
if (state.tokens[blkIdx].type !== 'inline') { continue; }
|
|
|
|
|
|
|
|
if (SCOPED_ABBR_TEST_RE.test(state.tokens[blkIdx].content)) {
|
|
|
|
replace_scoped(state.tokens[blkIdx].children);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RARE_RE.test(state.tokens[blkIdx].content)) {
|
|
|
|
replace_rare(state.tokens[blkIdx].children);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|