-
Notifications
You must be signed in to change notification settings - Fork 77
/
collect_readmes.py
260 lines (186 loc) · 6.25 KB
/
collect_readmes.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
import os
from docutils.core import publish_doctree
full_name_lut = {
'a35t': 'Arty 35T',
'a100t': 'Arty 100T',
'nexys4ddr': 'Nexys 4 DDR',
'basys3': 'Basys 3',
'eos_s3': 'EOS S3',
'zybo': 'Zybo Z7',
'nexys_video': 'Nexys Video',
}
families = ('xc7', 'eos-s3')
inlines = ('literal', 'strong', 'reference')
def handle_default_with_inlines(block):
"""
Extracts information from a block that contains inline blocks in
its subtree. The text from the block is combined with the contents
of the inline blocks. This is used, i.e., when the code is inserted
in the text with `` marks.
Args:
block: A block with a inline block in the subtree
Returns: A dictionary with the extracted information
"""
text = ""
for node in block.traverse(
include_self=False,
condition=lambda x: x.parent.tagname.strip() not in inlines):
tagname = node.tagname.strip()
if tagname in ('paragraph', ):
continue
if tagname == 'literal':
text += f'``{node.astext()}``'
elif tagname == 'strong':
text += f'**{node.astext()}**'
elif tagname == 'reference':
text += f'`{node.astext()} <{node["refuri"]}>`_'
else:
text += node.astext()
ret = {}
ret['type'] = block.tagname.strip()
ret['text'] = text
return ret
def subtree_has_tag(block, tagname):
"""
Check if the doctree node contains a particular tag in its subtree.
Args:
block: A block that has to be checked
tagname: The searched tag
Returns: True if the subtree contains a node with the tagname or False otherwise.
"""
for node in block.traverse(include_self=False):
if node.tagname.strip() == tagname:
return True
return False
def handle_title(block):
"""
Extracts information from a title block (from a README doctree).
Args:
block: A title doctree block
Returns: A dictionary with the extracted information
"""
ret = {
'type': 'title',
'text': '\n'.join((block.astext(), '~' * 20)),
}
return ret
def handle_img(block):
"""
Extracts information from an image block (from a README doctree).
Args:
block: An image doctree block
Returns: A dictionary with the extracted information
"""
ret = {}
ret['type'] = 'image'
ret['uri'] = '../_static/' + os.path.join(*block['uri'].split('/')[3:])
ret['align'] = block.get('align', 'center')
ret['width'] = block.get('width', '100%')
return ret
def handle_literal_block(block):
"""
Extracts information from a literal block (from a README doctree).
Args:
block: A literal doctree block
Returns: A dictionary with the extracted information
"""
ret = {}
ret['type'] = 'literal_block'
ret['text'] = block.astext().split('\n')
ret['classes'] = block['classes']
try:
ret['name'] = full_name_lut[''.join(block['names']).split('-')[2]]
except:
ret['name'] = ''
return ret
def handle_note(block):
"""
Extracts information from a note block (from a README doctree).
If the block contain inline block in its subtree, the data from that block
will also be extracted.
Args:
block: A note doctree block
Returns: A dictionary with the extracted information
"""
ret = {}
ret['type'] = block.tagname.strip()
if sum(map(lambda x: subtree_has_tag(block, x), inlines)):
for node in block.traverse(
condition=lambda x: x.tagname.strip() == 'paragraph'):
ret['text'] = handle_default_with_inlines(node)['text']
else:
ret['text'] = block.astext()
ret['text'] = ret['text'].split('\n')
return ret
def handle_default(block):
"""
Extracts information from doctree blocks that need not be handled in
a special way. Nevertheless, if the block contains a inline block,
the informaton from that block will also be extracted.
Args:
block: A doctree block
Returns: A dictionary with the extracted information
"""
if sum(map(lambda x: subtree_has_tag(block, x), inlines)):
return handle_default_with_inlines(block)
ret = {}
ret['type'] = block.tagname.strip()
ret['text'] = block.astext()
return ret
handle_block_funcs = {
'title': handle_title,
'image': handle_img,
'literal_block': handle_literal_block,
'note': handle_note,
'default': handle_default,
}
def get_blocks(text):
"""
Get a traversed README doctree
Args:
text: Text of the example's README
Returns: Returns an iterable containing the README document structure
See `traverse` implementation in docutils/nodes.py for more details.
"""
doctree = publish_doctree(text)
return doctree.traverse(
condition=lambda x: x.tagname.strip() != 'document' and x.parent.
tagname.strip() != 'note'
)
def fill_context(text):
"""
Creates a jinja context dictionary for a F4PGA Toolchain usage example.
The dictionary contains all the important information from the example's README.
Args:
text: Text of the example's README
Returns: A context dictionary that should be added to the main jinja_contexts
"""
ret = []
start_group = False
in_group = False
for block in get_blocks(text):
tagname = block.tagname.strip()
# do not process those
if tagname in ('#text', 'inline') + inlines:
continue
# try do get handler; if not found, use the default one
if tagname not in handle_block_funcs.keys():
handler = 'default'
else:
handler = tagname
entry = handle_block_funcs[handler](block)
# internal logic for grouping code blocks into tab groups
if tagname == 'literal_block' and 'group' in ''.join(block['names']):
if not in_group:
start_group = True
in_group = True
else:
start_group = False
in_group = True
else:
in_group = False
start_group = False
entry['start_group'] = start_group
entry['in_group'] = in_group
ret.append(entry)
return ret