Commits

Anonymous committed 7b89e16

Added syntax to allow inclusion of code into sample documentation.

Comments (0)

Files changed (3)

sphinx/pycode/__init__.py

                 break
             prefix = pnode.get_prefix()
         prefix = prefix.decode(self.encoding)
-        docstring = prepare_commentdoc(prefix)
+        docstring = prepare_commentdoc(prefix, node.get_code())
         if docstring:
             self.add_docstring(node, docstring)
 

sphinx/pycode/nodes.py

         # only leaves carry a prefix
         return self.get_next_leaf().prefix
 
+    def get_code(self):
+        return self.get_value().strip()
+
+    def get_value(self):
+        if isinstance(self, Node):
+            return ''.join([ x.get_value() for x in self.children ])
+        else:
+            return '\n'.join([ x for x in str(self).expandtabs().split('\n') if not x.strip().startswith('#') ])
 
 class Node(BaseNode):
     """

sphinx/util/docstrings.py

     return lines
 
 
-def prepare_commentdoc(s):
+def prepare_commentdoc(s, c=None):
     """
     Extract documentation comment lines (starting with #:) and return them as a
     list of lines.  Returns an empty list if there is no documentation.
     result = []
     lines = [line.strip() for line in s.expandtabs().splitlines()]
     for line in lines:
-        if line.startswith('#:'):
+        if line.startswith('#::'):
+            if c is not None:
+                result.extend([
+                    '.. code-block:: python',
+                    '    ',
+                    '    %s' % c,
+                    ''
+                    ])
+            line = line[3:]
+            # the first space after the comment is ignored
+            if line and line[0] == ' ':
+                line = line[1:]
+            result.append(line)
+        elif line.startswith('#:'):
             line = line[2:]
             # the first space after the comment is ignored
             if line and line[0] == ' ':