-
Notifications
You must be signed in to change notification settings - Fork 13
/
pandoc_include.py
165 lines (127 loc) · 4.55 KB
/
pandoc_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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Panflute filter to allow file includes
Each include statement has its own line and has the syntax:
!include ../somefolder/somefile
!include-header ./header.yaml
Or
$include ../somefolder/somefile
$include-header ./header.yaml
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
import yaml
import json
from collections import OrderedDict
def is_include_line(elem):
# Return 0 for false, 1 for include file, 2 for include header
if len(elem.content) < 3:
return 0
elif not all(isinstance(x, (pf.Str, pf.Space)) for x in elem.content):
return 0
elif elem.content[0].text != '!include' and elem.content[0].text != '$include' and \
elem.content[0].text != '!include-header' and elem.content[0].text != '$include-header':
return 0
elif type(elem.content[1]) != pf.Space:
return 0
elif elem.content[0].text == '!include' or elem.content[0].text == '$include':
# include file
return 1
else:
# include header
return 2
def get_filename(elem, includeType):
fn = pf.stringify(elem, newlines=False).split(maxsplit=1)[1]
if not os.path.splitext(fn)[1]:
if includeType == 1:
fn += '.md'
else:
fn += '.yaml'
return fn
# Record whether the entry has been entered
entryEnter = False
# Inherited options
options = None
temp_filename = '.temp.pandoc-include'
def action(elem, doc):
global entryEnter
global options
if isinstance(elem, pf.Para):
includeType = is_include_line(elem)
if includeType == 0:
return
# Try to read inherited options from temp file
if options is None:
try:
with open(temp_filename, 'r') as f:
options = json.load(f)
except:
options = {}
pass
# pandoc options
pandoc_options = doc.get_metadata('pandoc-options')
if not pandoc_options:
if 'pandoc-options' in options:
pandoc_options = options['pandoc-options']
else:
# default options
pandoc_options = ['--filter=pandoc-include']
else:
# Replace em-dash to double dashes in smart typography
for i in range(len(pandoc_options)):
pandoc_options[i] = pandoc_options[i].replace('\u2013', '--')
options['pandoc-options'] = pandoc_options
# The entry file's directory
entry = doc.get_metadata('include-entry')
if not entryEnter and entry:
os.chdir(entry)
entryEnter = True
fn = get_filename(elem, includeType)
if not os.path.isfile(fn):
raise ValueError('Included file not found: ' +
'%r %r %r' % (fn, entry, os.getcwd()))
with open(fn, encoding="utf-8") as f:
raw = f.read()
# Save current path
cur_path = os.getcwd()
# Change to included file's path so that sub-include's path is correct
target = os.path.dirname(fn)
# Empty means relative to current dir
if not target:
target = '.'
os.chdir(target)
# save options
with open(temp_filename, 'w+') as f:
json.dump(options, f)
# Add recursive include support
new_elems = None
new_metadata = None
if includeType == 1:
new_elems = pf.convert_text(
raw, extra_args=pandoc_options)
# Get metadata (Recursive header include)
new_metadata = pf.convert_text(raw, standalone=True, extra_args=pandoc_options).get_metadata()
else:
# Read header from yaml
new_metadata = yaml.safe_load(raw)
new_metadata = OrderedDict(new_metadata)
# Merge metadata
for key in new_metadata:
if not key in doc.get_metadata():
doc.metadata[key] = new_metadata[key]
# delete temp file (the file might have been deleted in subsequent executions)
if os.path.exists(temp_filename):
os.remove(temp_filename)
# Restore to current path
os.chdir(cur_path)
# 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()