File: rendering.py

package info (click to toggle)
python-mkdocs 1.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,812 kB
  • sloc: python: 14,346; javascript: 10,535; perl: 143; sh: 57; makefile: 30; xml: 11
file content (104 lines) | stat: -rw-r--r-- 3,504 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from __future__ import annotations

import copy
from typing import TYPE_CHECKING, Callable

import markdown
import markdown.treeprocessors

if TYPE_CHECKING:
    from xml.etree import ElementTree as etree

# TODO: This will become unnecessary after min-versions have Markdown >=3.4
_unescape: Callable[[str], str]
try:
    _unescape = markdown.treeprocessors.UnescapeTreeprocessor().unescape
except AttributeError:
    _unescape = lambda s: s

# TODO: Most of this file will become unnecessary after https://github.com/Python-Markdown/markdown/pull/1441


def get_heading_text(el: etree.Element, md: markdown.Markdown) -> str:
    el = copy.deepcopy(el)
    _remove_anchorlink(el)
    _remove_fnrefs(el)
    _extract_alt_texts(el)
    return _strip_tags(_render_inner_html(el, md))


def _strip_tags(text: str) -> str:
    """Strip HTML tags and return plain text. Note: HTML entities are unaffected."""
    # A comment could contain a tag, so strip comments first
    while (start := text.find('<!--')) != -1 and (end := text.find('-->', start)) != -1:
        text = text[:start] + text[end + 3 :]

    while (start := text.find('<')) != -1 and (end := text.find('>', start)) != -1:
        text = text[:start] + text[end + 1 :]

    # Collapse whitespace
    text = ' '.join(text.split())
    return text


def _render_inner_html(el: etree.Element, md: markdown.Markdown) -> str:
    # The `UnescapeTreeprocessor` runs after `toc` extension so run here.
    text = md.serializer(el)
    text = _unescape(text)

    # Strip parent tag
    start = text.index('>') + 1
    end = text.rindex('<')
    text = text[start:end].strip()

    for pp in md.postprocessors:
        text = pp.run(text)
    return text


def _remove_anchorlink(el: etree.Element) -> None:
    """Drop anchorlink from the element, if present."""
    if len(el) > 0 and el[-1].tag == 'a' and el[-1].get('class') == 'headerlink':
        del el[-1]


def _remove_fnrefs(root: etree.Element) -> None:
    """Remove footnote references from the element, if any are present."""
    for parent in root.findall('.//sup[@id]/..'):
        _replace_elements_with_text(parent, _predicate_for_fnrefs)


def _predicate_for_fnrefs(el: etree.Element) -> str | None:
    if el.tag == 'sup' and el.get('id', '').startswith('fnref'):
        return ''
    return None


def _extract_alt_texts(root: etree.Element) -> None:
    """For images that have an `alt` attribute, replace them with this content."""
    for parent in root.findall('.//img[@alt]/..'):
        _replace_elements_with_text(parent, _predicate_for_alt_texts)


def _predicate_for_alt_texts(el: etree.Element) -> str | None:
    if el.tag == 'img' and (alt := el.get('alt')):
        return alt
    return None


def _replace_elements_with_text(
    parent: etree.Element, predicate: Callable[[etree.Element], str | None]
) -> None:
    """For each child element, if matched, replace it with the text returned from the predicate."""
    carry_text = ""
    for child in reversed(parent):  # Reversed for the ability to mutate during iteration.
        # Remove matching elements but carry any `tail` text to preceding elements.
        new_text = predicate(child)
        if new_text is not None:
            carry_text = new_text + (child.tail or "") + carry_text
            parent.remove(child)
        elif carry_text:
            child.tail = (child.tail or "") + carry_text
            carry_text = ""
    if carry_text:
        parent.text = (parent.text or "") + carry_text