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