Class: MarkdownIt::RulesCore::Smartquotes
- Inherits:
-
Object
- Object
- MarkdownIt::RulesCore::Smartquotes
- Extended by:
- Common::Utils
- Defined in:
- lib/motion-markdown-it/rules_core/smartquotes.rb
Constant Summary collapse
- QUOTE_TEST_RE =
/['"]/
- QUOTE_RE =
/['"]/
- APOSTROPHE =
’
"\u2019"
Constants included from Common::Utils
Common::Utils::DIGITAL_ENTITY_TEST_RE, Common::Utils::ENTITY_RE, Common::Utils::HTML_ESCAPE_REPLACE_RE, Common::Utils::HTML_ESCAPE_TEST_RE, Common::Utils::HTML_REPLACEMENTS, Common::Utils::REGEXP_ESCAPE_RE, Common::Utils::UNESCAPE_ALL_RE, Common::Utils::UNESCAPE_MD_RE, Common::Utils::UNICODE_PUNCT_RE
Class Method Summary collapse
-
.process_inlines(tokens, state) ⇒ Object
——————————————————————————.
-
.replaceAt(str, index, ch) ⇒ Object
——————————————————————————.
-
.smartquotes(state) ⇒ Object
——————————————————————————.
Methods included from Common::Utils
arrayReplaceAt, assign, escapeHtml, escapeRE, fromCodePoint, isMdAsciiPunct, isPunctChar, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd
Class Method Details
.process_inlines(tokens, state) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/motion-markdown-it/rules_core/smartquotes.rb', line 19 def self.process_inlines(tokens, state) stack = [] (0...tokens.length).each do |i| token = tokens[i] thisLevel = tokens[i].level j = stack.length - 1 while j >= 0 break if (stack[j][:level] <= thisLevel) j -= 1 end # stack.length = j + 1 stack = (j < stack.length ? stack.slice(0, j + 1) : stack.fill(nil, stack.length...(j+1))) next if (token.type != 'text') text = token.content pos = 0 max = text.length # OUTER loop while pos < max continue_outer_loop = false t = QUOTE_RE.match(text, pos) break if t.nil? canOpen = true canClose = true pos = t.begin(0) + 1 isSingle = (t[0] == "'") # treat begin/end of the line as a whitespace lastChar = t.begin(0) - 1 >= 0 ? text.charCodeAt(t.begin(0) - 1) : 0x20 nextChar = pos < max ? text.charCodeAt(pos) : 0x20 isLastPunctChar = isMdAsciiPunct(lastChar) || isPunctChar(lastChar.chr(Encoding::UTF_8)) isNextPunctChar = isMdAsciiPunct(nextChar) || isPunctChar(nextChar.chr(Encoding::UTF_8)) isLastWhiteSpace = isWhiteSpace(lastChar) isNextWhiteSpace = isWhiteSpace(nextChar) if (isNextWhiteSpace) canOpen = false elsif (isNextPunctChar) if (!(isLastWhiteSpace || isLastPunctChar)) canOpen = false end end if (isLastWhiteSpace) canClose = false elsif (isLastPunctChar) if (!(isNextWhiteSpace || isNextPunctChar)) canClose = false end end if (nextChar == 0x22 && t[0] == '"') # " if (lastChar >= 0x30 && lastChar <= 0x39) # >= 0 && <= 9 # special case: 1"" - count first quote as an inch canClose = canOpen = false end end if (canOpen && canClose) # treat this as the middle of the word canOpen = false canClose = isNextPunctChar end if (!canOpen && !canClose) # middle of word if (isSingle) token.content = replaceAt(token.content, t.begin(0), APOSTROPHE) end next end if (canClose) # this could be a closing quote, rewind the stack to get a match j = stack.length - 1 while j >= 0 item = stack[j] break if (stack[j][:level] < thisLevel) if (item[:single] == isSingle && stack[j][:level] == thisLevel) item = stack[j] if (isSingle) tokens[item[:token]].content = replaceAt(tokens[item[:token]].content, item[:pos], state.md.[:quotes][2]) token.content = replaceAt(token.content, t.begin(0), state.md.[:quotes][3]) else tokens[item[:token]].content = replaceAt(tokens[item[:token]].content, item[:pos], state.md.[:quotes][0]) token.content = replaceAt(token.content, t.begin(0), state.md.[:quotes][1]) end # stack.length = j stack = (j < stack.length ? stack.slice(0, j) : stack.fill(nil, stack.length...(j))) continue_outer_loop = true # continue OUTER; break end j -= 1 end end next if continue_outer_loop if (canOpen) stack.push({ token: i, pos: t.begin(0), single: isSingle, level: thisLevel }) elsif (canClose && isSingle) token.content = replaceAt(token.content, t.begin(0), APOSTROPHE) end end end end |
.replaceAt(str, index, ch) ⇒ Object
14 15 16 |
# File 'lib/motion-markdown-it/rules_core/smartquotes.rb', line 14 def self.replaceAt(str, index, ch) return str[0, index] + ch + str.slice_to_end(index + 1) end |
.smartquotes(state) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/motion-markdown-it/rules_core/smartquotes.rb', line 141 def self.smartquotes(state) return if (!state.md.[:typographer]) blkIdx = state.tokens.length - 1 while blkIdx >= 0 if (state.tokens[blkIdx].type != 'inline' || !(QUOTE_TEST_RE =~ state.tokens[blkIdx].content)) blkIdx -= 1 next end process_inlines(state.tokens[blkIdx].children, state) blkIdx -= 1 end end |