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

pandoc_latex_french_spaces._main   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 20
dl 0
loc 64
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A main() 0 15 1
C spaces() 0 30 10
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