-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc_to_jekyll_impl.py
336 lines (241 loc) · 8.22 KB
/
rfc_to_jekyll_impl.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"""
Convert Abstract Factory RFC markdown-file into
jekyll-compatible markdown.
File is written to a /jekyll parent directory:
from -- /spec1.md
to -- /jekyll/1.md
"""
import os
import re
import time
import copy
DIV = '\n\n'
SPEC_TEMPLATE = 'http://rfc.abstractfactory.io/spec/{number}'
GITHUB_TEMPLATE = 'https://github.com/abstractfactory/rfc/blob/master/spec/spec{number}.md'
def blocks(content):
"""
Return a generator of blocks within `content`
*Blocks are text separated by an empty line.
"""
for group in content.split(DIV):
yield group
def parse(content):
"""Convert `content` into dictionary
Output
{
'title': str,
'summary': str,
'properties': str,
'content': str
}
"""
try:
gen = blocks(content)
title = gen.next().strip("# ")
summary = gen.next()
properties = gen.next()
if properties.startswith('!'):
# This is an optional image
properties = gen.next()
except StopIteration:
# At least these three blocks must exist
raise ValueError("Document is incorrectly formatted")
dproperties = {}
for prop in properties.splitlines():
prop = prop.strip("* ")
key, value = (prop.split(": ", 1) + [''])[:2]
dproperties[key.lower()] = value
content = DIV.join([g for g in gen])
result = {'title': title,
'summary': summary,
'properties': dproperties,
'content': content}
return result
def deparse(parsed):
"""Restore content from `parsed`"""
# Copy `parsed` so as to not accidentally
# make modifications to original
parsed = copy.deepcopy(parsed)
properties = parsed['properties']
# Convert properties into block, starting with standards
fields = []
syntax = '* %s: %s'
order = ('name', 'editor', 'related', 'tags', 'state')
for key in order:
value = properties.get(key)
if not value:
continue
field = syntax % (key.title(), value)
fields.append(field)
# And finishing with miscellaneous
for key, value in properties.iteritems():
if key in order:
continue
field = syntax % (key.title(), value)
fields.append(field)
properties = '\n'.join(fields)
parsed['properties'] = properties
blocks = []
order = ('title', 'summary', 'properties', 'content')
for key in order:
value = parsed[key]
if key == 'title':
value = '# ' + value
blocks.append(value)
reconstructed = DIV.join(blocks)
return reconstructed
def get_related(content):
"""Find mentions of 'RFC##' within `content` and return list of matches"""
related = re.findall(r'RFC\d+', content)
for rfc in related:
print "Document was implicitly related to %s" % rfc
return related
def reconstruct_parsed(parsed):
"""Given `parsed` content, reconstruct content with edited properties"""
# Copy `parsed` so as to not accidentally
# make modifications to original
parsed = copy.deepcopy(parsed)
# print parsed
properties = parsed['properties']
# Default state to 'raw'
if not 'state' in properties:
properties['state'] = 'raw'
content = parsed['content']
# Resolve relations
relations = properties.get('related') or ''
explicit_relations = []
if relations:
for relation in relations.split(", "):
explicit_relations.append(relation)
implicit_relations = get_related(content)
all_relations = explicit_relations + implicit_relations
all_relations.sort()
relations = ', '.join(all_relations)
properties['related'] = relations
return parsed
def to_jekyll_header(parsed):
"""Using parsed content, construct Jekyll header"""
jekyll_properties = {'layout': 'spec'}
# Disregard content
parsed.pop('content')
# Append properties from content
content_properties = parsed.pop('properties')
jekyll_properties.update(parsed)
jekyll_properties.update(content_properties)
header = '---\n'
for key, value in jekyll_properties.iteritems():
if not value:
continue
header += '%s: %s\n' % (key, value)
header += '---\n\n'
return header
def exclude_draft(content):
pat = re.compile(r'<draft>.*</draft>', re.DOTALL)
for group in pat.findall(content):
print "Excluding draft \n%s" % group
content = ''.join(content.split(group, 1)[0:2])
return content
def substitute_rfc(content):
"""
Find lone RFC statements and replace them with full links
Example
in This is RFC14
out This is [RFC14](http://path/to/rfc/14)
Expression
Statement MUST be preceeded by an empty space
Statement MUST be succeded by number
Statement MUST be succeded by either empty space OR newline
"""
pat = re.compile(r'RFC\d+')
rfcs = []
for rfc in pat.findall(content):
rfcs.append(rfc)
for rfc in rfcs:
number = rfc[3:]
link = SPEC_TEMPLATE.format(number=number)
replacement = "[%s](%s)" % (rfc, link)
# RFC may exist with multiple suffixes
suffixes = [' ', '\n', ', ', ')', '.', ':']
for suffix in suffixes:
source = rfc + suffix
target = replacement + suffix
content = content.replace(source, target)
print "Replacing %s with %s" % (rfc, replacement)
return content
def perform_string_substitution(content):
content = substitute_rfc(content)
return content
def generate_jekyll_document(path, simulate=False):
"""
Parse currently open file, and write it out to:
/jekyll/##.md
Where ## represents the number of the RFC
"""
with open(path) as f:
content = f.read()
# Only allow ASCII
# TODO: allow unicode..
content = content.decode('ascii', 'replace')
try:
content.decode('ascii')
except UnicodeDecodeError as e:
# block = unicode(content[(e.end - 10): (e.end + 10)])
# print dir(e)
# print
# text = content[e.end].decode('UTF-8', 'strict')
raise ValueError("Found non-ascii characters "
"in document %s: %s"
% (path))
# Append property derived from `path`
parsed = parse(content)
basename = os.path.basename(path)
name, ext = os.path.splitext(basename)
number = name.strip("spec")
parsed['number'] = number
# Append date
modified = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
modified += '+0100'
parsed['modified'] = modified
# Append GitHub link
link = GITHUB_TEMPLATE.format(number=number)
parsed['link'] = link
# Reconstruct content based on implicit data
# Related
# State Default State
parsed = reconstruct_parsed(parsed)
# Reconstruct content based on newly reconstructed parsed
content = deparse(parsed)
# Exclude parts of content marked <draft>
content = exclude_draft(content)
# Process string-substitutions
content = perform_string_substitution(content)
# Merge original content with jekyll header
jekyll_header = to_jekyll_header(parsed)
jekyll_document = jekyll_header + content
# Construct output path
root_directory = os.path.dirname(path)
output_directory = os.path.join(root_directory, 'jekyll')
output_file = '%s.md' % number
output_path = os.path.join(output_directory, output_file)
if not os.path.exists(output_directory):
os.mkdir(output_directory)
print "Writing Jekyll file %s" % output_path
if not simulate:
with open(output_path, 'w') as f:
f.write(jekyll_document)
else:
print jekyll_document
return True
if __name__ == '__main__':
demo_content = '''
# This is the title
Followed by a summary.
* Name: And some properties
* Editor: By Marcus <[email protected]>
This is RFC12, and this RFC14
'''
path = r'C:\Users\marcus\Dropbox\AF\development\marcus\pipi\rfc\spec\spec45.md'
generate_jekyll_document(path, simulate=True)
# parsed = parse(demo_content)
# parsed = reconstruct_parsed(parsed)
# print deparse(parsed)