Passed
Push — develop ( 25e15b...eb6ca8 )
by Christophe
01:09
created

pandoc_latex_french_spaces._main.spaces()   C

Complexity

Conditions 10

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 30
rs 5.9999
c 0
b 0
f 0
cc 10
nop 2

How to fix   Complexity   

Complexity

Complex classes like pandoc_latex_french_spaces._main.spaces() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python3
2
3
"""
4
Pandoc filter for converting spaces to non-breakable spaces.
5
6
This filter is for use in LaTeX for french ponctuation.
7
"""
8
from __future__ import annotations
9
10
from panflute import Doc, Element, RawInline, Space, Str, run_filter
11
12
13
def spaces(elem: Element, doc: Doc) -> RawInline | None:
14
    """
15
    Add LaTeX spaces when needed.
16
17
    Arguments
18
    ---------
19
    elem
20
        A tree element.
21
    doc
22
        The pandoc document.
23
24
    Returns
25
    -------
26
    RawInline | None
27
        A RawInLine or None.
28
    """
29
    # Is it in the right format and is it a Space?
30
    if doc.format in ("latex", "beamer") and isinstance(elem, Space):
31
        if (
32
            isinstance(elem.prev, Str)
33
            and elem.prev.text
34
            and elem.prev.text[-1] in ("«", "“", "‹")
35
        ):
36
            return RawInline("\\thinspace{}", "tex")
37
        if isinstance(elem.next, Str) and elem.next.text:
38
            if elem.next.text[0] == ":":
39
                return RawInline("~", "tex")
40
            if elem.next.text[0] in (";", "?", "!", "»", "”", "›"):
41
                return RawInline("\\thinspace{}", "tex")
42
    return None
43
44
45
def main(doc: Doc | None = None) -> Doc:
46
    """
47
    Process conversion.
48
49
    Arguments
50
    ---------
51
    doc
52
        The pandoc document
53
54
    Returns
55
    -------
56
    Doc
57
        The modified document
58
    """
59
    return run_filter(spaces, doc=doc)
60
61
62
if __name__ == "__main__":
63
    main()
64