|
| 1 | +"""Python-Markdown Extensions Captions. |
| 2 | +
|
| 3 | +Matches: |
| 4 | +
|
| 5 | +```md |
| 6 | +/// caption |
| 7 | +Default values for config variables. |
| 8 | +/// |
| 9 | +``` |
| 10 | +
|
| 11 | +Docs: |
| 12 | +https://github.com/facelessuser/pymdown-extensions/blob/main/pymdownx/blocks/caption.py |
| 13 | +
|
| 14 | +
|
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import re |
| 20 | +from typing import TYPE_CHECKING |
| 21 | + |
| 22 | +from mdit_py_plugins.utils import is_code_block |
| 23 | + |
| 24 | +from mdformat_mkdocs._synced.admon_factories._whitespace_admon_factories import ( |
| 25 | + new_token, |
| 26 | +) |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + from markdown_it import MarkdownIt |
| 30 | + from markdown_it.rules_block import StateBlock |
| 31 | + |
| 32 | +_CAPTION_START_PATTERN = re.compile( |
| 33 | + r"\s*///\s*(?P<type>figure-|table-|)caption\s*(\|\s*(?P<number>[\d\.]+))?", |
| 34 | +) |
| 35 | +_CAPTION_END_PATTERN = re.compile(r"^\s*///\s*$") |
| 36 | +_CAPTION_ATTRS_PATTERN = re.compile(r"^s*(?P<attrs>attrs:\s*\{[^}]*\})\s*$") |
| 37 | +PYMD_CAPTIONS_PREFIX = "mkdocs_caption" |
| 38 | + |
| 39 | + |
| 40 | +def _src_in_line(state: StateBlock, line: int) -> tuple[str, int, int]: |
| 41 | + """Get the source in a given line number.""" |
| 42 | + start_pos = state.bMarks[line] + state.tShift[line] |
| 43 | + end_pos = state.eMarks[line] |
| 44 | + return state.src[start_pos:end_pos], start_pos, end_pos |
| 45 | + |
| 46 | + |
| 47 | +def _parse( |
| 48 | + state: StateBlock, |
| 49 | + first_line_max_pos: int, |
| 50 | + start_line: int, |
| 51 | + end_line: int, |
| 52 | +) -> tuple[int, str, str | None]: |
| 53 | + """Parse a caption block: optionally read attrs and extract content.""" |
| 54 | + end_match = None |
| 55 | + max_line = start_line + 1 |
| 56 | + end_pos = -1 |
| 57 | + attrs_text, _, attrs_max_pos = _src_in_line(state, max_line) |
| 58 | + caption_attrs_match = _CAPTION_ATTRS_PATTERN.match(attrs_text) |
| 59 | + content_start_pos = ( |
| 60 | + first_line_max_pos + 1 if caption_attrs_match is None else attrs_max_pos + 1 |
| 61 | + ) |
| 62 | + attrs = ( |
| 63 | + caption_attrs_match.group("attrs") if caption_attrs_match is not None else None |
| 64 | + ) |
| 65 | + if not isinstance(attrs, str): |
| 66 | + attrs = None |
| 67 | + |
| 68 | + while end_match is None and max_line <= end_line: |
| 69 | + line_text, end_pos, _ = _src_in_line(state, max_line) |
| 70 | + if _CAPTION_END_PATTERN.match(line_text) is None: |
| 71 | + max_line += 1 |
| 72 | + else: |
| 73 | + end_match = max_line |
| 74 | + |
| 75 | + return max_line, state.src[content_start_pos:end_pos], attrs |
| 76 | + |
| 77 | + |
| 78 | +def _material_captions( |
| 79 | + state: StateBlock, |
| 80 | + start_line: int, |
| 81 | + end_line: int, |
| 82 | + silent: bool, |
| 83 | +) -> bool: |
| 84 | + """Detect caption blocks and wrap them in a token.""" |
| 85 | + if is_code_block(state, start_line): |
| 86 | + return False |
| 87 | + |
| 88 | + first_line_text, _, first_line_max_pos = _src_in_line(state, start_line) |
| 89 | + start_match = _CAPTION_START_PATTERN.match(first_line_text) |
| 90 | + if start_match is None: |
| 91 | + return False |
| 92 | + |
| 93 | + if silent: |
| 94 | + return True |
| 95 | + |
| 96 | + max_line, content, attrs = _parse(state, first_line_max_pos, start_line, end_line) |
| 97 | + |
| 98 | + with ( |
| 99 | + new_token(state, PYMD_CAPTIONS_PREFIX, "figcaption") as token, |
| 100 | + new_token(state, "", "p"), |
| 101 | + ): |
| 102 | + token.info = start_match.group("type") + "caption" |
| 103 | + token.meta = {"number": start_match.group("number")} |
| 104 | + if attrs is not None: |
| 105 | + token.meta["attrs"] = attrs |
| 106 | + tkn_inline = state.push("inline", "", 0) |
| 107 | + tkn_inline.content = content.strip() |
| 108 | + tkn_inline.map = [start_line, max_line] |
| 109 | + tkn_inline.children = [] |
| 110 | + |
| 111 | + state.line = max_line + 1 |
| 112 | + |
| 113 | + return True |
| 114 | + |
| 115 | + |
| 116 | +def pymd_captions_plugin(md: MarkdownIt) -> None: |
| 117 | + md.block.ruler.before( |
| 118 | + "fence", |
| 119 | + PYMD_CAPTIONS_PREFIX, |
| 120 | + _material_captions, |
| 121 | + {"alt": ["paragraph"]}, |
| 122 | + ) |
0 commit comments