Anonymous avatar Anonymous committed 1b3ce1a

Changed the way documentation is generated.

Comments (0)

Files changed (1)

piston/handlers_doc.py

-from docutils import nodes
-from sphinx.util.compat import Directive
 from piston.doc import generate_doc
+from piston.handler import handler_tracker
+import re
 
-class handlers_doc(nodes.General, nodes.Element): pass
+def generate_piston_documentation(app, docname, source):
+    e = re.compile(r"^\.\. piston_handlers:: ([\w\.]+)$")
+    old_source = source[0].split("\n")
+    new_source = old_source[:]
+    for line_nr, line in enumerate(old_source):
+        m = e.match(line)
+        if m:
+            module = m.groups()[0]
+            try:
+                __import__(module)
+            except ImportError:
+                pass
+            else:
+                new_lines = []
+                for handler in handler_tracker:
+                    doc = generate_doc(handler)
+                    new_lines.append(doc.name)
+                    new_lines.append("-" * len(doc.name))
+                    new_lines.append('::\n')
+                    new_lines.append('\t' + doc.get_resource_uri_template() + '\n')
+                    new_lines.append('Accepted methods:')
+                    for method in doc.allowed_methods:
+                        new_lines.append('\t* ' + method)
+                    new_lines.append('')
+                    if doc.doc:
+                        new_lines.append(doc.doc)
+                new_source[line_nr:line_nr+1] = new_lines
 
-class PistonHandlers(Directive):
-    required_arguments = 1
-
-    def run(self):
-        module_name = self.arguments[0]
-        content = []
-        try:
-            __import__(module_name)
-            content = [handlers_doc()]
-        except ImportError:
-            self.state.document.reporter.warning("Can't import %s module." % module_name)
-        return content
-
-def add_piston_documentation(app, doctree, fromdocname):
-    content = []
-
-    from piston.handler import handler_tracker
-    for handler in handler_tracker:
-        doc = generate_doc(handler)
-        title = nodes.title()
-        title += nodes.Text(doc.name)
-        content.append(title)
-        if doc.doc:
-            doctitle = nodes.paragraph()
-            doctitle += nodes.Text('Docstring:')
-            content.append(doctitle)
-            docstring = nodes.literal_block()
-            docstring += nodes.Text(doc.doc, doc.doc)
-            content.append(docstring)
-        uri = nodes.literal_block()
-        uri += nodes.Text(doc.get_resource_uri_template())
-        content.append(uri)
-        methods = nodes.paragraph()
-        methods_text = nodes.paragraph()
-        methods_text += nodes.Text("Accepted methods:")
-        methods += methods_text
-        accepted_methods = nodes.bullet_list()
-        for accepted_method in doc.allowed_methods:
-            item = nodes.list_item()
-            item_para = nodes.paragraph()
-            item_para += nodes.Text(accepted_method)
-            item += item_para
-            accepted_methods += item
-        methods += accepted_methods
-        content.append(methods)
-
-    for node in doctree.traverse(handlers_doc):
-        node.replace_self(content)
+    source[0] = "\n".join(new_source)
+    return source
 
 def setup(app):
-    app.add_node(handlers_doc)
-    app.add_directive('piston_handlers', PistonHandlers)
-    app.connect('doctree-resolved', add_piston_documentation)
+    app.connect('source-read', generate_piston_documentation)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.