2015-08-17 21:04:07 +00:00
|
|
|
import re
|
2015-06-14 20:14:16 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2015-07-20 18:10:52 +00:00
|
|
|
import traceback
|
2015-08-03 17:36:04 +00:00
|
|
|
import shutil
|
2015-08-17 21:04:07 +00:00
|
|
|
from collections import defaultdict
|
2016-08-25 23:24:14 +00:00
|
|
|
import unidecode
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2015-06-22 03:05:03 +00:00
|
|
|
import yaml
|
2015-06-14 20:14:16 +00:00
|
|
|
from sphinx.util.osutil import ensuredir
|
2015-08-03 17:36:04 +00:00
|
|
|
from sphinx.util.console import darkgreen, bold
|
2015-08-03 20:55:33 +00:00
|
|
|
from sphinx.errors import ExtensionError
|
2015-06-14 20:14:16 +00:00
|
|
|
|
2015-06-10 18:53:09 +00:00
|
|
|
from .base import PythonMapperBase, SphinxMapperBase
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-08-18 21:30:08 +00:00
|
|
|
# Doc comment patterns
|
|
|
|
DOC_COMMENT_PATTERN = r'''
|
|
|
|
\<%(tag)s
|
|
|
|
\s+%(attr)s="(?P<attr_value>[^"]*?)"
|
|
|
|
\s*?
|
|
|
|
(?:
|
|
|
|
\/\>|
|
|
|
|
\>(?P<inner>[^\<]*?)\<\/%(tag)s\>
|
|
|
|
)
|
|
|
|
'''
|
|
|
|
DOC_COMMENT_SEE_PATTERN = re.compile(
|
|
|
|
DOC_COMMENT_PATTERN % {'tag': '(?:see|seealso)',
|
|
|
|
'attr': 'cref'},
|
|
|
|
re.X)
|
|
|
|
DOC_COMMENT_PARAM_PATTERN = re.compile(
|
|
|
|
DOC_COMMENT_PATTERN % {'tag': '(?:paramref|typeparamref)',
|
|
|
|
'attr': 'name'},
|
|
|
|
re.X)
|
|
|
|
|
|
|
|
# Comment member identities
|
|
|
|
# From: https://msdn.microsoft.com/en-us/library/vstudio/fsbx0t7x(v=VS.100).aspx
|
|
|
|
DOC_COMMENT_IDENTITIES = {
|
2015-10-27 08:12:25 +00:00
|
|
|
'N': 'dn:ns',
|
|
|
|
'T': 'any', # can be any type (class, delegate, enum, etc), so use any
|
|
|
|
'F': 'dn:field',
|
|
|
|
'P': 'dn:prop',
|
|
|
|
'M': 'dn:meth',
|
|
|
|
'E': 'dn:event',
|
2015-08-18 21:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetSphinxMapper(SphinxMapperBase):
|
2015-04-23 20:31:03 +00:00
|
|
|
|
2015-04-14 22:59:09 +00:00
|
|
|
'''Auto API domain handler for .NET
|
|
|
|
|
|
|
|
Searches for YAML files, and soon to be JSON files as well, for auto API
|
2016-06-03 01:03:54 +00:00
|
|
|
sources. If no pattern configuration was explicitly specified, then default
|
2016-05-02 06:12:30 +00:00
|
|
|
to looking up a ``docfx.json`` file.
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
:param app: Sphinx application passed in as part of the extension
|
|
|
|
'''
|
|
|
|
|
2015-04-24 19:37:00 +00:00
|
|
|
top_namespaces = {}
|
|
|
|
|
2016-05-02 06:12:30 +00:00
|
|
|
DOCFX_OUTPUT_PATH = '_api'
|
|
|
|
|
2017-11-05 23:29:39 +00:00
|
|
|
# pylint: disable=arguments-differ
|
2015-09-23 23:00:43 +00:00
|
|
|
def load(self, patterns, dirs, ignore=None, **kwargs):
|
2016-05-02 06:12:30 +00:00
|
|
|
'''Load objects from the filesystem into the ``paths`` dictionary.
|
2015-06-14 20:14:16 +00:00
|
|
|
|
2016-06-03 01:03:54 +00:00
|
|
|
If the setting ``autoapi_patterns`` was not specified, look for a
|
|
|
|
``docfx.json`` file by default. A ``docfx.json`` should be treated as
|
|
|
|
the canonical source before the default patterns. Fallback to default
|
|
|
|
pattern matches if no ``docfx.json`` files are found.
|
2015-06-14 20:14:16 +00:00
|
|
|
'''
|
2015-08-03 21:13:08 +00:00
|
|
|
raise_error = kwargs.get('raise_error', True)
|
2015-08-03 22:05:33 +00:00
|
|
|
all_files = set()
|
2016-05-02 06:12:30 +00:00
|
|
|
if not self.app.config.autoapi_file_patterns:
|
|
|
|
all_files = set()
|
|
|
|
for _file in self.find_files(patterns=['docfx.json'], dirs=dirs,
|
|
|
|
ignore=ignore):
|
|
|
|
all_files.add(_file)
|
|
|
|
if not all_files:
|
|
|
|
for _file in self.find_files(patterns=patterns, dirs=dirs,
|
|
|
|
ignore=ignore):
|
|
|
|
all_files.add(_file)
|
2015-08-03 21:13:08 +00:00
|
|
|
if all_files:
|
|
|
|
try:
|
2015-08-21 08:13:08 +00:00
|
|
|
command = ['docfx', 'metadata', '--raw', '--force']
|
2015-08-03 21:13:08 +00:00
|
|
|
command.extend(all_files)
|
|
|
|
proc = subprocess.Popen(
|
2015-08-14 21:58:13 +00:00
|
|
|
' '.join(command),
|
2015-08-03 21:13:08 +00:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
2015-08-21 08:13:08 +00:00
|
|
|
shell=True,
|
2015-08-03 21:13:08 +00:00
|
|
|
env=dict((key, os.environ[key])
|
2015-08-21 08:13:08 +00:00
|
|
|
for key in ['PATH', 'HOME', 'SYSTEMROOT',
|
|
|
|
'USERPROFILE', 'WINDIR']
|
2015-08-03 21:13:08 +00:00
|
|
|
if key in os.environ),
|
|
|
|
)
|
|
|
|
_, error_output = proc.communicate()
|
|
|
|
if error_output:
|
|
|
|
self.app.warn(error_output)
|
|
|
|
except (OSError, subprocess.CalledProcessError) as e:
|
|
|
|
self.app.warn('Error generating metadata: {0}'.format(e))
|
|
|
|
if raise_error:
|
|
|
|
raise ExtensionError('Failure in docfx while generating AutoAPI output.')
|
2015-06-14 20:14:16 +00:00
|
|
|
# We now have yaml files
|
2016-05-02 06:12:30 +00:00
|
|
|
for xdoc_path in self.find_files(patterns=['*.yml'],
|
|
|
|
dirs=[self.DOCFX_OUTPUT_PATH],
|
|
|
|
ignore=ignore):
|
2015-06-14 20:14:16 +00:00
|
|
|
data = self.read_file(path=xdoc_path)
|
|
|
|
if data:
|
|
|
|
self.paths[xdoc_path] = data
|
|
|
|
|
2015-06-10 18:48:15 +00:00
|
|
|
def read_file(self, path, **kwargs):
|
|
|
|
'''Read file input into memory, returning deserialized objects
|
|
|
|
|
|
|
|
:param path: Path of file to read
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
with open(path, 'r') as handle:
|
|
|
|
parsed_data = yaml.safe_load(handle)
|
|
|
|
return parsed_data
|
2015-06-22 02:03:14 +00:00
|
|
|
except IOError:
|
2015-06-10 18:48:15 +00:00
|
|
|
self.app.warn('Error reading file: {0}'.format(path))
|
|
|
|
except TypeError:
|
|
|
|
self.app.warn('Error reading file: {0}'.format(path))
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Subclassed to iterate over items
|
2017-11-05 23:29:39 +00:00
|
|
|
def map(self, options=None):
|
2015-06-10 18:48:15 +00:00
|
|
|
'''Trigger find of serialized sources and build objects'''
|
|
|
|
for path, data in self.paths.items():
|
2016-02-26 23:13:05 +00:00
|
|
|
references = data.get('references', [])
|
2015-06-10 18:48:15 +00:00
|
|
|
for item in data['items']:
|
2016-02-25 22:35:09 +00:00
|
|
|
for obj in self.create_class(item, options,
|
|
|
|
references=references):
|
2015-06-10 18:48:15 +00:00
|
|
|
self.add_object(obj)
|
|
|
|
|
|
|
|
self.organize_objects()
|
|
|
|
|
2017-11-05 23:29:39 +00:00
|
|
|
def create_class(self, data, options=None, path=None, **kwargs):
|
2015-06-10 18:48:15 +00:00
|
|
|
'''
|
|
|
|
Return instance of class based on Roslyn type property
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
Data keys handled here:
|
|
|
|
|
|
|
|
type
|
|
|
|
Set the object class
|
|
|
|
|
|
|
|
items
|
|
|
|
Recurse into :py:meth:`create_class` to create child object
|
|
|
|
instances
|
|
|
|
|
|
|
|
:param data: dictionary data from Roslyn output artifact
|
|
|
|
'''
|
2015-06-10 18:48:15 +00:00
|
|
|
|
2015-05-31 05:03:19 +00:00
|
|
|
obj_map = dict(
|
|
|
|
(cls.type, cls) for cls
|
2015-06-10 18:48:15 +00:00
|
|
|
in ALL_CLASSES
|
|
|
|
)
|
2015-05-31 05:03:19 +00:00
|
|
|
try:
|
2015-06-01 18:20:54 +00:00
|
|
|
cls = obj_map[data['type'].lower()]
|
2015-05-31 05:03:19 +00:00
|
|
|
except KeyError:
|
2015-06-01 18:20:54 +00:00
|
|
|
self.app.warn('Unknown type: %s' % data)
|
2015-05-31 05:03:19 +00:00
|
|
|
else:
|
2015-10-27 18:13:08 +00:00
|
|
|
obj = cls(data, jinja_env=self.jinja_env, options=options,
|
2016-02-25 22:35:09 +00:00
|
|
|
url_root=self.url_root, **kwargs)
|
2015-05-31 05:03:19 +00:00
|
|
|
|
|
|
|
# Append child objects
|
|
|
|
# TODO this should recurse in the case we're getting back more
|
|
|
|
# complex argument listings
|
|
|
|
|
|
|
|
yield obj
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
def add_object(self, obj):
|
|
|
|
'''Add object to local and app environment storage
|
|
|
|
|
|
|
|
:param obj: Instance of a .NET object
|
|
|
|
'''
|
2015-06-01 18:20:54 +00:00
|
|
|
if obj.top_level_object:
|
2015-08-03 18:24:52 +00:00
|
|
|
if isinstance(obj, DotNetNamespace):
|
2015-04-24 19:37:00 +00:00
|
|
|
self.namespaces[obj.name] = obj
|
2015-04-25 01:32:16 +00:00
|
|
|
self.objects[obj.id] = obj
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
def organize_objects(self):
|
|
|
|
'''Organize objects and namespaces'''
|
|
|
|
|
2015-04-23 20:31:03 +00:00
|
|
|
def _render_children(obj):
|
|
|
|
for child in obj.children_strings:
|
|
|
|
child_object = self.objects.get(child)
|
|
|
|
if child_object:
|
2015-04-24 19:37:00 +00:00
|
|
|
obj.item_map[child_object.plural].append(child_object)
|
2015-04-23 20:31:03 +00:00
|
|
|
obj.children.append(child_object)
|
2015-06-14 20:14:16 +00:00
|
|
|
|
2015-04-23 20:31:03 +00:00
|
|
|
for key in obj.item_map:
|
|
|
|
obj.item_map[key].sort()
|
|
|
|
|
2015-04-14 22:59:09 +00:00
|
|
|
def _recurse_ns(obj):
|
2015-04-21 05:54:32 +00:00
|
|
|
if not obj:
|
|
|
|
return
|
2015-04-24 19:37:00 +00:00
|
|
|
namespace = obj.top_namespace
|
2015-04-14 22:59:09 +00:00
|
|
|
if namespace is not None:
|
2015-04-24 19:37:00 +00:00
|
|
|
ns_obj = self.top_namespaces.get(namespace)
|
2015-08-03 18:24:52 +00:00
|
|
|
if ns_obj is None or not isinstance(ns_obj, DotNetNamespace):
|
2015-06-14 20:14:16 +00:00
|
|
|
for ns_obj in self.create_class({'uid': namespace,
|
2015-06-01 18:20:54 +00:00
|
|
|
'type': 'namespace'}):
|
|
|
|
self.top_namespaces[ns_obj.id] = ns_obj
|
2015-04-24 19:37:00 +00:00
|
|
|
if obj not in ns_obj.children and namespace != obj.id:
|
2015-04-14 22:59:09 +00:00
|
|
|
ns_obj.children.append(obj)
|
|
|
|
|
2015-06-22 02:03:14 +00:00
|
|
|
for obj in self.objects.values():
|
2015-04-23 20:31:03 +00:00
|
|
|
_render_children(obj)
|
2015-04-14 22:59:09 +00:00
|
|
|
_recurse_ns(obj)
|
|
|
|
|
2015-04-24 19:37:00 +00:00
|
|
|
# Clean out dead namespaces
|
2015-08-03 22:05:33 +00:00
|
|
|
for key, ns in self.top_namespaces.copy().items():
|
2017-11-05 23:29:39 +00:00
|
|
|
if not ns.children:
|
2015-04-24 19:37:00 +00:00
|
|
|
del self.top_namespaces[key]
|
|
|
|
|
|
|
|
for key, ns in self.namespaces.items():
|
2017-11-05 23:29:39 +00:00
|
|
|
if not ns.children:
|
2015-04-24 19:37:00 +00:00
|
|
|
del self.namespaces[key]
|
|
|
|
|
2015-06-14 20:14:16 +00:00
|
|
|
def output_rst(self, root, source_suffix):
|
2017-11-05 23:29:39 +00:00
|
|
|
if not self.objects:
|
2015-08-14 21:54:28 +00:00
|
|
|
raise ExtensionError("No API objects exist. Can't continue")
|
2015-06-14 20:14:16 +00:00
|
|
|
for id, obj in self.objects.items():
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2015-06-14 20:14:16 +00:00
|
|
|
if not obj or not obj.top_level_object:
|
|
|
|
continue
|
2015-04-22 22:46:40 +00:00
|
|
|
|
2015-06-14 20:14:16 +00:00
|
|
|
rst = obj.render()
|
|
|
|
if not rst:
|
|
|
|
continue
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2015-08-19 01:10:25 +00:00
|
|
|
detail_dir = os.path.join(root, obj.pathname)
|
2015-06-14 20:14:16 +00:00
|
|
|
ensuredir(detail_dir)
|
|
|
|
path = os.path.join(detail_dir, '%s%s' % ('index', source_suffix))
|
2015-08-11 03:16:40 +00:00
|
|
|
with open(path, 'wb') as detail_file:
|
2015-06-14 20:14:16 +00:00
|
|
|
detail_file.write(rst.encode('utf-8'))
|
|
|
|
|
|
|
|
# Render Top Index
|
|
|
|
top_level_index = os.path.join(root, 'index.rst')
|
2015-08-11 03:16:40 +00:00
|
|
|
with open(top_level_index, 'wb') as top_level_file:
|
2015-06-14 20:14:16 +00:00
|
|
|
content = self.jinja_env.get_template('index.rst')
|
2015-08-03 22:05:33 +00:00
|
|
|
top_level_file.write(content.render(pages=self.namespaces.values()).encode('utf-8'))
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2015-08-03 17:36:04 +00:00
|
|
|
@staticmethod
|
|
|
|
def build_finished(app, exception):
|
|
|
|
if app.verbosity > 1:
|
|
|
|
app.info(bold('[AutoAPI] ') + darkgreen('Cleaning generated .yml files'))
|
2016-05-02 06:12:30 +00:00
|
|
|
if os.path.exists(DotNetSphinxMapper.DOCFX_OUTPUT_PATH):
|
|
|
|
shutil.rmtree(DotNetSphinxMapper.DOCFX_OUTPUT_PATH)
|
2015-08-03 17:36:04 +00:00
|
|
|
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetPythonMapper(PythonMapperBase):
|
2015-04-23 20:31:03 +00:00
|
|
|
|
2016-02-25 22:35:09 +00:00
|
|
|
"""Base .NET object representation
|
|
|
|
|
|
|
|
:param references: object reference list from docfx
|
|
|
|
:type references: list of dict objects
|
|
|
|
"""
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
language = 'dotnet'
|
|
|
|
|
2015-06-10 18:48:15 +00:00
|
|
|
def __init__(self, obj, **kwargs):
|
2016-02-25 22:35:09 +00:00
|
|
|
self.references = dict((obj.get('uid'), obj)
|
|
|
|
for obj in kwargs.pop('references', [])
|
|
|
|
if 'uid' in obj)
|
2015-06-10 21:23:50 +00:00
|
|
|
super(DotNetPythonMapper, self).__init__(obj, **kwargs)
|
2015-06-10 18:48:15 +00:00
|
|
|
|
2015-04-14 22:59:09 +00:00
|
|
|
# Always exist
|
2015-06-14 20:14:16 +00:00
|
|
|
self.id = obj.get('uid', obj.get('id'))
|
2016-03-04 18:05:21 +00:00
|
|
|
self.definition = obj.get('definition', self.id)
|
|
|
|
self.name = obj.get('fullName', self.definition)
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
# Optional
|
2015-04-24 20:16:15 +00:00
|
|
|
self.fullname = obj.get('fullName')
|
2015-08-17 21:04:07 +00:00
|
|
|
self.summary = self.transform_doc_comments(obj.get('summary', ''))
|
2015-04-14 22:59:09 +00:00
|
|
|
self.parameters = []
|
2015-04-14 23:54:43 +00:00
|
|
|
self.items = obj.get('items', [])
|
2015-04-23 20:31:03 +00:00
|
|
|
self.children_strings = obj.get('children', [])
|
2015-04-14 22:59:09 +00:00
|
|
|
self.children = []
|
2015-04-14 23:54:43 +00:00
|
|
|
self.item_map = defaultdict(list)
|
2015-04-15 06:35:36 +00:00
|
|
|
self.inheritance = []
|
2016-05-02 02:57:58 +00:00
|
|
|
self.assemblies = obj.get('assemblies', [])
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
# Syntax example and parameter list
|
|
|
|
syntax = obj.get('syntax', None)
|
|
|
|
self.example = ''
|
|
|
|
if syntax is not None:
|
|
|
|
# Code example
|
|
|
|
try:
|
2015-07-20 18:10:52 +00:00
|
|
|
self.example = syntax['content']
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
traceback.print_exc()
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
self.parameters = []
|
|
|
|
for param in syntax.get('parameters', []):
|
|
|
|
if 'id' in param:
|
|
|
|
self.parameters.append({
|
|
|
|
'name': param.get('id'),
|
2016-02-25 22:35:09 +00:00
|
|
|
'type': self.resolve_spec_identifier(param.get('type')),
|
2015-08-17 21:04:07 +00:00
|
|
|
'desc': self.transform_doc_comments(
|
|
|
|
param.get('description', ''))
|
2015-04-14 22:59:09 +00:00
|
|
|
})
|
|
|
|
|
2016-02-24 19:44:15 +00:00
|
|
|
self.returns = {}
|
2016-02-25 22:35:09 +00:00
|
|
|
self.returns['type'] = self.resolve_spec_identifier(
|
|
|
|
syntax.get('return', {}).get('type')
|
|
|
|
)
|
2016-02-24 19:44:15 +00:00
|
|
|
self.returns['description'] = self.transform_doc_comments(
|
|
|
|
syntax.get('return', {}).get('description'))
|
2015-04-15 07:10:50 +00:00
|
|
|
|
2015-04-15 06:35:36 +00:00
|
|
|
# Inheritance
|
|
|
|
# TODO Support more than just a class type here, should support enum/etc
|
2015-06-14 20:14:16 +00:00
|
|
|
self.inheritance = [DotNetClass({'uid': name, 'name': name})
|
|
|
|
for name in obj.get('inheritance', [])]
|
2015-04-15 06:35:36 +00:00
|
|
|
|
2015-04-14 22:59:09 +00:00
|
|
|
def __str__(self):
|
|
|
|
return '<{cls} {id}>'.format(cls=self.__class__.__name__,
|
|
|
|
id=self.id)
|
|
|
|
|
2016-08-25 23:24:14 +00:00
|
|
|
@property
|
|
|
|
def pathname(self):
|
|
|
|
'''Sluggified path for filenames
|
|
|
|
|
|
|
|
Slugs to a filename using the follow steps
|
|
|
|
|
|
|
|
* Decode unicode to approximate ascii
|
|
|
|
* Remove existing hypens
|
|
|
|
* Substitute hyphens for non-word characters
|
|
|
|
* Break up the string as paths
|
|
|
|
'''
|
|
|
|
slug = self.name
|
|
|
|
try:
|
|
|
|
slug = self.name.split('(')[0]
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
slug = unidecode.unidecode(slug)
|
|
|
|
slug = slug.replace('-', '')
|
|
|
|
slug = re.sub(r'[^\w\.]+', '-', slug).strip('-')
|
|
|
|
return os.path.join(*slug.split('.'))
|
|
|
|
|
2015-04-25 01:32:16 +00:00
|
|
|
@property
|
|
|
|
def short_name(self):
|
|
|
|
'''Shorten name property'''
|
|
|
|
return self.name.split('.')[-1]
|
|
|
|
|
2015-04-24 20:16:15 +00:00
|
|
|
@property
|
|
|
|
def edit_link(self):
|
|
|
|
try:
|
|
|
|
repo = self.source['remote']['repo'].replace('.git', '')
|
|
|
|
path = self.path
|
|
|
|
return '{repo}/blob/master/{path}'.format(
|
|
|
|
repo=repo,
|
|
|
|
path=path,
|
2015-06-10 18:48:15 +00:00
|
|
|
)
|
2015-04-24 20:16:15 +00:00
|
|
|
except:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
return self.obj.get('source')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self.source['path']
|
|
|
|
|
2015-04-14 23:54:43 +00:00
|
|
|
@property
|
2015-04-14 22:59:09 +00:00
|
|
|
def namespace(self):
|
|
|
|
pieces = self.id.split('.')[:-1]
|
|
|
|
if pieces:
|
|
|
|
return '.'.join(pieces)
|
|
|
|
|
2015-04-24 19:37:00 +00:00
|
|
|
@property
|
|
|
|
def top_namespace(self):
|
|
|
|
pieces = self.id.split('.')[:2]
|
|
|
|
if pieces:
|
|
|
|
return '.'.join(pieces)
|
|
|
|
|
2015-04-15 01:05:15 +00:00
|
|
|
@property
|
|
|
|
def ref_type(self):
|
|
|
|
return self.type
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ref_directive(self):
|
|
|
|
return self.type
|
|
|
|
|
2015-04-25 01:32:16 +00:00
|
|
|
@property
|
|
|
|
def ref_name(self):
|
|
|
|
'''Return object name suitable for use in references
|
|
|
|
|
|
|
|
Escapes several known strings that cause problems, including the
|
|
|
|
following reference syntax::
|
|
|
|
|
|
|
|
:dotnet:cls:`Foo.Bar<T>`
|
|
|
|
|
|
|
|
As the `<T>` notation is also special syntax in references, indicating
|
|
|
|
the reference to Foo.Bar should be named T.
|
|
|
|
|
|
|
|
See: http://sphinx-doc.org/domains.html#role-cpp:any
|
|
|
|
'''
|
|
|
|
return (self.name
|
|
|
|
.replace('<', '\<')
|
|
|
|
.replace('`', '\`'))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ref_short_name(self):
|
|
|
|
'''Same as above, return the truncated name instead'''
|
|
|
|
return self.ref_name.split('.')[-1]
|
|
|
|
|
2015-08-17 21:04:07 +00:00
|
|
|
@staticmethod
|
|
|
|
def transform_doc_comments(text):
|
2015-08-17 20:30:55 +00:00
|
|
|
"""
|
|
|
|
Parse XML content for references and other syntax.
|
2015-08-17 21:04:07 +00:00
|
|
|
|
|
|
|
This avoids an LXML dependency, we only need to parse out a small subset
|
|
|
|
of elements here. Iterate over string to reduce regex pattern complexity
|
|
|
|
and make substitutions easier
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
`Doc comment reference <https://msdn.microsoft.com/en-us/library/5ast78ax.aspx>`
|
|
|
|
Reference on XML documentation comment syntax
|
2015-08-17 20:30:55 +00:00
|
|
|
"""
|
2015-08-17 21:04:07 +00:00
|
|
|
try:
|
2015-08-18 21:30:08 +00:00
|
|
|
while True:
|
|
|
|
found = DOC_COMMENT_SEE_PATTERN.search(text)
|
|
|
|
if found is None:
|
|
|
|
break
|
2015-10-27 07:46:59 +00:00
|
|
|
ref = (found.group('attr_value')
|
|
|
|
.replace('<', '\<')
|
|
|
|
.replace('`', '\`'))
|
|
|
|
|
2015-10-27 08:12:25 +00:00
|
|
|
reftype = 'any'
|
2015-08-18 21:30:08 +00:00
|
|
|
replacement = ''
|
|
|
|
# Given the pattern of `\w:\w+`, inspect first letter of
|
|
|
|
# reference for identity type
|
|
|
|
if ref[1] == ':' and ref[0] in DOC_COMMENT_IDENTITIES:
|
|
|
|
reftype = DOC_COMMENT_IDENTITIES[ref[:1]]
|
|
|
|
ref = ref[2:]
|
2015-10-27 08:12:25 +00:00
|
|
|
replacement = ':{reftype}:`{ref}`'.format(
|
2015-08-18 21:30:08 +00:00
|
|
|
reftype=reftype, ref=ref)
|
|
|
|
elif ref[:2] == '!:':
|
|
|
|
replacement = ref[2:]
|
|
|
|
else:
|
2015-10-27 08:12:25 +00:00
|
|
|
replacement = ':any:`{ref}`'.format(ref=ref)
|
2015-08-18 21:30:08 +00:00
|
|
|
|
2015-10-27 07:46:59 +00:00
|
|
|
# Escape following text
|
|
|
|
text_end = text[found.end():]
|
|
|
|
text_start = text[:found.start()]
|
|
|
|
text_end = re.sub(r'^(\S)', r'\\\1', text_end)
|
|
|
|
text_start = re.sub(r'(\S)$', r'\1 ', text_start)
|
|
|
|
|
|
|
|
text = ''.join([text_start, replacement, text_end])
|
2016-02-24 19:44:15 +00:00
|
|
|
while True:
|
|
|
|
found = DOC_COMMENT_PARAM_PATTERN.search(text)
|
|
|
|
if found is None:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Escape following text
|
|
|
|
text_end = text[found.end():]
|
|
|
|
text_start = text[:found.start()]
|
|
|
|
text_end = re.sub(r'^(\S)', r'\\\1', text_end)
|
|
|
|
text_start = re.sub(r'(\S)$', r'\1 ', text_start)
|
|
|
|
|
|
|
|
text = ''.join([text_start, '``', found.group('attr_value'),
|
|
|
|
'``', text_end])
|
2015-08-17 21:04:07 +00:00
|
|
|
except TypeError:
|
|
|
|
pass
|
2015-08-17 20:30:55 +00:00
|
|
|
return text
|
|
|
|
|
2016-02-25 22:35:09 +00:00
|
|
|
def resolve_spec_identifier(self, obj_name):
|
|
|
|
"""Find reference name based on spec identifier
|
|
|
|
|
|
|
|
Spec identifiers are used in parameter and return type definitions, but
|
|
|
|
should be a user-friendly version instead. Use docfx ``references``
|
|
|
|
lookup mapping for resolution.
|
2016-03-04 18:05:21 +00:00
|
|
|
|
|
|
|
If the spec identifier reference has a ``spec.csharp`` key, this implies
|
|
|
|
a compound reference that should be linked in a special way. Resolve to
|
|
|
|
a nested reference, with the corrected nodes.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
This uses a special format that is interpreted by the domain for
|
|
|
|
parameter type and return type fields.
|
|
|
|
|
|
|
|
:param obj_name: spec identifier to resolve to a correct reference
|
|
|
|
:returns: resolved string with one or more references
|
|
|
|
:rtype: str
|
2016-02-25 22:35:09 +00:00
|
|
|
"""
|
2016-03-04 18:05:21 +00:00
|
|
|
ref = self.references.get(obj_name)
|
|
|
|
if ref is None:
|
|
|
|
return obj_name
|
|
|
|
|
|
|
|
resolved = ref.get('fullName', obj_name)
|
|
|
|
spec = ref.get('spec.csharp', [])
|
|
|
|
parts = []
|
|
|
|
for part in spec:
|
|
|
|
if part.get('name') == '<':
|
|
|
|
parts.append('{')
|
|
|
|
elif part.get('name') == '>':
|
|
|
|
parts.append('}')
|
|
|
|
elif 'fullName' in part and 'uid' in part:
|
|
|
|
parts.append('{fullName}<{uid}>'.format(**part))
|
|
|
|
elif 'uid' in part:
|
|
|
|
parts.append(part['uid'])
|
2016-03-04 18:49:01 +00:00
|
|
|
elif 'fullName' in part:
|
2016-03-04 18:05:21 +00:00
|
|
|
parts.append(part['fullName'])
|
|
|
|
if parts:
|
|
|
|
resolved = ''.join(parts)
|
|
|
|
return resolved
|
2016-02-25 22:35:09 +00:00
|
|
|
|
2015-04-14 22:59:09 +00:00
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetNamespace(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'namespace'
|
2015-04-15 01:05:15 +00:00
|
|
|
ref_directive = 'ns'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'namespaces'
|
2015-06-01 18:20:54 +00:00
|
|
|
top_level_object = True
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetMethod(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'method'
|
2015-04-15 01:05:15 +00:00
|
|
|
ref_directive = 'meth'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'methods'
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2016-05-02 06:12:30 +00:00
|
|
|
class DotNetOperator(DotNetPythonMapper):
|
|
|
|
type = 'operator'
|
|
|
|
ref_directive = 'op'
|
|
|
|
plural = 'operators'
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetProperty(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'property'
|
2015-04-15 01:05:15 +00:00
|
|
|
ref_directive = 'prop'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'properties'
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetEnum(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'enum'
|
2015-04-15 01:05:15 +00:00
|
|
|
ref_type = 'enumeration'
|
|
|
|
ref_directive = 'enum'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'enumerations'
|
2015-06-01 18:20:54 +00:00
|
|
|
top_level_object = True
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetStruct(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'struct'
|
2015-04-15 01:05:15 +00:00
|
|
|
ref_type = 'structure'
|
|
|
|
ref_directive = 'struct'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'structures'
|
2015-06-01 18:20:54 +00:00
|
|
|
top_level_object = True
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetConstructor(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'constructor'
|
2015-04-15 01:05:15 +00:00
|
|
|
ref_directive = 'ctor'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'constructors'
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetInterface(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'interface'
|
2015-04-15 01:05:15 +00:00
|
|
|
ref_directive = 'iface'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'interfaces'
|
2015-06-01 18:20:54 +00:00
|
|
|
top_level_object = True
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetDelegate(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'delegate'
|
2015-04-15 01:05:15 +00:00
|
|
|
ref_directive = 'del'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'delegates'
|
2015-06-01 18:20:54 +00:00
|
|
|
top_level_object = True
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetClass(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'class'
|
2015-04-15 01:05:15 +00:00
|
|
|
ref_directive = 'cls'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'classes'
|
2015-06-01 18:20:54 +00:00
|
|
|
top_level_object = True
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetField(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'field'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'fields'
|
2015-04-14 22:59:09 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class DotNetEvent(DotNetPythonMapper):
|
2015-04-14 22:59:09 +00:00
|
|
|
type = 'event'
|
2015-04-24 19:37:00 +00:00
|
|
|
plural = 'events'
|
2015-06-10 18:48:15 +00:00
|
|
|
|
2015-08-17 21:04:07 +00:00
|
|
|
|
2015-06-10 18:48:15 +00:00
|
|
|
ALL_CLASSES = [
|
|
|
|
DotNetNamespace, DotNetClass, DotNetEnum, DotNetStruct,
|
2016-05-02 06:12:30 +00:00
|
|
|
DotNetInterface, DotNetDelegate, DotNetOperator, DotNetProperty,
|
|
|
|
DotNetMethod, DotNetConstructor, DotNetField, DotNetEvent
|
2015-06-10 18:48:15 +00:00
|
|
|
]
|