Class: MarkdownIt::RulesBlock::Table
- Inherits:
-
Object
- Object
- MarkdownIt::RulesBlock::Table
- Extended by:
- Common::Utils
- Defined in:
- lib/motion-markdown-it/rules_block/table.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
-
.escapedSplit(str) ⇒ Object
——————————————————————————.
-
.getLine(state, line) ⇒ Object
——————————————————————————.
-
.table(state, startLine, endLine, silent) ⇒ Object
——————————————————————————.
Methods included from Common::Utils
arrayReplaceAt, assign, charCodeAt, escapeHtml, escapeRE, fromCharCode, fromCodePoint, isMdAsciiPunct, isPunctChar, isSpace, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd
Class Method Details
.escapedSplit(str) ⇒ Object
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 |
# File 'lib/motion-markdown-it/rules_block/table.rb', line 17 def self.escapedSplit(str) result = [] pos = 0 max = str.length isEscaped = false lastPos = 0 current = '' ch = charCodeAt(str, pos) while (pos < max) if ch == 0x7c # | if (!isEscaped) # pipe separating cells, '|' result.push(current + str[lastPos...pos]) current = '' lastPos = pos + 1 else # escaped pipe, '\|' current += str[lastPos...(pos - 1)] lastPos = pos end end isEscaped = (ch == 0x5c) # '\' pos += 1 ch = charCodeAt(str, pos) end result.push(current + str[lastPos..-1]) return result end |
.getLine(state, line) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/motion-markdown-it/rules_block/table.rb', line 9 def self.getLine(state, line) pos = state.bMarks[line] + state.tShift[line] max = state.eMarks[line] return state.src[pos, max - pos] end |
.table(state, startLine, endLine, silent) ⇒ Object
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/motion-markdown-it/rules_block/table.rb', line 53 def self.table(state, startLine, endLine, silent) # should have at least two lines return false if (startLine + 2 > endLine) nextLine = startLine + 1 return false if (state.sCount[nextLine] < state.blkIndent) # if it's indented more than 3 spaces, it should be a code block return false if state.sCount[nextLine] - state.blkIndent >= 4 # first character of the second line should be '|', '-', ':', # and no other characters are allowed but spaces; # basically, this is the equivalent of /^[-:|][-:|\s]*$/ regexp pos = state.bMarks[nextLine] + state.tShift[nextLine] return false if (pos >= state.eMarks[nextLine]) firstCh = charCodeAt(state.src, pos) pos += 1 return false if (firstCh != 0x7C && firstCh != 0x2D && firstCh != 0x3A) # | or - or : return false if (pos >= state.eMarks[nextLine]) secondCh = charCodeAt(state.src, pos) pos += 1 return false if (secondCh != 0x7C && secondCh != 0x2D && secondCh != 0x3A && !isSpace(secondCh)) # | or - or : # if first character is '-', then second character must not be a space # (due to parsing ambiguity with list) return false if (firstCh === 0x2D && isSpace(secondCh)) # - while pos < state.eMarks[nextLine] ch = charCodeAt(state.src, pos) return false if (ch != 0x7C && ch != 0x2D && ch != 0x3A && !isSpace(ch)) # | or - or : pos += 1 end lineText = getLine(state, startLine + 1) columns = lineText.split('|') aligns = [] (0...columns.length).each do |i| t = columns[i].strip if t.empty? # allow empty columns before and after table, but not in between columns # e.g. allow ` |---| `, disallow ` ---||--- ` if (i == 0 || i == columns.length - 1) next else return false end end return false if (/^:?-+:?$/ =~ t).nil? if (charCodeAt(t, t.length - 1) == 0x3A) # ':' aligns.push(charCodeAt(t, 0) == 0x3A ? 'center' : 'right') elsif (charCodeAt(t, 0) == 0x3A) aligns.push('left') else aligns.push('') end end lineText = getLine(state, startLine).strip return false if !lineText.include?('|') return false if state.sCount[startLine] - state.blkIndent >= 4 columns = self.escapedSplit(lineText) columns.shift if (columns.length && columns[0] == '') columns.pop if (columns.length && columns[columns.length - 1] == '') # header row will define an amount of columns in the entire table, # and align row should be exactly the same (the rest of the rows can differ) columnCount = columns.length return false if columnCount == 0 || columnCount != aligns.length return true if silent oldParentType = state.parentType state.parentType = 'table' # use 'blockquote' lists for termination because it's # the most similar to tables terminatorRules = state.md.block.ruler.getRules('blockquote') token = state.push('table_open', 'table', 1) token.map = tableLines = [ startLine, 0 ] token = state.push('thead_open', 'thead', 1) token.map = [ startLine, startLine + 1 ] token = state.push('tr_open', 'tr', 1) token.map = [ startLine, startLine + 1 ] (0...columns.length).each do |i| token = state.push('th_open', 'th', 1) unless aligns[i].empty? token.attrs = [ [ 'style', 'text-align:' + aligns[i] ] ] end token = state.push('inline', '', 0) token.content = columns[i].strip token.children = [] token = state.push('th_close', 'th', -1) end token = state.push('tr_close', 'tr', -1) token = state.push('thead_close', 'thead', -1) nextLine = startLine + 2 while nextLine < endLine break if (state.sCount[nextLine] < state.blkIndent) terminate = false (0...terminatorRules.length).each do |i| if (terminatorRules[i].call(state, nextLine, endLine, true)) terminate = true break end end break if (terminate) lineText = getLine(state, nextLine).strip break if lineText.empty? break if state.sCount[nextLine] - state.blkIndent >= 4 columns = self.escapedSplit(lineText) columns.shift if (columns.length && columns[0] == '') columns.pop if (columns.length && columns[columns.length - 1] == '') if (nextLine == startLine + 2) token = state.push('tbody_open', 'tbody', 1) token.map = tbodyLines = [ startLine + 2, 0 ] end token = state.push('tr_open', 'tr', 1) token.map = [ nextLine, nextLine + 1 ] (0...columnCount).each do |i| token = state.push('td_open', 'td', 1) token.map = [ nextLine, nextLine + 1 ] unless aligns[i].empty? token.attrs = [ [ 'style', 'text-align:' + aligns[i] ] ] end token = state.push('inline', '', 0) token.content = columns[i] ? columns[i].strip : '' token.children = [] token = state.push('td_close', 'td', -1) end token = state.push('tr_close', 'tr', -1) nextLine += 1 end if (tbodyLines) token = state.push('tbody_close', 'tbody', -1) tbodyLines[1] = nextLine end token = state.push('table_close', 'table', -1) tableLines[1] = nextLine state.parentType = oldParentType state.line = nextLine return true end |