Passed
Push — develop ( a26c09...d88333 )
by Christophe
03:56
created

pandoc_figure._main.figure()   B

Complexity

Conditions 6

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 36
rs 8.5166
c 0
b 0
f 0
cc 6
nop 2
1
#!/usr/bin/env python
2
3
"""
4
Pandoc filter for adding complex figures.
5
"""
6
7
from panflute import (
0 ignored issues
show
introduced by
Unable to import 'panflute'
Loading history...
8
    Caption,
9
    Div,
10
    Doc,
11
    Element,
12
    Figure,
13
    Plain,
14
    convert_text,
15
    debug,
16
    run_filter,
17
)
18
19
20
# pylint: disable=broad-exception-caught
0 ignored issues
show
introduced by
Bad option value 'broad-exception-caught'
Loading history...
21
def figure(elem: Element, doc: Doc) -> Figure | None:
22
    """
23
    Transform a div element into a figure element.
24
25
    Arguments
26
    ---------
27
    elem
28
        The pandoc element
29
    doc
30
        The pandoc document
31
32
    Returns
33
    -------
34
    Figure | None
35
        Figure or None.
36
    """
37
    if (
38
        doc.api_version >= (1, 23)
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
39
        and isinstance(elem, Div)
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
40
        and "figure" in elem.classes
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
41
        and "caption" in elem.attributes
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
42
    ):
43
        try:
44
            caption = convert_text(elem.attributes["caption"])
45
            del elem.attributes["caption"]
46
            elem.classes.remove("figure")
47
            return Figure(
48
                *elem.content,
49
                caption=Caption(Plain(*caption[0].content)),
50
                identifier=elem.identifier,
51
                classes=elem.classes,
52
                attributes=elem.attributes,
53
            )
54
        except Exception as error:  # noqa: B902
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
55
            debug(f"[WARNING] pandoc-figure: {error}")
56
    return None
57
58
59
def prepare(doc: Doc) -> None:
60
    """
61
    Prepare the pandoc document.
62
63
    Arguments
64
    ---------
65
    doc
66
        The pandoc document
67
    """
68
    if doc.api_version < (1, 23):
69
        debug(
70
            f"[WARNING] pandoc-figure: pandoc api version "
71
            f"{'.'.join(str(value) for value in doc.api_version)} "
72
            "is not compatible"
73
        )
74
75
76
def main(doc: Doc | None = None) -> Doc:
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
Loading history...
77
    """
78
    Convert the pandoc document.
79
80
    Arguments
81
    ---------
82
    doc
83
        The pandoc document
84
85
    Returns
86
    -------
87
    Doc
88
        The modified pandoc document
89
    """
90
    return run_filter(figure, prepare=prepare, doc=doc)
91
92
93
if __name__ == "__main__":
94
    main()
95