1
|
|
|
#!/usr/bin/env python |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
Pandoc filter for changing font size in LaTeX |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
from panflute import * |
8
|
|
|
|
9
|
|
|
def fontsize(elem, doc): |
10
|
|
|
# Is it in the right format and is it a Span, Div, Code or CodeBlock? |
11
|
|
|
if doc.format == 'latex' and elem.tag in ['Span', 'Div', 'Code', 'CodeBlock']: |
12
|
|
|
|
13
|
|
|
# Get the classes |
14
|
|
|
classes = set(elem.classes) |
15
|
|
|
|
16
|
|
|
# Loop on all fontsize definition |
17
|
|
|
for definition in doc.defined: |
18
|
|
|
|
19
|
|
|
# Is the classes correct? |
20
|
|
|
if classes >= definition['classes']: |
21
|
|
|
|
22
|
|
|
# Is it a span? |
23
|
|
|
if isinstance(elem, Span): |
24
|
|
|
elem.content.insert(0, RawInline(definition['latex'], 'tex')) |
25
|
|
|
|
26
|
|
|
# Is it a Div? |
27
|
|
|
elif isinstance(elem, Div): |
28
|
|
|
elem.content.insert(0, RawBlock('{' + definition['latex'], 'tex')) |
29
|
|
|
elem.content.append(RawBlock('}', 'tex')) |
30
|
|
|
|
31
|
|
|
# Is it a Code? |
32
|
|
|
elif isinstance(elem, Code): |
33
|
|
|
return [RawInline('{' + definition['latex'], 'tex'), elem, RawInline('}', 'tex')] |
34
|
|
|
|
35
|
|
|
# Is it a CodeBlock? |
36
|
|
|
elif isinstance(elem, CodeBlock): |
37
|
|
|
return [RawBlock('{' + definition['latex'], 'tex'), elem, RawBlock('}', 'tex')] |
38
|
|
|
|
39
|
|
|
def prepare(doc): |
40
|
|
|
# Prepare the definitions |
41
|
|
|
doc.defined = [] |
42
|
|
|
|
43
|
|
|
# Get the meta data |
44
|
|
|
meta = doc.get_metadata('pandoc-latex-fontsize') |
45
|
|
|
|
46
|
|
|
if isinstance(meta, list): |
47
|
|
|
|
48
|
|
|
# Loop on all definitions |
49
|
|
|
for definition in meta: |
50
|
|
|
|
51
|
|
|
# Verify the definition type |
52
|
|
|
if isinstance(definition, dict): |
53
|
|
|
|
54
|
|
|
if 'classes' in definition and isinstance(definition['classes'], list): |
55
|
|
|
|
56
|
|
|
# Get the classes |
57
|
|
|
classes = definition['classes'] |
58
|
|
|
|
59
|
|
|
# Get the size |
60
|
|
|
if 'size' in definition and definition['size'] in ['Huge', 'huge', 'LARGE', 'Large', 'large', 'normalsize', 'small', 'footnotesize', 'scriptsize', 'tiny']: |
61
|
|
|
size = definition['size'] |
62
|
|
|
else: |
63
|
|
|
size = 'normalsize' |
64
|
|
|
|
65
|
|
|
# Add a definition |
66
|
|
|
doc.defined.append({'classes' : set(classes), 'latex': '\\' + size + ' '}) |
67
|
|
|
|
68
|
|
|
def main(doc = None): |
69
|
|
|
run_filter(fontsize, prepare = prepare, doc = doc) |
70
|
|
|
|
71
|
|
|
if __name__ == '__main__': |
72
|
|
|
main() |
73
|
|
|
|
74
|
|
|
|