|
|
@ -20,82 +20,90 @@ function replaceAt(str, index, ch) { |
|
|
|
return str.substr(0, index) + ch + str.substr(index + 1); |
|
|
|
} |
|
|
|
|
|
|
|
var stack = []; |
|
|
|
|
|
|
|
module.exports = function smartquotes(typographer, blockToken) { |
|
|
|
module.exports = function smartquotes(state) { |
|
|
|
/*eslint max-depth:0*/ |
|
|
|
var i, token, text, t, pos, max, thisLevel, lastSpace, nextSpace, item, canOpen, canClose, j, isSingle, chars, |
|
|
|
var i, token, text, t, pos, max, thisLevel, lastSpace, nextSpace, item, |
|
|
|
canOpen, canClose, j, isSingle, chars, blkIdx, tokens, |
|
|
|
typographer = state.typographer, |
|
|
|
options = typographer.options, |
|
|
|
tokens = blockToken.children; |
|
|
|
stack = []; |
|
|
|
|
|
|
|
stack.length = 0; |
|
|
|
|
|
|
|
for (i = 0; i < tokens.length; i++) { |
|
|
|
token = tokens[i]; |
|
|
|
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) { |
|
|
|
|
|
|
|
if (token.type !== 'text' || QUOTE_TEST_RE.test(token.text)) { continue; } |
|
|
|
if (state.tokens[blkIdx].type !== 'inline') { continue; } |
|
|
|
|
|
|
|
thisLevel = tokens[i].level; |
|
|
|
tokens = state.tokens[blkIdx].children; |
|
|
|
stack.length = 0; |
|
|
|
|
|
|
|
for (j = stack.length - 1; j >= 0; j--) { |
|
|
|
if (stack[j].level <= thisLevel) { break; } |
|
|
|
} |
|
|
|
stack.length = j + 1; |
|
|
|
|
|
|
|
text = token.content; |
|
|
|
pos = 0; |
|
|
|
max = text.length; |
|
|
|
|
|
|
|
/*eslint no-labels:0,block-scoped-var:0*/ |
|
|
|
OUTER: |
|
|
|
while (pos < max) { |
|
|
|
QUOTE_RE.lastIndex = pos; |
|
|
|
t = QUOTE_RE.exec(text); |
|
|
|
if (!t) { break; } |
|
|
|
|
|
|
|
lastSpace = !isLetter(text, t.index - 1); |
|
|
|
pos = t.index + 1; |
|
|
|
isSingle = (t[0] === "'"); |
|
|
|
nextSpace = !isLetter(text, pos); |
|
|
|
|
|
|
|
if (!nextSpace && !lastSpace) { |
|
|
|
// middle of word
|
|
|
|
if (isSingle) { |
|
|
|
token.content = replaceAt(token.content, t.index, APOSTROPHE); |
|
|
|
} |
|
|
|
continue; |
|
|
|
for (i = 0; i < tokens.length; i++) { |
|
|
|
token = tokens[i]; |
|
|
|
|
|
|
|
if (token.type !== 'text' || QUOTE_TEST_RE.test(token.text)) { continue; } |
|
|
|
|
|
|
|
thisLevel = tokens[i].level; |
|
|
|
|
|
|
|
for (j = stack.length - 1; j >= 0; j--) { |
|
|
|
if (stack[j].level <= thisLevel) { break; } |
|
|
|
} |
|
|
|
stack.length = j + 1; |
|
|
|
|
|
|
|
text = token.content; |
|
|
|
pos = 0; |
|
|
|
max = text.length; |
|
|
|
|
|
|
|
/*eslint no-labels:0,block-scoped-var:0*/ |
|
|
|
OUTER: |
|
|
|
while (pos < max) { |
|
|
|
QUOTE_RE.lastIndex = pos; |
|
|
|
t = QUOTE_RE.exec(text); |
|
|
|
if (!t) { break; } |
|
|
|
|
|
|
|
lastSpace = !isLetter(text, t.index - 1); |
|
|
|
pos = t.index + 1; |
|
|
|
isSingle = (t[0] === "'"); |
|
|
|
nextSpace = !isLetter(text, pos); |
|
|
|
|
|
|
|
if (!nextSpace && !lastSpace) { |
|
|
|
// middle of word
|
|
|
|
if (isSingle) { |
|
|
|
token.content = replaceAt(token.content, t.index, APOSTROPHE); |
|
|
|
} |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
canOpen = !nextSpace; |
|
|
|
canClose = !lastSpace; |
|
|
|
canOpen = !nextSpace; |
|
|
|
canClose = !lastSpace; |
|
|
|
|
|
|
|
if (canClose) { |
|
|
|
// this could be a closing quote, rewind the stack to get a match
|
|
|
|
for (j = stack.length - 1; j >= 0; j--) { |
|
|
|
item = stack[j]; |
|
|
|
if (stack[j].level < thisLevel) { break; } |
|
|
|
if (item.single === isSingle && stack[j].level === thisLevel) { |
|
|
|
if (canClose) { |
|
|
|
// this could be a closing quote, rewind the stack to get a match
|
|
|
|
for (j = stack.length - 1; j >= 0; j--) { |
|
|
|
item = stack[j]; |
|
|
|
chars = isSingle ? options.singleQuotes : options.doubleQuotes; |
|
|
|
if (chars) { |
|
|
|
tokens[item.token].content = replaceAt(tokens[item.token].content, item.pos, chars[0]); |
|
|
|
token.content = replaceAt(token.content, t.index, chars[1]); |
|
|
|
if (stack[j].level < thisLevel) { break; } |
|
|
|
if (item.single === isSingle && stack[j].level === thisLevel) { |
|
|
|
item = stack[j]; |
|
|
|
chars = isSingle ? options.singleQuotes : options.doubleQuotes; |
|
|
|
if (chars) { |
|
|
|
tokens[item.token].content = replaceAt(tokens[item.token].content, item.pos, chars[0]); |
|
|
|
token.content = replaceAt(token.content, t.index, chars[1]); |
|
|
|
} |
|
|
|
stack.length = j; |
|
|
|
continue OUTER; |
|
|
|
} |
|
|
|
stack.length = j; |
|
|
|
continue OUTER; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (canOpen) { |
|
|
|
stack.push({ |
|
|
|
token: i, |
|
|
|
pos: t.index, |
|
|
|
single: isSingle, |
|
|
|
level: thisLevel |
|
|
|
}); |
|
|
|
} else if (canClose && isSingle) { |
|
|
|
token.content = replaceAt(token.content, t.index, APOSTROPHE); |
|
|
|
if (canOpen) { |
|
|
|
stack.push({ |
|
|
|
token: i, |
|
|
|
pos: t.index, |
|
|
|
single: isSingle, |
|
|
|
level: thisLevel |
|
|
|
}); |
|
|
|
} else if (canClose && isSingle) { |
|
|
|
token.content = replaceAt(token.content, t.index, APOSTROPHE); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|