|
| 1 | +"""Material Definition Lists. |
| 2 | +
|
| 3 | +Based on |
| 4 | +[mdformat-deflist](https://github.com/executablebooks/mdformat-deflist/blob/bbcf9ed4f80847db58b6f932ed95e2c7a6c49ae5/mdformat_deflist/plugin.py), |
| 5 | +but modified for mkdocs-material conventions. |
| 6 | +
|
| 7 | +Example: |
| 8 | +```md |
| 9 | +`Lorem ipsum dolor sit amet` |
| 10 | +
|
| 11 | +: Sed sagittis eleifend rutrum. Donec vitae suscipit est. Nullam tempus |
| 12 | + tellus non sem sollicitudin, quis rutrum leo facilisis. |
| 13 | +
|
| 14 | +`Cras arcu libero` |
| 15 | +
|
| 16 | +: Aliquam metus eros, pretium sed nulla venenatis, faucibus auctor ex. Proin |
| 17 | + ut eros sed sapien ullamcorper consequat. Nunc ligula ante. |
| 18 | +
|
| 19 | + Duis mollis est eget nibh volutpat, fermentum aliquet dui mollis. |
| 20 | + Name vulputate tincidunt fringilla. |
| 21 | + Nullam dignissim ultrices urna non auctor. |
| 22 | +``` |
| 23 | +
|
| 24 | +Docs: |
| 25 | +<https://squidfunk.github.io/mkdocs-material/reference/lists/#using-definition-lists> |
| 26 | +
|
| 27 | +""" |
| 28 | + |
| 29 | +from __future__ import annotations |
| 30 | + |
| 31 | +from mdformat.renderer import RenderContext, RenderTreeNode |
| 32 | +from mdformat.renderer.typing import Render |
| 33 | + |
| 34 | + |
| 35 | +def make_render_children(separator: str) -> Render: |
| 36 | + """Create a renderer that joins child nodes with a separator.""" |
| 37 | + |
| 38 | + def render_children( |
| 39 | + node: RenderTreeNode, |
| 40 | + context: RenderContext, |
| 41 | + ) -> str: |
| 42 | + return separator.join(child.render(context) for child in node.children) |
| 43 | + |
| 44 | + return render_children |
| 45 | + |
| 46 | + |
| 47 | +def render_material_definition_list() -> str: |
| 48 | + """Render Material Definition List.""" |
| 49 | + return make_render_children("\n") |
| 50 | + |
| 51 | + |
| 52 | +def render_material_definition_term() -> str: |
| 53 | + """Render Material Definition Term.""" |
| 54 | + return make_render_children("\n") |
| 55 | + |
| 56 | + |
| 57 | +def render_material_definition_body( |
| 58 | + node: RenderTreeNode, |
| 59 | + context: RenderContext, |
| 60 | +) -> str: |
| 61 | + """Render the definition body.""" |
| 62 | + tight_list = all( |
| 63 | + child.type != "paragraph" or child.hidden for child in node.children |
| 64 | + ) |
| 65 | + marker = ": " # FYI: increased for material |
| 66 | + indent_width = len(marker) |
| 67 | + context.env["indent_width"] += indent_width |
| 68 | + try: |
| 69 | + text = make_render_children("\n\n")(node, context) |
| 70 | + lines = text.splitlines() |
| 71 | + if not lines: |
| 72 | + return ":" |
| 73 | + indented_lines = [f"{marker}{lines[0]}"] + [ |
| 74 | + f"{' ' * indent_width}{line}" if line else "" for line in lines[1:] |
| 75 | + ] |
| 76 | + indented_lines = ("" if tight_list else "\n") + "\n".join(indented_lines) |
| 77 | + next_sibling = node.next_sibling |
| 78 | + return indented_lines + ( |
| 79 | + "\n" if (next_sibling and next_sibling.type == "dt") else "" |
| 80 | + ) |
| 81 | + finally: |
| 82 | + context.env["indent_width"] -= indent_width |
| 83 | + |
| 84 | + |
| 85 | +def escape_deflist( |
| 86 | + text: str, |
| 87 | + node: RenderTreeNode, # noqa: ARG001 |
| 88 | + context: RenderContext, # noqa: ARG001 |
| 89 | +) -> str: |
| 90 | + """Escape line starting ":" which would otherwise be parsed as a definition list.""" |
| 91 | + return "\n".join( |
| 92 | + "\\" + line if line.startswith(":") else line for line in text.split("\n") |
| 93 | + ) |
0 commit comments