Class: MarkdownIt::RulesInline::Emphasis
- Inherits:
-
Object
- Object
- MarkdownIt::RulesInline::Emphasis
- Extended by:
- Common::Utils
- Defined in:
- lib/motion-markdown-it/rules_inline/emphasis.rb
Constant Summary
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
-
.emphasis(state, silent) ⇒ Object
——————————————————————————.
-
.scanDelims(state, start) ⇒ Object
parse sequence of emphasis markers, “start” should point at a valid marker ——————————————————————————.
Methods included from Common::Utils
arrayReplaceAt, assign, escapeHtml, escapeRE, fromCodePoint, isMdAsciiPunct, isPunctChar, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd
Class Method Details
.emphasis(state, silent) ⇒ Object
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 |
# File 'lib/motion-markdown-it/rules_inline/emphasis.rb', line 65 def self.emphasis(state, silent) max = state.posMax start = state.pos marker = state.src.charCodeAt(start) return false if (marker != 0x5F && marker != 0x2A) # _ * return false if (silent) # don't run any pairs in validation mode res = scanDelims(state, start) startCount = res[:delims] if (!res[:can_open]) state.pos += startCount # Earlier we checked !silent, but this implementation does not need it state.pending += state.src.slice(start...state.pos) return true end state.pos = start + startCount stack = [ startCount ] while (state.pos < max) if (state.src.charCodeAt(state.pos) == marker) res = scanDelims(state, state.pos) count = res[:delims] if (res[:can_close]) oldCount = stack.pop() newCount = count while (oldCount != newCount) if (newCount < oldCount) stack.push(oldCount - newCount) break end # assert(newCount > oldCount) newCount -= oldCount break if (stack.length == 0) state.pos += oldCount oldCount = stack.pop() end if (stack.length == 0) startCount = oldCount found = true break end state.pos += count next end stack.push(count) if (res[:can_open]) state.pos += count next end state.md.inline.skipToken(state) end if (!found) # parser failed to find ending tag, so it's not valid emphasis state.pos = start return false end # found! state.posMax = state.pos state.pos = start + startCount # Earlier we checked !silent, but this implementation does not need it # we have `startCount` starting and ending markers, # now trying to serialize them into tokens count = startCount while count > 1 token = state.push('strong_open', 'strong', 1) token.markup = marker.chr + marker.chr count -= 2 end if (count % 2 == 1) token = state.push('em_open', 'em', 1) token.markup = marker.chr end state.md.inline.tokenize(state) if (count % 2 == 1) token = state.push('em_close', 'em', -1) token.markup = marker.chr end count = startCount while count > 1 token = state.push('strong_close', 'strong', -1) token.markup = marker.chr + marker.chr count -= 2 end state.pos = state.posMax + startCount state.posMax = max return true end |
.scanDelims(state, start) ⇒ Object
parse sequence of emphasis markers, “start” should point at a valid marker
11 12 13 14 15 16 17 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 |
# File 'lib/motion-markdown-it/rules_inline/emphasis.rb', line 11 def self.scanDelims(state, start) pos = start left_flanking = true right_flanking = true max = state.posMax marker = state.src.charCodeAt(start) # treat beginning of the line as a whitespace lastChar = start > 0 ? state.src.charCodeAt(start - 1) : 0x20 while (pos < max && state.src.charCodeAt(pos) == marker) pos += 1 end count = pos - start # treat end of the line as a whitespace nextChar = pos < max ? state.src.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) left_flanking = false elsif (isNextPunctChar) if (!(isLastWhiteSpace || isLastPunctChar)) left_flanking = false end end if (isLastWhiteSpace) right_flanking = false elsif (isLastPunctChar) if (!(isNextWhiteSpace || isNextPunctChar)) right_flanking = false end end if (marker == 0x5F) # _ # "_" inside a word can neither open nor close an emphasis can_open = left_flanking && (!right_flanking || isLastPunctChar) can_close = right_flanking && (!left_flanking || isNextPunctChar) else can_open = left_flanking can_close = right_flanking end return { can_open: can_open, can_close: can_close, delims: count } end |