1
|
|
|
#!/usr/bin/env python |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
Pandoc filter for unlisting user defined headers in LaTeX |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
from panflute import * |
8
|
|
|
|
9
|
|
|
def header(elem, doc): |
10
|
|
|
# Is it in the right format and is it an Header? |
11
|
|
|
if doc.format == 'latex' and isinstance(elem, Header): |
12
|
|
|
classes = set(elem.classes) |
13
|
|
|
for current in doc.list_classes: |
14
|
|
|
if current <= classes: |
15
|
|
|
return [ |
16
|
|
|
RawBlock('\\renewcommand{\\addcontentsline}[3]{}', 'tex'), |
17
|
|
|
elem, |
18
|
|
|
RawBlock('\\renewcommand{\\addcontentsline}[3]{\\oldaddcontentsline{#1}{#2}{#3}}', 'tex'), |
19
|
|
|
] |
20
|
|
|
|
21
|
|
|
def prepare(doc): |
22
|
|
|
doc.list_classes = [set(['unlisted'])] |
23
|
|
|
metadata = doc.get_metadata() |
24
|
|
|
if 'pandoc-latex-unlisted' in metadata and isinstance(metadata['pandoc-latex-unlisted'], list): |
25
|
|
|
doc.list_classes = [] |
26
|
|
|
for classes in metadata['pandoc-latex-unlisted']: |
27
|
|
|
try: |
28
|
|
|
type = unicode |
29
|
|
|
except NameError: |
30
|
|
|
type = str |
31
|
|
|
if isinstance(classes, type): |
32
|
|
|
doc.list_classes.append(set([classes])) |
33
|
|
|
elif isinstance(classes, list): |
34
|
|
|
doc.list_classes.append(set(classes)) |
35
|
|
|
|
36
|
|
|
def finalize(doc): |
37
|
|
|
# Add header-includes if necessary |
38
|
|
|
if 'header-includes' not in doc.metadata: |
39
|
|
|
doc.metadata['header-includes'] = MetaList() |
40
|
|
|
# Convert header-includes to MetaList if necessary |
41
|
|
|
elif not isinstance(doc.metadata['header-includes'], MetaList): |
42
|
|
|
doc.metadata['header-includes'] = MetaList(doc.metadata['header-includes']) |
43
|
|
|
doc.metadata['header-includes'].append(MetaInlines(RawInline('\\let\\oldaddcontentsline\\addcontentsline', 'tex'))) |
44
|
|
|
|
45
|
|
|
def main(doc = None): |
46
|
|
|
return run_filter(header, prepare = prepare, doc = doc, finalize = finalize) |
47
|
|
|
|
48
|
|
|
if __name__ == '__main__': |
49
|
|
|
main() |
50
|
|
|
|
51
|
|
|
|