|
| 1 | +"""Python-Markdown Snippets. |
| 2 | +
|
| 3 | +WARNING: matches only the "scissors" portion, leaving the rest unparsed |
| 4 | +
|
| 5 | +```md |
| 6 | +--8<-- ... |
| 7 | +``` |
| 8 | +
|
| 9 | +Docs: <https://facelessuser.github.io/pymd-extensions/extensions/snippets> |
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import re |
| 16 | + |
| 17 | +from markdown_it import MarkdownIt |
| 18 | +from markdown_it.rules_block import StateBlock |
| 19 | +from mdit_py_plugins.utils import is_code_block |
| 20 | + |
| 21 | +from mdformat_mkdocs._synced.admon_factories import new_token |
| 22 | + |
| 23 | +_SNIPPET_MARKER = "--8<--" |
| 24 | +_ABBREVIATION_PATTERN = re.compile(rf"^{_SNIPPET_MARKER}(?P<label>.*)") |
| 25 | + |
| 26 | +PYMD_SNIPPET_PREFIX = "pymd_snippet" |
| 27 | + |
| 28 | + |
| 29 | +def _content(state: StateBlock, start_line: int) -> str: |
| 30 | + """Content.""" |
| 31 | + start = state.bMarks[start_line] + state.tShift[start_line] |
| 32 | + maximum = state.eMarks[start_line] |
| 33 | + return state.src[start:maximum] |
| 34 | + |
| 35 | + |
| 36 | +def _parse( |
| 37 | + state: StateBlock, |
| 38 | + match: re.Match[str], |
| 39 | + start_line: int, |
| 40 | + end_line: int, |
| 41 | +) -> tuple[int, str]: |
| 42 | + """Return the max line and matched content.""" |
| 43 | + max_line = start_line + 1 |
| 44 | + inline = f"{_SNIPPET_MARKER}{match['label']}" |
| 45 | + |
| 46 | + if not match["label"]: |
| 47 | + max_search = 20 # Upper limit of lines to search for multi-line snippet |
| 48 | + current_line = start_line + 1 |
| 49 | + inner: list[str] = [] |
| 50 | + while (current_line - start_line) < max_search: |
| 51 | + if max_line == end_line: |
| 52 | + break # no 'label' |
| 53 | + line = _content(state, current_line) |
| 54 | + if _ABBREVIATION_PATTERN.match(line): |
| 55 | + max_line = current_line + 1 |
| 56 | + inline = "\n".join([_SNIPPET_MARKER, *inner, _SNIPPET_MARKER]) |
| 57 | + break |
| 58 | + if line: |
| 59 | + inner.append(line) |
| 60 | + current_line += 1 |
| 61 | + |
| 62 | + return max_line, inline |
| 63 | + |
| 64 | + |
| 65 | +def _pymd_snippet( |
| 66 | + state: StateBlock, |
| 67 | + start_line: int, |
| 68 | + end_line: int, |
| 69 | + silent: bool, |
| 70 | +) -> bool: |
| 71 | + if is_code_block(state, start_line): |
| 72 | + return False |
| 73 | + |
| 74 | + match = _ABBREVIATION_PATTERN.match(_content(state, start_line)) |
| 75 | + if match is None: |
| 76 | + return False |
| 77 | + |
| 78 | + if silent: |
| 79 | + return True |
| 80 | + |
| 81 | + max_line, inline = _parse(state, match, start_line, end_line) |
| 82 | + |
| 83 | + with new_token(state, PYMD_SNIPPET_PREFIX, "p"): |
| 84 | + tkn_inline = state.push("inline", "", 0) |
| 85 | + tkn_inline.content = inline |
| 86 | + tkn_inline.map = [start_line, max_line] |
| 87 | + tkn_inline.children = [] |
| 88 | + |
| 89 | + state.line = max_line |
| 90 | + |
| 91 | + return True |
| 92 | + |
| 93 | + |
| 94 | +def pymd_snippet_plugin(md: MarkdownIt) -> None: |
| 95 | + md.block.ruler.before( |
| 96 | + "reference", |
| 97 | + PYMD_SNIPPET_PREFIX, |
| 98 | + _pymd_snippet, |
| 99 | + {"alt": ["paragraph"]}, |
| 100 | + ) |
0 commit comments