-
Notifications
You must be signed in to change notification settings - Fork 16
/
acronyms.py
59 lines (42 loc) · 1.93 KB
/
acronyms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Panflute filter that allows for acronyms in latex
Usage:
- In markdown, use it as links: [SO](acro "Stack Overflow")
- When outputting to latex, you must add this line to the preamble:
\\usepackage[acronym,smallcaps]{glossaries}
- Then, this filter will add "\newacronym{LRU}{LRU}{Least Recently Used} for the definition of LRU and finally \gls{LRU} to every time the term is used in the text."
(see https://groups.google.com/forum/#!topic/pandoc-discuss/Bz1cG55BKjM)
"""
from string import Template # using .format() is hard because of {} in tex
import panflute as pf
TEMPLATE_GLS = Template(r"\gls{$acronym}")
TEMPLATE_NEWACRONYM = Template(r"\newacronym{$acronym}{$acronym}{$definition}")
def prepare(doc):
doc.acronyms = {}
def action(e, doc):
if isinstance(e, pf.Link) and e.url == 'acro':
acronym = pf.stringify(e)
definition = e.title
# Only update dictionary if definition is not empty
if definition:
doc.acronyms[acronym] = definition
if doc.format == 'latex':
tex = '\gls{{}}'.format(acronym)
tex = TEMPLATE_GLS.safe_substitute(acronym=acronym)
return pf.RawInline(tex, format='latex')
def finalize(doc):
if doc.format == 'latex':
tex = [r'\usepackage[acronym,smallcaps]{glossaries}', '\makeglossaries']
for acronym, definition in doc.acronyms.items():
tex_acronym = TEMPLATE_NEWACRONYM.safe_substitute(acronym=acronym, definition=definition)
tex.append(tex_acronym)
tex = [pf.MetaInlines(pf.RawInline(line, format='latex')) for line in tex]
tex = pf.MetaList(*tex)
if 'header-includes' in doc.metadata:
doc.metadata['header-includes'].content.extend(tex)
else:
doc.metadata['header-includes'] = tex
def main(doc=None):
return pf.run_filter(action, prepare=prepare, finalize=finalize, doc=doc)
if __name__ == '__main__':
main()