Total Complexity | 10 |
Total Lines | 82 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | #!/usr/bin/env python |
||
2 | |||
3 | """ |
||
4 | Pandoc filter for converting horizontal rule to new page in LaTeX. |
||
5 | """ |
||
6 | from __future__ import annotations |
||
7 | |||
8 | from panflute import ( |
||
9 | Doc, |
||
10 | Element, |
||
11 | HorizontalRule, |
||
12 | MetaBool, |
||
13 | MetaMap, |
||
14 | RawBlock, |
||
15 | run_filter, |
||
16 | ) |
||
17 | |||
18 | |||
19 | def newpage(elem: Element, doc: Doc) -> RawBlock | None: |
||
20 | r""" |
||
21 | Replace HorizontalRule by a \clearpage or a \cleardoublepage. |
||
22 | |||
23 | Arguments |
||
24 | --------- |
||
25 | elem |
||
26 | A pandoc element |
||
27 | doc |
||
28 | pandoc document |
||
29 | |||
30 | Returns |
||
31 | ------- |
||
32 | RawBlock | None |
||
33 | A RawBlock or None. |
||
34 | """ |
||
35 | # Is it in the right format and is it an HorizontalRule? |
||
36 | if doc.format == "latex" and isinstance(elem, HorizontalRule): |
||
37 | if doc.double: |
||
38 | return RawBlock("\\cleardoublepage", "tex") |
||
39 | return RawBlock("\\clearpage", "tex") |
||
40 | return None |
||
41 | |||
42 | |||
43 | def prepare(doc: Doc) -> None: |
||
44 | """ |
||
45 | Prepare document. |
||
46 | |||
47 | Arguments |
||
48 | --------- |
||
49 | doc |
||
50 | pandoc document |
||
51 | """ |
||
52 | doc.double = True |
||
53 | |||
54 | if ( |
||
55 | "pandoc-latex-newpage" in doc.metadata.content |
||
56 | and isinstance(doc.metadata.content["pandoc-latex-newpage"], MetaMap) |
||
57 | and "double" in doc.metadata.content["pandoc-latex-newpage"] |
||
58 | and isinstance(doc.metadata.content["pandoc-latex-newpage"]["double"], MetaBool) |
||
59 | ): |
||
60 | doc.double = doc.metadata.content["pandoc-latex-newpage"]["double"].boolean |
||
61 | |||
62 | |||
63 | def main(doc: Doc | None = None) -> Doc: |
||
64 | """ |
||
65 | Convert the pandoc document. |
||
66 | |||
67 | Arguments |
||
68 | --------- |
||
69 | doc |
||
70 | pandoc document |
||
71 | |||
72 | Returns |
||
73 | ------- |
||
74 | Doc |
||
75 | The modified document. |
||
76 | """ |
||
77 | return run_filter(newpage, prepare=prepare, doc=doc) |
||
78 | |||
79 | |||
80 | if __name__ == "__main__": |
||
81 | main() |
||
82 |