Commits

Dan Callaghan committed 5d8f28c

javadocref extension, for resolving references to external Javadoc

Comments (0)

Files changed (6)

javadocref/MANIFEST.in

+include README
+include LICENSE
+include CHANGES.*

javadocref/README

Empty file added.

javadocref/setup.cfg

+[egg_info]
+tag_build = dev
+tag_date = true
+
+[aliases]
+release = egg_info -RDb ''

javadocref/setup.py

+# -*- coding: utf-8 -*-
+
+from setuptools import setup, find_packages
+
+long_desc = '''
+This package contains the javadocref Sphinx extension.
+
+It can resolve Java class and method references by linking to external Javadoc.
+'''
+
+requires = ['Sphinx>=0.6']
+
+setup(
+    name='sphinxcontrib-javadocref',
+    version='0.1',
+    url='http://bitbucket.org/birkenfeld/sphinx-contrib',
+    download_url='http://pypi.python.org/pypi/sphinxcontrib-javadocref',
+    license='BSD',
+    author='Dan Callaghan',
+    author_email='djc@djc.id.au',
+    description='Sphinx "javadocref" extension',
+    long_description=long_desc,
+    zip_safe=False,
+    classifiers=[
+        'Development Status :: 4 - Beta',
+        'Environment :: Console',
+        'Environment :: Web Environment',
+        'Intended Audience :: Developers',
+        'License :: OSI Approved :: BSD License',
+        'Operating System :: OS Independent',
+        'Programming Language :: Python',
+        'Topic :: Documentation',
+        'Topic :: Utilities',
+    ],
+    platforms='any',
+    packages=find_packages(),
+    include_package_data=True,
+    install_requires=requires,
+    namespace_packages=['sphinxcontrib'],
+)

javadocref/sphinxcontrib/__init__.py

+# -*- coding: utf-8 -*-
+"""
+    sphinxcontrib
+    ~~~~~~~~~~~~~
+
+    This package is a namespace package that contains all extensions
+    distributed in the ``sphinx-contrib`` distribution.
+
+    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+"""
+
+__import__('pkg_resources').declare_namespace(__name__)
+

javadocref/sphinxcontrib/javadocref.py

+
+import os
+import urllib2
+import lxml.html
+
+from docutils import nodes
+
+handlers = [urllib2.ProxyHandler(), urllib2.HTTPRedirectHandler(),
+            urllib2.HTTPHandler()]
+if hasattr(urllib2, 'HTTPSHandler'):
+    handlers.append(urllib2.HTTPSHandler)
+
+urllib2.install_opener(urllib2.build_opener(*handlers))
+
+loaded = {} # base url -> list of class names # XXX cache this properly
+
+def load_class_lists(app):
+    for location in app.config.javadocref_locations:
+        all_classes_url = os.path.join(location, 'allclasses-frame.html')
+        app.info('Loading javadoc class list from %s' % all_classes_url)
+        doc = lxml.html.parse(urllib2.urlopen(all_classes_url)).getroot()
+        loaded[location] = []
+        for anchor in doc.findall('.//a'):
+            href = anchor.get('href')[:-5] # strip off .html
+            loaded[location].append(href.replace('/', '.'))
+
+def missing_reference(app, env, node, contnode):
+    if node.get('refdomain') != 'java': return
+    if node['reftype'] == 'method':
+        classname = node['reftarget'].split('#')[0]
+    else:
+        classname = node['reftarget']
+    for javadoc, classes in loaded.iteritems():
+        if classname in classes:
+            refuri = os.path.join(javadoc, classname.replace('.', '/') + '.html')
+            if node['reftype'] == 'method':
+                refuri += '#' + node['reftarget'].split('#', 1)[1]
+            newnode = nodes.reference('', '', internal=False, refuri=refuri)
+            newnode += contnode
+            return newnode
+
+def setup(app):
+    app.add_config_value('javadocref_locations', [], True)
+    app.connect('builder-inited', load_class_lists)
+    app.connect('missing-reference', missing_reference)
+
+if __name__ == '__main__':
+    print load_class_list('http://code.djc.id.au/rdftemplate/javadoc/latest/')