pandoc_beamer_block._main.main()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
#!/usr/bin/env python
2
3
"""
4
Pandoc filter for adding beamer block on specific div.
5
"""
6
7
from panflute import Div, Doc, Element, RawBlock, convert_text, run_filter
8
9
10
def prepare(doc: Doc):
11
    """
12
    Prepare the document.
13
14
    Arguments
15
    ---------
16
    doc
17
        The pandoc document
18
    """
19
    # Prepare the definitions
20
    doc.defined = []
21
22
    # Get the meta data
23
    meta = doc.get_metadata("pandoc-beamer-block")
24
25
    if isinstance(meta, list):
26
        # Loop on all definitions
27
        for definition in meta:
28
            # Verify the definition
29
            if (
30
                isinstance(definition, dict)
31
                and "classes" in definition
32
                and isinstance(definition["classes"], list)
33
            ):
34
                definition["classes"] = frozenset(definition["classes"])
35
                definition["type"] = definition.get("type", "info")
36
                doc.defined.append(definition)
37
38
39
def latex(
40
    elem: Element, environment: str, title: str, optional: bool = False
41
) -> list[Element]:
42
    """
43
    Generate the LaTeX code.
44
45
    Arguments
46
    ---------
47
    elem
48
        The current element
49
50
    environment
51
        The environment to add
52
53
    title
54
        The environment title
55
56
    optional
57
        The title optionality
58
59
    Returns
60
    -------
61
    list[Element]
62
        A list of pandoc elements.
63
    """
64
    if optional:
65
        if title:
66
            return [
67
                RawBlock(f"\\begin{{{environment}}}[{title}]", "tex"),
68
                elem,
69
                RawBlock(f"\\end{{{environment}}}", "tex"),
70
            ]
71
        return [
72
            RawBlock(f"\\begin{{{environment}}}", "tex"),
73
            elem,
74
            RawBlock(f"\\end{{{environment}}}", "tex"),
75
        ]
76
    return [
77
        RawBlock(f"\\begin{{{environment}}}{{{title}}}", "tex"),
78
        elem,
79
        RawBlock(f"\\end{{{environment}}}", "tex"),
80
    ]
81
82
83
# pylint: disable=too-many-return-statements
84
def block(elem: Element, doc: Doc) -> list[Element] | None:
85
    """
86
    Transform div element.
87
88
    Arguments
89
    ---------
90
    elem
91
        current element
92
    doc
93
        pandoc document
94
95
    Returns
96
    -------
97
    list[Element] | None
98
        A list of pandoc elements or None.
99
    """
100
    if doc.format == "beamer" and isinstance(elem, Div):
101
        classes = frozenset(elem.classes)
102
103
        # Loop on all fontsize definition
104
        for definition in doc.defined:
105
            # Are the classes correct?
106
            if classes >= definition["classes"]:
107
                if "title" in elem.attributes:
108
                    title = convert_text(
109
                        elem.attributes["title"],
110
                        output_format="latex",
111
                    )
112
                else:
113
                    title = ""
114
115
                if definition["type"] == "alert":
116
                    return latex(elem, "alertblock", title)
117
                if definition["type"] == "example":
118
                    return latex(elem, "exampleblock", title)
119
                if definition["type"] in (
120
                    "theorem",
121
                    "proof",
122
                    "corollary",
123
                    "definition",
124
                    "lemma",
125
                    "fact",
126
                ):
127
                    return latex(elem, definition["type"], title, True)
128
                return latex(elem, "block", title)
129
    return None
130
131
132
def main(doc: Doc | None = None) -> Doc:
133
    """
134
    Convert the pandoc document.
135
136
    Arguments
137
    ---------
138
    doc
139
        pandoc document
140
141
    Returns
142
    -------
143
    Doc
144
        The modified pandoc document.
145
    """
146
    return run_filter(block, doc=doc, prepare=prepare)
147
148
149
if __name__ == "__main__":
150
    main()
151