2015-06-06 20:08:44 +00:00
|
|
|
import json
|
|
|
|
import subprocess
|
2017-11-12 21:24:21 +00:00
|
|
|
import os
|
2015-06-06 20:08:44 +00:00
|
|
|
|
2019-01-27 01:11:12 +00:00
|
|
|
import sphinx.util.logging
|
|
|
|
|
2015-06-10 18:53:09 +00:00
|
|
|
from .base import PythonMapperBase, SphinxMapperBase
|
2015-06-06 20:08:44 +00:00
|
|
|
|
2019-01-27 01:11:12 +00:00
|
|
|
LOGGER = sphinx.util.logging.getLogger(__name__)
|
|
|
|
|
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
|
|
|
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Auto API domain handler for Javascript
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
Parses directly from Javascript files.
|
|
|
|
|
|
|
|
:param app: Sphinx application passed in as part of the extension
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
2015-06-06 20:08:44 +00:00
|
|
|
|
2015-06-06 23:11:49 +00:00
|
|
|
def read_file(self, path, **kwargs):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Read file input into memory, returning deserialized objects
|
2015-06-06 23:11:49 +00:00
|
|
|
|
|
|
|
:param path: Path of file to read
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
2015-06-06 23:11:49 +00:00
|
|
|
# TODO support JSON here
|
|
|
|
# TODO sphinx way of reporting errors in logs?
|
2019-01-27 05:20:45 +00:00
|
|
|
subcmd = "jsdoc"
|
|
|
|
if os.name == "nt":
|
|
|
|
subcmd = ".".join([subcmd, "cmd"])
|
2015-06-06 23:11:49 +00:00
|
|
|
|
|
|
|
try:
|
2019-01-27 05:20:45 +00:00
|
|
|
parsed_data = json.loads(subprocess.check_output([subcmd, "-X", path]))
|
2015-06-06 23:11:49 +00:00
|
|
|
return parsed_data
|
|
|
|
except IOError:
|
2019-01-27 05:20:45 +00:00
|
|
|
LOGGER.warning("Error reading file: {0}".format(path))
|
2015-06-06 23:11:49 +00:00
|
|
|
except TypeError:
|
2019-01-27 05:20:45 +00:00
|
|
|
LOGGER.warning("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
|
2017-11-05 23:29:39 +00:00
|
|
|
def map(self, options=None):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Trigger find of serialized sources and build objects"""
|
2015-06-06 23:11:49 +00:00
|
|
|
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)
|
|
|
|
|
2017-11-09 17:56:42 +00:00
|
|
|
def create_class(self, data, options=None, **kwargs):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""Return instance of class based on Javascript data
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
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
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
|
|
|
obj_map = dict((cls.type, cls) for cls in ALL_CLASSES)
|
2015-06-06 20:08:44 +00:00
|
|
|
try:
|
2019-01-27 05:20:45 +00:00
|
|
|
cls = obj_map[data["kind"]]
|
2015-06-23 03:25:25 +00:00
|
|
|
except (KeyError, TypeError):
|
2019-01-27 05:20:45 +00:00
|
|
|
LOGGER.warning("Unknown Type: %s" % data)
|
2015-06-06 20:08:44 +00:00
|
|
|
else:
|
|
|
|
# Recurse for children
|
2015-06-10 18:36:18 +00:00
|
|
|
obj = cls(data, jinja_env=self.jinja_env)
|
2019-01-27 05:20:45 +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
|
|
|
|
2019-01-27 05:20:45 +00:00
|
|
|
language = "javascript"
|
2015-06-06 20:08:44 +00:00
|
|
|
|
2015-06-10 18:36:18 +00:00
|
|
|
def __init__(self, obj, **kwargs):
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
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.
|
2019-01-27 05:20:45 +00:00
|
|
|
"""
|
2015-06-06 23:11:49 +00:00
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
super(JavaScriptPythonMapper, self).__init__(obj, **kwargs)
|
2019-01-27 05:20:45 +00:00
|
|
|
self.name = obj.get("name")
|
2015-06-06 20:08:44 +00:00
|
|
|
self.id = self.name
|
|
|
|
|
|
|
|
# Second level
|
2019-01-27 05:20:45 +00:00
|
|
|
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
|
|
|
|
2019-01-27 05:20:45 +00:00
|
|
|
self.imports = obj.get("imports", [])
|
2015-06-06 20:08:44 +00:00
|
|
|
self.children = []
|
|
|
|
self.parameters = map(
|
2019-01-27 05:20:45 +00:00
|
|
|
lambda n: {"name": n["name"], "type": n["type"][0]}, obj.get("param", [])
|
2015-06-06 20:08:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Language Specific
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptClass(JavaScriptPythonMapper):
|
2019-01-27 05:20:45 +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):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "function"
|
|
|
|
ref_type = "func"
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptData(JavaScriptPythonMapper):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "data"
|
|
|
|
ref_directive = "data"
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptMember(JavaScriptPythonMapper):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "member"
|
|
|
|
ref_directive = "member"
|
2015-06-06 20:08:44 +00:00
|
|
|
|
|
|
|
|
2015-06-10 21:23:50 +00:00
|
|
|
class JavaScriptAttribute(JavaScriptPythonMapper):
|
2019-01-27 05:20:45 +00:00
|
|
|
type = "attribute"
|
|
|
|
ref_directive = "attr"
|
2015-06-06 20:08:44 +00:00
|
|
|
|
2018-06-06 06:15:45 +00:00
|
|
|
|
2015-06-06 20:08:44 +00:00
|
|
|
ALL_CLASSES = [
|
|
|
|
JavaScriptFunction,
|
|
|
|
JavaScriptClass,
|
|
|
|
JavaScriptData,
|
|
|
|
JavaScriptAttribute,
|
|
|
|
JavaScriptMember,
|
|
|
|
]
|