forked from leod/hncynic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_markdown.py
executable file
·114 lines (98 loc) · 2.96 KB
/
filter_markdown.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
"""
Do stuff
"""
import sys
from panflute import *
# Sections that we filter out completely
SECTION_IGNORE = [
'See also',
'Notes',
'References',
'External links',
'Gallery',
'Works cited',
'External links and suggested reading',
'Editions',
'Discography',
'Filmography',
'Notes and references',
'Further reading',
'Works',
'Awards',
'Patents & awards',
]
# Elements that we ignore
ELEMENT_IGNORE = [
Image,
Note, # footnotes and endnotes
Table,
RawBlock,
RawInline,
SmallCaps,
Strikeout,
Subscript,
Superscript,
]
def prepare(doc):
doc.my_current_section = None
def action(elem, doc):
#sys.stderr.write(repr(elem) + '\n')
# We must not filter out Doc, that leads to errors
if isinstance(elem, Doc):
return elem
# Filter certain sections
if doc.my_current_section is not None and doc.my_current_section in SECTION_IGNORE:
return []
if isinstance(elem, Link):
# For links, only keep the description text
descr = elem.content.list
# Filter links that don't have any description
if len(descr) == 0:
return []
# Filter strange thumbnail links
#
# E.g. there is a link whose description is:
# [Str(thumb|upright=1.1|),
# Emph(Str([The) Space Str(Astronomer](The_Astronomer_(Vermeer)) Space Str("wikilink"))),
# Space, Str(by), Space, Str([Johannes), Space, Str(Vermeer](Johannes_Vermeer), Space,
# Str("wikilink"))]
link_str = stringify(elem)
if (isinstance(descr[0], Str) and 'thumb|' in descr[0].text) or 'thumb|' in link_str:
return []
# Also ignore links to Wikipedia media
if link_str.startswith('File:') or link_str.startswith('Category:'):
return []
return descr
elif any([isinstance(elem, t) for t in ELEMENT_IGNORE]):
return []
elif isinstance(elem, Header):
#if elem.level == 2:
doc.my_current_section = stringify(elem)
if doc.my_current_section in SECTION_IGNORE:
return []
elif isinstance(elem, Strong):
# Hacker News only has Emph
return Emph(*elem.content)
elif isinstance(elem, Emph):
# Hacker News only has Emph
if len(elem.content) == 1 and isinstance(elem.content[0], Emph):
return elem.content[0]
else:
return elem
def main(doc=None):
return run_filter(action, prepare=prepare, doc=doc)
if __name__ == '__main__':
# If I don't do the following, I get:
#
# $ pandoc --wrap=none -f mediawiki -t markdown --filter filter_markdown.py < wiki/docs/00/New_Holland%2C_South_Dakota.txt
# [...]
# File "[...]/panflute/elements.py", line 695, in __init__
# self.format = check_group(format, RAW_FORMATS)
# File "[...]/panflute/utils.py", line 34, in check_group
# raise TypeError(msg)
# TypeError: element str not in group {'rtf', 'noteref', 'openxml', 'opendocument', 'latex', 'icml', 'html', 'context', 'tex'}
# pandoc: Error running filter filter_markdown.py
# Filter returned error status 1
elements.RAW_FORMATS.add('mediawiki')
main()