2020-12-14 05:03:31 -05:00
|
|
|
import os
|
|
|
|
from docutils.core import publish_doctree
|
|
|
|
|
|
|
|
full_name_lut = {
|
|
|
|
'a35t': 'Arty 35T',
|
|
|
|
'a100t': 'Arty 100T',
|
2021-04-21 13:52:09 -04:00
|
|
|
'nexys4ddr': 'Nexys 4 DDR',
|
2020-12-14 05:03:31 -05:00
|
|
|
'basys3': 'Basys 3',
|
|
|
|
'eos_s3': 'EOS S3',
|
2020-12-15 08:37:52 -05:00
|
|
|
'zybo': 'Zybo Z7',
|
2020-11-30 05:46:40 -05:00
|
|
|
'nexys_video': 'Nexys Video',
|
2020-12-14 05:03:31 -05:00
|
|
|
}
|
|
|
|
families = ('xc7', 'eos-s3')
|
2020-12-17 06:24:09 -05:00
|
|
|
inlines = ('literal', 'strong', 'reference')
|
2020-12-14 05:03:31 -05:00
|
|
|
|
|
|
|
|
2020-12-17 06:24:09 -05:00
|
|
|
def handle_default_with_inlines(block):
|
2020-12-14 05:03:31 -05:00
|
|
|
"""
|
2020-12-17 06:24:09 -05:00
|
|
|
Extracts information from a block that contains inline blocks in
|
2020-12-14 05:03:31 -05:00
|
|
|
its subtree. The text from the block is combined with the contents
|
2020-12-17 06:24:09 -05:00
|
|
|
of the inline blocks. This is used, i.e., when the code is inserted
|
2020-12-14 05:03:31 -05:00
|
|
|
in the text with `` marks.
|
|
|
|
|
|
|
|
Args:
|
2020-12-17 06:24:09 -05:00
|
|
|
block: A block with a inline block in the subtree
|
2020-12-14 05:03:31 -05:00
|
|
|
|
|
|
|
Returns: A dictionary with the extracted information
|
|
|
|
|
|
|
|
"""
|
|
|
|
text = ""
|
2021-05-13 14:05:33 -04:00
|
|
|
for node in block.traverse(
|
|
|
|
include_self=False,
|
|
|
|
condition=lambda x: x.parent.tagname.strip() not in inlines):
|
2020-12-14 05:03:31 -05:00
|
|
|
tagname = node.tagname.strip()
|
2021-05-13 14:05:33 -04:00
|
|
|
if tagname in ('paragraph', ):
|
2020-12-14 05:03:31 -05:00
|
|
|
continue
|
|
|
|
|
|
|
|
if tagname == 'literal':
|
2020-12-17 06:24:09 -05:00
|
|
|
text += f'``{node.astext()}``'
|
|
|
|
elif tagname == 'strong':
|
|
|
|
text += f'**{node.astext()}**'
|
|
|
|
elif tagname == 'reference':
|
|
|
|
text += f'`{node.astext()} <{node["refuri"]}>`_'
|
2020-12-14 05:03:31 -05:00
|
|
|
else:
|
2020-12-17 06:24:09 -05:00
|
|
|
text += node.astext()
|
2020-12-14 05:03:31 -05:00
|
|
|
|
|
|
|
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'
|
2022-08-09 07:27:13 -04:00
|
|
|
ret['uri'] = '../_static/' + os.path.join(*block['uri'].split('/')[3:])
|
2020-12-14 05:03:31 -05:00
|
|
|
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).
|
2020-12-17 06:24:09 -05:00
|
|
|
If the block contain inline block in its subtree, the data from that block
|
2020-12-14 05:03:31 -05:00
|
|
|
will also be extracted.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
block: A note doctree block
|
|
|
|
|
|
|
|
Returns: A dictionary with the extracted information
|
|
|
|
|
|
|
|
"""
|
|
|
|
ret = {}
|
|
|
|
ret['type'] = block.tagname.strip()
|
|
|
|
|
2020-12-17 06:24:09 -05:00
|
|
|
if sum(map(lambda x: subtree_has_tag(block, x), inlines)):
|
2021-05-13 14:05:33 -04:00
|
|
|
for node in block.traverse(
|
|
|
|
condition=lambda x: x.tagname.strip() == 'paragraph'):
|
2020-12-17 06:24:09 -05:00
|
|
|
ret['text'] = handle_default_with_inlines(node)['text']
|
2020-12-14 05:03:31 -05:00
|
|
|
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
|
2020-12-17 06:24:09 -05:00
|
|
|
a special way. Nevertheless, if the block contains a inline block,
|
2020-12-14 05:03:31 -05:00
|
|
|
the informaton from that block will also be extracted.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
block: A doctree block
|
|
|
|
|
|
|
|
Returns: A dictionary with the extracted information
|
|
|
|
|
|
|
|
"""
|
2020-12-17 06:24:09 -05:00
|
|
|
if sum(map(lambda x: subtree_has_tag(block, x), inlines)):
|
|
|
|
return handle_default_with_inlines(block)
|
2020-12-14 05:03:31 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-05-13 14:05:33 -04:00
|
|
|
return doctree.traverse(
|
|
|
|
condition=lambda x: x.tagname.strip() != 'document' and x.parent.
|
|
|
|
tagname.strip() != 'note'
|
|
|
|
)
|
2020-12-14 05:03:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
def fill_context(text):
|
|
|
|
"""
|
2022-02-18 12:15:44 -05:00
|
|
|
Creates a jinja context dictionary for a F4PGA Toolchain usage example.
|
2020-12-14 05:03:31 -05:00
|
|
|
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
|
2020-12-17 06:24:09 -05:00
|
|
|
if tagname in ('#text', 'inline') + inlines:
|
2020-12-14 05:03:31 -05:00
|
|
|
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
|