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, fromCharCode, fromCodePoint, isMdAsciiPunct, isPunctChar, isSpace, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd
Class Method Details
.process_inlines(tokens, state) ⇒ Object
18 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/motion-markdown-it/rules_core/smartquotes.rb', line 18 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] == "'") # Find previous character, # default to space if it's the beginning of the line # lastChar = 0x20 if t.begin(0) - 1 >= 0 lastChar = text.charCodeAt(t.begin(0) - 1) else (i - 1).downto(0) do |j| break if tokens[j].type == 'softbreak' || tokens[j].type == 'hardbreak' # lastChar defaults to 0x20 next if tokens[j].type != 'text' lastChar = tokens[j].content.charCodeAt(tokens[j].content.length - 1) break end end # Find next character, # default to space if it's the end of the line # nextChar = 0x20 if pos < max nextChar = text.charCodeAt(pos) else (i + 1).upto(tokens.length - 1) do |j| break if tokens[j].type == 'softbreak' || tokens[j].type == 'hardbreak' # nextChar defaults to 0x20 next if tokens[j].type != 'text' nextChar = tokens[j].content.charCodeAt(0) break end end isLastPunctChar = isMdAsciiPunct(lastChar) || isPunctChar(fromCodePoint(lastChar)) isNextPunctChar = isMdAsciiPunct(nextChar) || isPunctChar(fromCodePoint(nextChar)) 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 openQuote = state.md.[:quotes][2] closeQuote = state.md.[:quotes][3] else openQuote = state.md.[:quotes][0] closeQuote = state.md.[:quotes][1] end # replace token.content *before* tokens[item.token].content, # because, if they are pointing at the same token, replaceAt # could mess up indices when quote length != 1 token.content = replaceAt(token.content, t.begin(0), closeQuote) tokens[item[:token]].content = replaceAt(tokens[item[:token]].content, item[:pos], openQuote) pos += closeQuote.length - 1 pos += (openQuote.length - 1) if item[:token] == i text = token.content max = text.length stack = (j < stack.length ? stack.slice(0, j) : stack.fill(nil, stack.length...(j))) # 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
13 14 15 |
# File 'lib/motion-markdown-it/rules_core/smartquotes.rb', line 13 def self.replaceAt(str, index, ch) return str[0, index] + ch + str.slice_to_end(index + 1) end |
.smartquotes(state) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/motion-markdown-it/rules_core/smartquotes.rb', line 182 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 |