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
|
|
|
return add_latex(elem, definition['latex']) |
22
|
|
|
|
23
|
|
|
def add_latex(elem, latex): |
24
|
|
|
# Is it a Span? |
25
|
|
|
if isinstance(elem, Span): |
26
|
|
|
elem.content.insert(0, RawInline(latex, 'tex')) |
27
|
|
|
|
28
|
|
|
# Is it a Div? |
29
|
|
|
elif isinstance(elem, Div): |
30
|
|
|
elem.content.insert(0, RawBlock('{' + latex, 'tex')) |
31
|
|
|
elem.content.append(RawBlock('}', 'tex')) |
32
|
|
|
|
33
|
|
|
# Is it a Code? |
34
|
|
|
elif isinstance(elem, Code): |
35
|
|
|
return [RawInline('{' + latex, 'tex'), elem, RawInline('}', 'tex')] |
36
|
|
|
|
37
|
|
|
# Is it a CodeBlock? |
38
|
|
|
elif isinstance(elem, CodeBlock): |
39
|
|
|
return [RawBlock('{' + latex, 'tex'), elem, RawBlock('}', 'tex')] |
40
|
|
|
|
41
|
|
|
def prepare(doc): |
42
|
|
|
# Prepare the definitions |
43
|
|
|
doc.defined = [] |
44
|
|
|
|
45
|
|
|
# Get the meta data |
46
|
|
|
meta = doc.get_metadata('pandoc-latex-fontsize') |
47
|
|
|
|
48
|
|
|
if isinstance(meta, list): |
49
|
|
|
|
50
|
|
|
# Loop on all definitions |
51
|
|
|
for definition in meta: |
52
|
|
|
|
53
|
|
|
# Verify the definition |
54
|
|
|
if isinstance(definition, dict) and 'classes' in definition and isinstance(definition['classes'], list): |
55
|
|
|
add_definition(doc.defined, definition) |
56
|
|
|
|
57
|
|
|
def add_definition(defined, definition): |
58
|
|
|
# Get the classes |
59
|
|
|
classes = definition['classes'] |
60
|
|
|
|
61
|
|
|
# Get the size |
62
|
|
|
if 'size' in definition and definition['size'] in ['Huge', 'huge', 'LARGE', 'Large', 'large', 'normalsize', 'small', 'footnotesize', 'scriptsize', 'tiny']: |
63
|
|
|
size = definition['size'] |
64
|
|
|
else: |
65
|
|
|
size = 'normalsize' |
66
|
|
|
|
67
|
|
|
# Add a definition |
68
|
|
|
defined.append({'classes' : set(classes), 'latex': '\\' + size + ' '}) |
69
|
|
|
|
70
|
|
|
def main(doc = None): |
71
|
|
|
run_filter(fontsize, prepare = prepare, doc = doc) |
72
|
|
|
|
73
|
|
|
if __name__ == '__main__': |
74
|
|
|
main() |
75
|
|
|
|
76
|
|
|
|