-
Notifications
You must be signed in to change notification settings - Fork 3
/
linkFilter.py
executable file
·64 lines (48 loc) · 2.01 KB
/
linkFilter.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
60
61
62
63
64
#!/usr/bin/python
#original (does not work on Linux or default OSX):
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python
"""
Pandoc filter to convert links from mediawiki in a more
useful format.
"""
import sys
from pandocfilters import toJSONFilter, Link, Str, RawInline
def linkhandler(key, value, frmt, meta):
if key == 'Link':
sys.stderr.write(
'Key: {} Type of Key: {} \nValue: {}\nfrmt: {} \nmeta: {}\n-------\n'
.format(
key, type(key),
value, frmt, meta))
if frmt == 'latex':
[unknown, [link, kind]] = value
if ("talk:" in link.lower()) and (kind == "wikilink"):
# deal with talk extension
# sys.stderr.write('unknown: {}\n'.format([u['c'] for u in unknown]))
textlist = [u['c'] if u['c'] else ' ' for u in unknown]
# sys.stderr.write('unknown: {}\n'.format(textlist))
textlist = ''.join(textlist)
# sys.stderr.write('unknown: {}\n'.format(textlist))
try:
return RawInline('latex', textlist)
except:
return RawInline('latex', '??')
if kind == "wikilink":
if "#" in link:
link = link.split('#')[1]
sys.stderr.write("link: {}\n---\n\n".format(link))
link = '-'.join([x.lower() for x in link.split('_')])
return RawInline('latex', "\\autoref{{{}}}".format(link))
elif key == 'RawInline':
sys.stderr.write(
'Key: {} Type of Key: {} \nValue: {}\nfrmt: {} \nmeta: {}\n-------\n'
.format(
key, type(key),
value, frmt, meta))
if frmt == 'latex':
kind, val = value
if kind == 'html':
if val == '<newpage>':
return RawInline('latex', '\\newpage')
if __name__ == "__main__":
toJSONFilter(linkhandler)