Browse Source

Splitted replacements & smartquotes to smaller functions

pull/82/head
Vitaly Puzrin 10 years ago
parent
commit
9ae876b0fe
  1. 48
      lib/rules_core/replacements.js
  2. 35
      lib/rules_core/smartquotes.js

48
lib/rules_core/replacements.js

@ -36,26 +36,26 @@ function replaceScopedAbbr(str) {
} }
module.exports = function replace(state) { function replace_scoped(inlineTokens) {
var i, token, text, inlineTokens, blkIdx; var i, token;
if (!state.md.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--) { for (i = inlineTokens.length - 1; i >= 0; i--) {
token = inlineTokens[i]; token = inlineTokens[i];
if (token.type === 'text') { if (token.type === 'text') {
text = token.content; token.content = replaceScopedAbbr(token.content);
}
}
}
text = replaceScopedAbbr(text); function replace_rare(inlineTokens) {
var i, token;
if (RARE_RE.test(text)) { for (i = inlineTokens.length - 1; i >= 0; i--) {
text = text.replace(/\+-/g, '±') token = inlineTokens[i];
if (token.type === 'text') {
if (RARE_RE.test(token.content)) {
token.content = token.content
.replace(/\+-/g, '±')
// .., ..., ....... -> … // .., ..., ....... -> …
// but ?..... & !..... -> ?.. & !.. // but ?..... & !..... -> ?.. & !..
.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..') .replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..')
@ -66,9 +66,27 @@ module.exports = function replace(state) {
.replace(/(^|\s)--(\s|$)/mg, '$1\u2013$2') .replace(/(^|\s)--(\s|$)/mg, '$1\u2013$2')
.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--) {
token.content = text; if (state.tokens[blkIdx].type !== 'inline') { continue; }
if (SCOPED_ABBR_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);
} }
} }
}; };

35
lib/rules_core/smartquotes.js

@ -16,27 +16,13 @@ function replaceAt(str, index, ch) {
return str.substr(0, index) + ch + str.substr(index + 1); return str.substr(0, index) + ch + str.substr(index + 1);
} }
function process_inlines(tokens, state) {
module.exports = function smartquotes(state) {
/*eslint max-depth:0*/
var i, token, text, t, pos, max, thisLevel, item, lastChar, nextChar, var i, token, text, t, pos, max, thisLevel, item, lastChar, nextChar,
isLastPunctChar, isNextPunctChar, isLastWhiteSpace, isNextWhiteSpace, isLastPunctChar, isNextPunctChar, isLastWhiteSpace, isNextWhiteSpace,
canOpen, canClose, j, isSingle, blkIdx, tokens, stack; canOpen, canClose, j, isSingle, stack;
if (!state.md.options.typographer) { return; }
stack = []; stack = [];
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
if (state.tokens[blkIdx].type !== 'inline' ||
!QUOTE_TEST_RE.test(state.tokens[blkIdx].content)) {
continue;
}
tokens = state.tokens[blkIdx].children;
stack.length = 0;
for (i = 0; i < tokens.length; i++) { for (i = 0; i < tokens.length; i++) {
token = tokens[i]; token = tokens[i];
@ -148,4 +134,21 @@ module.exports = function smartquotes(state) {
} }
} }
} }
module.exports = function smartquotes(state) {
/*eslint max-depth:0*/
var blkIdx;
if (!state.md.options.typographer) { return; }
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
if (state.tokens[blkIdx].type !== 'inline' ||
!QUOTE_TEST_RE.test(state.tokens[blkIdx].content)) {
continue;
}
process_inlines(state.tokens[blkIdx].children, state);
}
}; };

Loading…
Cancel
Save