|
| 1 | +"""Process definition lists.""" |
| 2 | +from markdown_it import MarkdownIt |
| 3 | +from markdown_it.common.utils import charCodeAt |
| 4 | +from markdown_it.rules_block import StateBlock |
| 5 | + |
| 6 | + |
| 7 | +def deflist_plugin(md: MarkdownIt): |
| 8 | + isSpace = md.utils.isSpace |
| 9 | + |
| 10 | + def skipMarker(state: StateBlock, line: int): |
| 11 | + """Search `[:~][\n ]`, returns next pos after marker on success or -1 on fail.""" |
| 12 | + start = state.bMarks[line] + state.tShift[line] |
| 13 | + maximum = state.eMarks[line] |
| 14 | + |
| 15 | + if start >= maximum: |
| 16 | + return -1 |
| 17 | + |
| 18 | + # Check bullet |
| 19 | + marker = charCodeAt(state.src, start) |
| 20 | + start += 1 |
| 21 | + if marker != 0x7E and marker != 0x3A: # ~ : |
| 22 | + return -1 |
| 23 | + |
| 24 | + pos = state.skipSpaces(start) |
| 25 | + |
| 26 | + # require space after ":" |
| 27 | + if start == pos: |
| 28 | + return -1 |
| 29 | + |
| 30 | + # no empty definitions, e.g. " : " |
| 31 | + if pos >= maximum: |
| 32 | + return -1 |
| 33 | + |
| 34 | + return start |
| 35 | + |
| 36 | + def markTightParagraphs(state: StateBlock, idx: int): |
| 37 | + |
| 38 | + level = state.level + 2 |
| 39 | + |
| 40 | + i = idx + 2 |
| 41 | + l2 = len(state.tokens) - 2 |
| 42 | + while i < l2: |
| 43 | + if ( |
| 44 | + state.tokens[i].level == level |
| 45 | + and state.tokens[i].type == "paragraph_open" |
| 46 | + ): |
| 47 | + state.tokens[i + 2].hidden = True |
| 48 | + state.tokens[i].hidden = True |
| 49 | + i += 2 |
| 50 | + i += 1 |
| 51 | + |
| 52 | + def deflist(state: StateBlock, startLine: int, endLine: int, silent: bool): |
| 53 | + |
| 54 | + if silent: |
| 55 | + # quirk: validation mode validates a dd block only, not a whole deflist |
| 56 | + if state.ddIndent < 0: |
| 57 | + return False |
| 58 | + return skipMarker(state, startLine) >= 0 |
| 59 | + |
| 60 | + nextLine = startLine + 1 |
| 61 | + if nextLine >= endLine: |
| 62 | + return False |
| 63 | + |
| 64 | + if state.isEmpty(nextLine): |
| 65 | + nextLine += 1 |
| 66 | + if nextLine >= endLine: |
| 67 | + return False |
| 68 | + |
| 69 | + if state.sCount[nextLine] < state.blkIndent: |
| 70 | + return False |
| 71 | + contentStart = skipMarker(state, nextLine) |
| 72 | + if contentStart < 0: |
| 73 | + return False |
| 74 | + |
| 75 | + # Start list |
| 76 | + listTokIdx = len(state.tokens) |
| 77 | + tight = True |
| 78 | + |
| 79 | + token = state.push("dl_open", "dl", 1) |
| 80 | + token.map = listLines = [startLine, 0] |
| 81 | + |
| 82 | + # Iterate list items |
| 83 | + dtLine = startLine |
| 84 | + ddLine = nextLine |
| 85 | + |
| 86 | + # One definition list can contain multiple DTs, |
| 87 | + # and one DT can be followed by multiple DDs. |
| 88 | + # |
| 89 | + # Thus, there is two loops here, and label is |
| 90 | + # needed to break out of the second one |
| 91 | + # |
| 92 | + break_outer = False |
| 93 | + |
| 94 | + while True: |
| 95 | + prevEmptyEnd = False |
| 96 | + |
| 97 | + token = state.push("dt_open", "dt", 1) |
| 98 | + token.map = [dtLine, dtLine] |
| 99 | + |
| 100 | + token = state.push("inline", "", 0) |
| 101 | + token.map = [dtLine, dtLine] |
| 102 | + token.content = state.getLines( |
| 103 | + dtLine, dtLine + 1, state.blkIndent, False |
| 104 | + ).strip() |
| 105 | + token.children = [] |
| 106 | + |
| 107 | + token = state.push("dt_close", "dt", -1) |
| 108 | + |
| 109 | + while True: |
| 110 | + token = state.push("dd_open", "dd", 1) |
| 111 | + token.map = itemLines = [nextLine, 0] |
| 112 | + |
| 113 | + pos = contentStart |
| 114 | + maximum = state.eMarks[ddLine] |
| 115 | + offset = ( |
| 116 | + state.sCount[ddLine] |
| 117 | + + contentStart |
| 118 | + - (state.bMarks[ddLine] + state.tShift[ddLine]) |
| 119 | + ) |
| 120 | + |
| 121 | + while pos < maximum: |
| 122 | + ch = charCodeAt(state.src, pos) |
| 123 | + |
| 124 | + if isSpace(ch): |
| 125 | + if ch == 0x09: |
| 126 | + offset += 4 - offset % 4 |
| 127 | + else: |
| 128 | + offset += 1 |
| 129 | + else: |
| 130 | + break |
| 131 | + |
| 132 | + pos += 1 |
| 133 | + |
| 134 | + contentStart = pos |
| 135 | + |
| 136 | + oldTight = state.tight |
| 137 | + oldDDIndent = state.ddIndent |
| 138 | + oldIndent = state.blkIndent |
| 139 | + oldTShift = state.tShift[ddLine] |
| 140 | + oldSCount = state.sCount[ddLine] |
| 141 | + oldParentType = state.parentType |
| 142 | + state.blkIndent = state.ddIndent = state.sCount[ddLine] + 2 |
| 143 | + state.tShift[ddLine] = contentStart - state.bMarks[ddLine] |
| 144 | + state.sCount[ddLine] = offset |
| 145 | + state.tight = True |
| 146 | + state.parentType = "deflist" |
| 147 | + |
| 148 | + state.md.block.tokenize(state, ddLine, endLine, True) |
| 149 | + |
| 150 | + # If any of list item is tight, mark list as tight |
| 151 | + if not state.tight or prevEmptyEnd: |
| 152 | + tight = False |
| 153 | + |
| 154 | + # Item become loose if finish with empty line, |
| 155 | + # but we should filter last element, because it means list finish |
| 156 | + prevEmptyEnd = (state.line - ddLine) > 1 and state.isEmpty( |
| 157 | + state.line - 1 |
| 158 | + ) |
| 159 | + |
| 160 | + state.tShift[ddLine] = oldTShift |
| 161 | + state.sCount[ddLine] = oldSCount |
| 162 | + state.tight = oldTight |
| 163 | + state.parentType = oldParentType |
| 164 | + state.blkIndent = oldIndent |
| 165 | + state.ddIndent = oldDDIndent |
| 166 | + |
| 167 | + token = state.push("dd_close", "dd", -1) |
| 168 | + |
| 169 | + itemLines[1] = nextLine = state.line |
| 170 | + |
| 171 | + if nextLine >= endLine: |
| 172 | + break_outer = True |
| 173 | + break |
| 174 | + |
| 175 | + if state.sCount[nextLine] < state.blkIndent: |
| 176 | + break_outer = True |
| 177 | + break |
| 178 | + |
| 179 | + contentStart = skipMarker(state, nextLine) |
| 180 | + if contentStart < 0: |
| 181 | + break |
| 182 | + |
| 183 | + ddLine = nextLine |
| 184 | + |
| 185 | + # go to the next loop iteration: |
| 186 | + # insert DD tag and repeat checking |
| 187 | + |
| 188 | + if break_outer: |
| 189 | + break_outer = False |
| 190 | + break |
| 191 | + |
| 192 | + if nextLine >= endLine: |
| 193 | + break |
| 194 | + dtLine = nextLine |
| 195 | + |
| 196 | + if state.isEmpty(dtLine): |
| 197 | + break |
| 198 | + if state.sCount[dtLine] < state.blkIndent: |
| 199 | + break |
| 200 | + |
| 201 | + ddLine = dtLine + 1 |
| 202 | + if ddLine >= endLine: |
| 203 | + break |
| 204 | + if state.isEmpty(ddLine): |
| 205 | + ddLine += 1 |
| 206 | + if ddLine >= endLine: |
| 207 | + break |
| 208 | + |
| 209 | + if state.sCount[ddLine] < state.blkIndent: |
| 210 | + break |
| 211 | + contentStart = skipMarker(state, ddLine) |
| 212 | + if contentStart < 0: |
| 213 | + break |
| 214 | + |
| 215 | + # go to the next loop iteration: |
| 216 | + # insert DT and DD tags and repeat checking |
| 217 | + |
| 218 | + # Finalise list |
| 219 | + token = state.push("dl_close", "dl", -1) |
| 220 | + |
| 221 | + listLines[1] = nextLine |
| 222 | + |
| 223 | + state.line = nextLine |
| 224 | + |
| 225 | + # mark paragraphs tight if needed |
| 226 | + if tight: |
| 227 | + markTightParagraphs(state, listTokIdx) |
| 228 | + |
| 229 | + return True |
| 230 | + |
| 231 | + md.block.ruler.before( |
| 232 | + "paragraph", "deflist", deflist, {"alt": ["paragraph", "reference"]} |
| 233 | + ) |
0 commit comments