Passed
Push — develop ( 3f128f...d8a87a )
by Christophe
02:11
created

pandoc_latex_french_spaces._main   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 16
dl 0
loc 59
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A main() 0 15 1
B spaces() 0 26 8
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
9
from panflute import Doc, Element, RawInline, Space, Str, run_filter
10
11
12
def spaces(elem: Element, doc: Doc) -> RawInline | None:
13
    """
14
    Add LaTeX spaces when needed.
15
16
    Arguments
17
    ---------
18
    elem
19
        A tree element.
20
    doc
21
        The pandoc document.
22
23
    Returns
24
    -------
25
    RawInline | None
26
        A RawInLine or None.
27
    """
28
    # Is it in the right format and is it a Space?
29
    if doc.format in ("latex", "beamer") and isinstance(elem, Space):
30
        if isinstance(elem.prev, Str) and elem.prev.text[-1] in ("«", "“", "‹"):
31
            return RawInline("\\thinspace{}", "tex")
32
        if isinstance(elem.next, Str):
33
            if elem.next.text[0] == ":":
34
                return RawInline("~", "tex")
35
            if elem.next.text[0] in (";", "?", "!", "»", "”", "›"):
36
                return RawInline("\\thinspace{}", "tex")
37
    return None
38
39
40
def main(doc: Doc | None = None) -> Doc:
41
    """
42
    Process conversion.
43
44
    Arguments
45
    ---------
46
    doc
47
        The pandoc document
48
49
    Returns
50
    -------
51
    Doc
52
        The modified document
53
    """
54
    return run_filter(spaces, doc=doc)
55
56
57
if __name__ == "__main__":
58
    main()
59