pandoc_latex_french_spaces.spaces()   B
last analyzed

Complexity

Conditions 8

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
rs 7.3333
c 0
b 0
f 0
cc 8
nop 2
1
#!/usr/bin/env python3
2
3
4
"""
5
Pandoc filter for converting spaces to non-breakable spaces in LaTeX
6
for french ponctuation
7
"""
8
9
from panflute import Space, Str, RawInline, run_filter  # type: ignore
10
11
12
def spaces(elem, doc):
13
    """
14
    Add LaTeX spaces when needed.
15
    """
16
    # Is it in the right format and is it a Space?
17
    if doc.format in ["latex", "beamer"] and isinstance(elem, Space):
18
        if isinstance(elem.prev, Str) and elem.prev.text in ["«", "“", "‹"]:
19
            return RawInline("\\thinspace{}", "tex")
20
        if isinstance(elem.next, Str):
21
            if elem.next.text == ":":
22
                return RawInline("~", "tex")
23
            if elem.next.text in [";", "?", "!", "»", "”", "›"]:
24
                return RawInline("\\thinspace{}", "tex")
25
    return None
26
27
28
def main(doc=None):
29
    """
30
    main function.
31
    """
32
    return run_filter(spaces, doc=doc)
33
34
35
if __name__ == "__main__":
36
    main()
37