Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Fix Bug: Resolve ref for combined type (#63)
Browse files Browse the repository at this point in the history
* Fix Bug: Resolve ref for combined type

* Update extension.py
  • Loading branch information
yannanwang1 authored and bianliu1013 committed Oct 24, 2017
1 parent 53068a8 commit 94b91ba
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
11 changes: 10 additions & 1 deletion docfx_yaml/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ def convert_module_to_package_if_needed(obj):
if 'exceptions' in obj['syntax'] and obj['syntax']['exceptions']:
obj['exceptions'] = obj['syntax'].pop('exceptions')

# Raise up references
if 'references' in obj['syntax'] and obj['syntax']['references']:
obj.setdefault('references', []).extend(obj['syntax'].pop('references'))

# add content of temp list 'added_attribute' to children and yaml_data
if 'added_attribute' in obj['syntax'] and obj['syntax']['added_attribute']:
added_attribute = obj['syntax'].pop('added_attribute')
Expand All @@ -534,7 +538,12 @@ def convert_module_to_package_if_needed(obj):
obj['references'].append(_create_reference(attrData, parent))

if 'references' in obj:
references.extend(obj.pop('references'))
# Ensure that references have no duplicate ref
ref_uids = [r['uid'] for r in references]
for ref_obj in obj['references']:
if ref_obj['uid'] not in ref_uids:
references.append(ref_obj)
obj.pop('references')

if obj['type'] == 'module':
convert_module_to_package_if_needed(obj)
Expand Down
54 changes: 46 additions & 8 deletions docfx_yaml/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sphinx.addnodes import desc, desc_signature
from .utils import transform_node as _transform_node

TYPE_SEP_PATTERN = '(\[|\]|, |\(|\))'

def _get_desc_data(node):
assert node.tagname == 'desc'
Expand Down Expand Up @@ -149,6 +150,7 @@ def get_data_structure(entries, types, field_object):
'variables': [],
'exceptions': [],
'return': {},
'references': [],
}

def make_param(_id, _description, _type=None):
Expand All @@ -165,6 +167,36 @@ def transform_para(para_field):
return transform_node(para_field)
else:
return para_field.astext()

def resolve_type(data_type):
# Remove @ ~ and \n for cross reference in parameter/return value type to apply to docfx correctly
data_type = re.sub('[@~\n]', '', data_type)

# Add references for docfx to resolve ref if type contains TYPE_SEP_PATTERN
_spec_list = []
_spec_fullnames = re.split(TYPE_SEP_PATTERN, data_type)

_added_reference = {}
if len(_spec_fullnames) > 1:
_added_reference_name = ''
for _spec_fullname in _spec_fullnames:
if _spec_fullname != '':
_spec = {}
_spec['name'] = _spec_fullname.split('.')[-1]
_spec['fullName'] = _spec_fullname
if re.match(TYPE_SEP_PATTERN, _spec_fullname) is None:
_spec['uid'] = _spec_fullname
_spec_list.append(_spec)
_added_reference_name += _spec['name']

_added_reference = {
'uid': data_type,
'name': _added_reference_name,
'fullName': data_type,
'spec.python': _spec_list
}

return data_type, _added_reference

def extract_exception_desc(field_object):
ret = []
Expand Down Expand Up @@ -208,10 +240,14 @@ def extract_exception_desc(field_object):
if returntype_ret:
# Support or in returntype
for returntype in re.split(' or[ \n]', returntype_ret):
# Remove @ ~ and \n for cross reference in return type to apply to docfx correctly
if returntype.startswith('@') or returntype.startswith('~'):
returntype = returntype[1:]
data['return'].setdefault('type', []).append(returntype.rstrip('\n'))
returntype, _added_reference = resolve_type(returntype)
if _added_reference:
if len(data['references']) == 0:
data['references'].append(_added_reference)
elif any(r['uid'] != _added_reference['uid'] for r in data['references']):
data['references'].append(_added_reference)

data['return'].setdefault('type', []).append(returntype)
if fieldtype.name == 'returnvalue':
returnvalue_ret = transform_node(content[1][0])
if returnvalue_ret:
Expand All @@ -230,10 +266,12 @@ def extract_exception_desc(field_object):
if _type:
# Support or in parameter type
for _s_type in re.split(' or[ \n]', _type):
# Remove @ ~ and \n for cross reference in parameter type to apply to docfx correctly
if _s_type and (_s_type.startswith('@') or _s_type.startswith('~')):
_s_type = _s_type[1:]
_s_type = _s_type.rstrip('\n')
_s_type, _added_reference = resolve_type(_s_type)
if _added_reference:
if len(data['references']) == 0:
data['references'].append(_added_reference)
elif any(r['uid'] != _added_reference['uid'] for r in data['references']):
data['references'].append(_added_reference)

_para_types.append(_s_type)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name='sphinx-docfx-yaml',
version='1.2.23',
version='1.2.24',
author='Eric Holscher',
author_email='eric@ericholscher.com',
url='https://github.com/ericholscher/sphinx-docfx-yaml',
Expand Down

0 comments on commit 94b91ba

Please sign in to comment.