2015-06-06 20:08:44 +00:00
|
|
|
import json
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
2015-06-10 18:53:09 +00:00
|
|
|
from .base import PythonMapperBase, SphinxMapperBase
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptSphinxMapper(SphinxMapperBase):
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
'''Auto API domain handler for Javascript
|
|
|
|
|
|
|
|
Parses directly from Javascript files.
|
|
|
|
|
|
|
|
:param app: Sphinx application passed in as part of the extension
|
|
|
|
'''
|
|
|
|
|
2015-06-06 23:11:49 +00:00
|
|
|
def read_file(self, path, **kwargs):
|
|
|
|
'''Read file input into memory, returning deserialized objects
|
|
|
|
|
|
|
|
:param path: Path of file to read
|
|
|
|
'''
|
|
|
|
# TODO support JSON here
|
|
|
|
# TODO sphinx way of reporting errors in logs?
|
|
|
|
|
|
|
|
try:
|
|
|
|
parsed_data = json.loads(subprocess.check_output(['jsdoc', '-X', path]))
|
|
|
|
return parsed_data
|
|
|
|
except IOError:
|
2015-06-10 18:01:06 +00:00
|
|
|
self.app.warn('Error reading file: {0}'.format(path))
|
2015-06-06 23:11:49 +00:00
|
|
|
except TypeError:
|
2015-06-10 18:01:06 +00:00
|
|
|
self.app.warn('Error reading file: {0}'.format(path))
|
2015-06-06 23:11:49 +00:00
|
|
|
return None
|
|
|
|
|
2015-06-10 18:36:18 +00:00
|
|
|
# Subclassed to iterate over items
|
2015-06-10 21:23:50 +00:00
|
|
|
def map(self, options=None, **kwargs):
|
2015-06-06 23:11:49 +00:00
|
|
|
'''Trigger find of serialized sources and build objects'''
|
|
|
|
for path, data in self.paths.items():
|
|
|
|
for item in data:
|
2015-06-10 20:58:52 +00:00
|
|
|
for obj in self.create_class(item, options):
|
2015-06-06 23:11:49 +00:00
|
|
|
obj.jinja_env = self.jinja_env
|
|
|
|
self.add_object(obj)
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
def create_class(self, data, options=None, **kwargs):
|
2015-06-06 20:08:44 +00:00
|
|
|
'''Return instance of class based on Javascript data
|
|
|
|
|
|
|
|
Data keys handled here:
|
|
|
|
|
|
|
|
type
|
|
|
|
Set the object class
|
|
|
|
|
|
|
|
consts, types, vars, funcs
|
|
|
|
Recurse into :py:meth:`create_class` to create child object
|
|
|
|
instances
|
|
|
|
|
|
|
|
:param data: dictionary data from godocjson output
|
|
|
|
'''
|
|
|
|
obj_map = dict(
|
|
|
|
(cls.type, cls) for cls
|
|
|
|
in ALL_CLASSES
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
cls = obj_map[data['kind']]
|
2015-06-23 03:25:25 +00:00
|
|
|
except (KeyError, TypeError):
|
2015-06-06 20:08:44 +00:00
|
|
|
self.app.warn('Unknown Type: %s' % data)
|
|
|
|
else:
|
|
|
|
# Recurse for children
|
2015-06-10 18:36:18 +00:00
|
|
|
obj = cls(data, jinja_env=self.jinja_env)
|
2015-06-06 20:08:44 +00:00
|
|
|
if 'children' in data:
|
|
|
|
for child_data in data['children']:
|
2015-06-10 20:58:52 +00:00
|
|
|
for child_obj in self.create_class(child_data, options=options):
|
2015-06-06 20:08:44 +00:00
|
|
|
obj.children.append(child_obj)
|
|
|
|
yield obj
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptPythonMapper(PythonMapperBase):
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
language = 'javascript'
|
|
|
|
|
2015-06-10 18:36:18 +00:00
|
|
|
def __init__(self, obj, **kwargs):
|
2015-06-06 23:11:49 +00:00
|
|
|
'''
|
|
|
|
Map JSON data into Python object.
|
|
|
|
|
|
|
|
This is the standard object that will be rendered into the templates,
|
|
|
|
so we try and keep standard naming to keep templates more re-usable.
|
|
|
|
'''
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
super(JavaScriptPythonMapper, self).__init__(obj, **kwargs)
|
2015-06-06 20:08:44 +00:00
|
|
|
self.name = obj.get('name')
|
|
|
|
self.id = self.name
|
|
|
|
|
|
|
|
# Second level
|
|
|
|
self.docstring = obj.get('description', '')
|
2015-06-06 23:11:49 +00:00
|
|
|
# self.docstring = obj.get('comment', '')
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
self.imports = obj.get('imports', [])
|
|
|
|
self.children = []
|
|
|
|
self.parameters = map(
|
|
|
|
lambda n: {'name': n['name'],
|
|
|
|
'type': n['type'][0]},
|
|
|
|
obj.get('param', [])
|
|
|
|
)
|
|
|
|
|
|
|
|
# Language Specific
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptClass(JavaScriptPythonMapper):
|
2015-06-06 20:08:44 +00:00
|
|
|
type = 'class'
|
|
|
|
ref_directive = 'class'
|
2015-06-10 20:13:34 +00:00
|
|
|
top_level_object = True
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptFunction(JavaScriptPythonMapper):
|
2015-06-06 20:08:44 +00:00
|
|
|
type = 'function'
|
|
|
|
ref_type = 'func'
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptData(JavaScriptPythonMapper):
|
2015-06-06 20:08:44 +00:00
|
|
|
type = 'data'
|
|
|
|
ref_directive = 'data'
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptMember(JavaScriptPythonMapper):
|
2015-06-06 20:08:44 +00:00
|
|
|
type = 'member'
|
|
|
|
ref_directive = 'member'
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptAttribute(JavaScriptPythonMapper):
|
2015-06-06 20:08:44 +00:00
|
|
|
type = 'attribute'
|
|
|
|
ref_directive = 'attr'
|
|
|
|
|
|
|
|
ALL_CLASSES = [
|
|
|
|
JavaScriptFunction,
|
|
|
|
JavaScriptClass,
|
|
|
|
JavaScriptData,
|
|
|
|
JavaScriptAttribute,
|
|
|
|
JavaScriptMember,
|
|
|
|
]
|