-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathinclude.py
62 lines (43 loc) · 1.36 KB
/
include.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
"""
Panflute filter to allow file includes
Each include statement has its own line and has the syntax:
$include ../somefolder/somefile
Each include statement must be in its own paragraph. That is, in its own line
and separated by blank lines.
If no extension was given, ".md" is assumed.
"""
import os
import panflute as pf
def is_include_line(elem):
if len(elem.content) < 3:
return False
elif not all (isinstance(x, (pf.Str, pf.Space)) for x in elem.content):
return False
elif elem.content[0].text != '$include':
return False
elif type(elem.content[1]) != pf.Space:
return False
else:
return True
def get_filename(elem):
fn = pf.stringify(elem, newlines=False).split(maxsplit=1)[1]
if not os.path.splitext(fn)[1]:
fn += '.md'
return fn
def action(elem, doc):
if isinstance(elem, pf.Para) and is_include_line(elem):
fn = get_filename(elem)
if not os.path.isfile(fn):
return
with open(fn) as f:
raw = f.read()
new_elems = pf.convert_text(raw)
# Alternative A:
return new_elems
# Alternative B:
# div = pf.Div(*new_elems, attributes={'source': fn})
# return div
def main(doc=None):
return pf.run_filter(action, doc=doc)
if __name__ == '__main__':
main()