|
| 1 | +"""Process block-level custom containers.""" |
| 2 | +from math import floor |
| 3 | + |
| 4 | +from markdown_it import MarkdownIt |
| 5 | +from markdown_it.common.utils import charCodeAt |
| 6 | +from markdown_it.rules_block import StateBlock |
| 7 | + |
| 8 | + |
| 9 | +def container_plugin(md: MarkdownIt, name, **options): |
| 10 | + """Second param may be useful, |
| 11 | + if you decide to increase minimal allowed marker length |
| 12 | + """ |
| 13 | + |
| 14 | + def validateDefault(params: str, *args): |
| 15 | + return params.strip().split(" ", 2)[0] == name |
| 16 | + |
| 17 | + def renderDefault(self, tokens, idx, _options, env): |
| 18 | + # add a class to the opening tag |
| 19 | + if tokens[idx].nesting == 1: |
| 20 | + tokens[idx].attrJoin("class", name) |
| 21 | + |
| 22 | + return self.renderToken(tokens, idx, _options, env) |
| 23 | + |
| 24 | + min_markers = 3 |
| 25 | + marker_str = options.get("marker", ":") |
| 26 | + marker_char = charCodeAt(marker_str, 0) |
| 27 | + marker_len = len(marker_str) |
| 28 | + validate = options.get("validate", validateDefault) |
| 29 | + render = options.get("render", renderDefault) |
| 30 | + |
| 31 | + def container_func(state: StateBlock, startLine: int, endLine: int, silent: bool): |
| 32 | + |
| 33 | + auto_closed = False |
| 34 | + start = state.bMarks[startLine] + state.tShift[startLine] |
| 35 | + maximum = state.eMarks[startLine] |
| 36 | + |
| 37 | + # Check out the first character quickly, |
| 38 | + # this should filter out most of non-containers |
| 39 | + if marker_char != charCodeAt(state.src, start): |
| 40 | + return False |
| 41 | + |
| 42 | + # Check out the rest of the marker string |
| 43 | + pos = start + 1 |
| 44 | + while pos <= maximum: |
| 45 | + if marker_str[(pos - start) % marker_len] != state.src[pos]: |
| 46 | + break |
| 47 | + pos += 1 |
| 48 | + |
| 49 | + marker_count = floor((pos - start) / marker_len) |
| 50 | + if marker_count < min_markers: |
| 51 | + return False |
| 52 | + pos -= (pos - start) % marker_len |
| 53 | + |
| 54 | + markup = state.src[start:pos] |
| 55 | + params = state.src[pos:maximum] |
| 56 | + if not validate(params, markup): |
| 57 | + return False |
| 58 | + |
| 59 | + # Since start is found, we can report success here in validation mode |
| 60 | + if silent: |
| 61 | + return True |
| 62 | + |
| 63 | + # Search for the end of the block |
| 64 | + nextLine = startLine |
| 65 | + |
| 66 | + while True: |
| 67 | + nextLine += 1 |
| 68 | + if nextLine >= endLine: |
| 69 | + # unclosed block should be autoclosed by end of document. |
| 70 | + # also block seems to be autoclosed by end of parent |
| 71 | + break |
| 72 | + |
| 73 | + start = state.bMarks[nextLine] + state.tShift[nextLine] |
| 74 | + maximum = state.eMarks[nextLine] |
| 75 | + |
| 76 | + if start < maximum and state.sCount[nextLine] < state.blkIndent: |
| 77 | + # non-empty line with negative indent should stop the list: |
| 78 | + # - ``` |
| 79 | + # test |
| 80 | + break |
| 81 | + |
| 82 | + if marker_char != charCodeAt(state.src, start): |
| 83 | + continue |
| 84 | + |
| 85 | + if state.sCount[nextLine] - state.blkIndent >= 4: |
| 86 | + # closing fence should be indented less than 4 spaces |
| 87 | + continue |
| 88 | + |
| 89 | + pos = start + 1 |
| 90 | + while pos <= maximum: |
| 91 | + if marker_str[(pos - start) % marker_len] != state.src[pos]: |
| 92 | + break |
| 93 | + pos += 1 |
| 94 | + |
| 95 | + # closing code fence must be at least as long as the opening one |
| 96 | + if floor((pos - start) / marker_len) < marker_count: |
| 97 | + continue |
| 98 | + |
| 99 | + # make sure tail has spaces only |
| 100 | + pos -= (pos - start) % marker_len |
| 101 | + pos = state.skipSpaces(pos) |
| 102 | + |
| 103 | + if pos < maximum: |
| 104 | + continue |
| 105 | + |
| 106 | + # found! |
| 107 | + auto_closed = True |
| 108 | + break |
| 109 | + |
| 110 | + old_parent = state.parentType |
| 111 | + old_line_max = state.lineMax |
| 112 | + state.parentType = "container" |
| 113 | + |
| 114 | + # this will prevent lazy continuations from ever going past our end marker |
| 115 | + state.lineMax = nextLine |
| 116 | + |
| 117 | + token = state.push(f"container_{name}_open", "div", 1) |
| 118 | + token.markup = markup |
| 119 | + token.block = True |
| 120 | + token.info = params |
| 121 | + token.map = [startLine, nextLine] |
| 122 | + |
| 123 | + state.md.block.tokenize(state, startLine + 1, nextLine) |
| 124 | + |
| 125 | + token = state.push(f"container_{name}_close", "div", -1) |
| 126 | + token.markup = state.src[start:pos] |
| 127 | + token.block = True |
| 128 | + |
| 129 | + state.parentType = old_parent |
| 130 | + state.lineMax = old_line_max |
| 131 | + state.line = nextLine + (1 if auto_closed else 0) |
| 132 | + |
| 133 | + return True |
| 134 | + |
| 135 | + md.block.ruler.before( |
| 136 | + "fence", |
| 137 | + "container_" + name, |
| 138 | + container_func, |
| 139 | + {"alt": ["paragraph", "reference", "blockquote", "list"]}, |
| 140 | + ) |
| 141 | + md.add_render_rule(f"container_{name}_open", render) |
| 142 | + md.add_render_rule(f"container_{name}_close", render) |
0 commit comments