Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ericholscher committed Mar 27, 2015
0 parents commit c7bef8e
Show file tree
Hide file tree
Showing 12 changed files with 790 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.pyc
*.egg-info
build
dist
wheelhouse
4 changes: 4 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,4 @@
recursive-include readthedocs_ext *.css
recursive-include readthedocs_ext *.js_t
recursive-include readthedocs_ext *.js
include *.rst
23 changes: 23 additions & 0 deletions README.rst
@@ -0,0 +1,23 @@
Sphinx Auto API
===============

A tool that generates a full API ref (Javadoc style) for your project.
It requires no RST and is fully automated,
while being integrated into Sphinx.

The first implementation is for Microsoft .Net.
Other implementations are encouraged for your language.

Design
------

Read more about the deisgn in our Design doc.

Future
------
Our goal is to support the following soon:

* Javascript
* PHP
* Python
* Go
Empty file added autoapi/__init__.py
Empty file.
126 changes: 126 additions & 0 deletions autoapi/extension.py
@@ -0,0 +1,126 @@
# -*- coding: utf-8 -*-
"""
Sphinx Auto-API
"""

import os
import yaml
from collections import defaultdict

from docutils import nodes
from docutils import utils

from sphinx.util.osutil import ensuredir

from jinja2 import Environment, FileSystemLoader
from .utils import TEMPLATE_DIR

env = Environment(loader=FileSystemLoader(TEMPLATE_DIR))


class DotNetMember(object):

def __init__(self, obj):
self.obj = obj

def render(self):
print "Unknown Type: %s (%s)" % (self.obj['type'], self.obj['name'])
self.obj['underline'] = len(self.obj['qualifiedName']['CSharp']) * "#"
self.obj['lower'] = self.obj['type'].lower()
template = env.get_template('member.rst')
return template.render(**self.obj)


class DotNetClass(object):

def __init__(self, obj):
self.obj = obj
self.item_map = defaultdict(list)
self.sort()

def sort(self):
for item in self.obj['items']:
if 'type' not in item:
print "Missing Type: %s" % item
continue
self.item_map[item['type']].append(item)

def render(self):
print "Rendering class %s" % self.obj['name']
self.obj['underline'] = len(self.obj['qualifiedName']['CSharp']) * "#"
template = env.get_template('class.rst')

ctx = self.obj
ctx.update(dict(
ctors=self.item_map['Constructor'],
methods=self.item_map['Method'],
attributes=self.item_map['Property'],
))
return template.render(**ctx)


def parse(obj):
if 'type' not in obj:
return ''

if obj['type'] == 'Class':
return DotNetClass(obj).render()
if obj['type'] == 'Namespace':
return '' #for now
else:
return DotNetMember(obj).render()


def load_yaml(app):
if not app.config.autoapi_dir:
return
app.env.autoapi_data = []
if app.config.autoapi_type == 'yaml':
for _file in os.listdir(app.env.config.autoapi_dir):
print "Loading Yaml from %s" % _file
to_open = os.path.join(app.env.config.autoapi_dir, _file)
app.env.autoapi_data.append(yaml.safe_load(open(to_open, 'r')))
app.env.autoapi_enabled = True

# Generate RST
for obj in app.env.autoapi_data:
print "Parsing %s" % obj['name']
rst = parse(obj)
if rst:
path = os.path.join(app.config.autoapi_root, '%s.rst' % obj['name']['CSharp'])
ensuredir(app.config.autoapi_root)
with open(path, 'w+') as fp:
fp.write(rst)


def doctree_read(app, doctree):
pass

# para = nodes.paragraph('Test Para', 'Test Para')
# new_doc = utils.new_document(para)


def env_updated(app, env):
# env.found_docs.add(os.path.join(app.config.autoapi_root, 'test'))
pass


def collect_pages(app):
pass

context = {
'title': 'Test Title',
'body': 'Fak',
}

yield (os.path.join(app.config.autoapi_root, 'test_insert'), context, 'page.html')


def setup(app):
app.connect('doctree-read', doctree_read)
app.connect('builder-inited', load_yaml)
app.connect('env-updated', env_updated)
app.connect('html-collect-pages', collect_pages)
app.add_config_value('autoapi_type', 'yaml', 'html')
app.add_config_value('autoapi_root', 'autoapi', 'html')
app.add_config_value('autoapi_dir', '/Users/eric/projects/sphinx-dotnet-test/examples/yaml/', 'html')
64 changes: 64 additions & 0 deletions autoapi/templates/class.rst
@@ -0,0 +1,64 @@
{{ name.CSharp }}
{{ underline }}

{#
.. currentmodule:: {{ module }}
#}

Summary
-------

{{ summary }}

Inheritance Hierarchy
---------------------

{% for item in inheritance %}
* {{ item.id }}
{% endfor %}

Syntax

.. code-block:: csharp
{{ syntax.content.CSharp }}
Class Information
-----------------

.. class:: {{ name.CSharp }}

{% if ctors %}

.. rubric:: Constructors

{% for item in ctors %}
{% include "member.rst" %}
{%- endfor %}
{% endif %}


{% block methods %}

{% if methods %}

.. rubric:: Methods

{% for item in methods %}
{% include "member.rst" %}
{%- endfor %}
{% endif %}
{% endblock %}



{% block attributes %}
{% if attributes %}

.. rubric:: Attributes

{% for item in attributes %}
{% include "member.rst" %}
{%- endfor %}
{% endif %}
{% endblock %}
15 changes: 15 additions & 0 deletions autoapi/templates/member.rst
@@ -0,0 +1,15 @@
{# Identention in this file is important #}

.. {{ type.lower() }}:: {{ item.qualifiedName.CSharp }}
.. example-code::

.. code-block:: csharp
{{ item.syntax.content.CSharp }}
.. code-block:: cpp
This will be different
{{ item.syntax.content.CSharp }}
18 changes: 18 additions & 0 deletions autoapi/templates/methods.rst
@@ -0,0 +1,18 @@
{# Identention in this file is important #}
{% block methods %}

{% if methods %}

.. rubric:: Methods

{% for item in methods %}

.. method:: {{ item.qualifiedName.CSharp }}

.. code-block:: csharp
{{ item.syntax.content.CSharp }}

{%- endfor %}
{% endif %}
{% endblock %}
6 changes: 6 additions & 0 deletions autoapi/utils.py
@@ -0,0 +1,6 @@
import re
import os

SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

TEMPLATE_DIR = os.path.join(SITE_ROOT, 'templates')
2 changes: 2 additions & 0 deletions setup.cfg
@@ -0,0 +1,2 @@
[wheel]
universal = 1
28 changes: 28 additions & 0 deletions setup.py
@@ -0,0 +1,28 @@
import codecs
try:
from setuptools import setup, find_packages
extra_setup = dict(
install_requires=['pyyaml'],
)
except ImportError:
from distutils.core import setup
extra_setup = {}

setup(
name='sphinx-autoapi',
version='0.1.0',
author='Eric Holscher',
author_email='eric@ericholscher.com',
url='http://github.com/ericholscher/sphinx-autoapi',
license='BSD',
description='',
package_dir={'': '.'},
packages=find_packages('.'),
long_description=codecs.open("README.rst", "r", "utf-8").read(),
# trying to add files...
include_package_data=True,
package_data={
'': ['_static/*.js', '_static/*.js_t', '_static/*.css'],
},
**extra_setup
)

0 comments on commit c7bef8e

Please sign in to comment.