|
1 | 1 | from sphinx.ext.autodoc import ClassDocumenter, MethodDocumenter, FunctionDocumenter
|
2 |
| - |
3 |
| -def setup(app): |
4 |
| - app.add_autodocumenter(RawFunctionDocumenter) |
5 |
| - app.add_autodocumenter(RawMethodDocumenter) |
6 |
| - app.add_autodocumenter(RawClassDocumenter) |
7 |
| - return {'version': '1.0', 'parallel_read_safe': True} |
| 2 | +import inspect |
8 | 3 |
|
9 | 4 | class RawDocumenterMixin:
|
10 |
| - def add_directive_header(self, sig): |
11 |
| - super().add_directive_header(sig) |
| 5 | + def get_doc(self, encoding=None, ignore=1): |
| 6 | + """Directly return the docstring as-is.""" |
12 | 7 | if self.object.__doc__:
|
13 |
| - # Add raw docstring as a code block |
| 8 | + # Return as a list of lines |
| 9 | + return [self.object.__doc__.splitlines()] |
| 10 | + return [] |
| 11 | + |
| 12 | + def add_content(self, more_content): |
| 13 | + """Add content from docstrings, attribute documentation and user.""" |
| 14 | + docstring = self.get_doc() |
| 15 | + if docstring: |
| 16 | + self.add_line('', '<autodoc>') |
| 17 | + self.add_line('**Raw Docstring:**', '<autodoc>') |
14 | 18 | self.add_line('', '<autodoc>')
|
15 | 19 | self.add_line('.. code-block:: python', '<autodoc>')
|
16 | 20 | self.add_line('', '<autodoc>')
|
17 |
| - for line in self.object.__doc__.splitlines(): |
| 21 | + for line in docstring[0]: |
18 | 22 | self.add_line(f' {line}', '<autodoc>')
|
19 | 23 | self.add_line('', '<autodoc>')
|
| 24 | + |
| 25 | + # Add any additional content from parent class |
| 26 | + super().add_content(more_content) |
20 | 27 |
|
21 | 28 | class RawFunctionDocumenter(RawDocumenterMixin, FunctionDocumenter):
|
22 | 29 | objtype = 'function'
|
| 30 | + priority = 10 + FunctionDocumenter.priority |
23 | 31 |
|
24 | 32 | class RawMethodDocumenter(RawDocumenterMixin, MethodDocumenter):
|
25 | 33 | objtype = 'method'
|
| 34 | + priority = 10 + MethodDocumenter.priority |
26 | 35 |
|
27 | 36 | class RawClassDocumenter(RawDocumenterMixin, ClassDocumenter):
|
28 | 37 | objtype = 'class'
|
| 38 | + priority = 10 + ClassDocumenter.priority |
| 39 | + |
| 40 | +def setup(app): |
| 41 | + app.add_autodocumenter(RawFunctionDocumenter) |
| 42 | + app.add_autodocumenter(RawMethodDocumenter) |
| 43 | + app.add_autodocumenter(RawClassDocumenter) |
| 44 | + return {'version': '1.0', 'parallel_read_safe': True} |
0 commit comments