|
| 1 | +# -*- coding: utf8 -*- |
| 2 | +# Copyright (c) 2019 Niklas Rosenstein |
| 3 | +# !!! Modified 2024 Jindřich Bär |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to |
| 7 | +# deal in the Software without restriction, including without limitation the |
| 8 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 | +# sell copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | +# IN THE SOFTWARE. |
| 22 | + |
| 23 | +import dataclasses |
| 24 | +import re |
| 25 | +import typing as t |
| 26 | + |
| 27 | +import docspec |
| 28 | + |
| 29 | +from pydoc_markdown.contrib.processors.sphinx import generate_sections_markdown |
| 30 | +from pydoc_markdown.interfaces import Processor, Resolver |
| 31 | + |
| 32 | +import json |
| 33 | + |
| 34 | + |
| 35 | +@dataclasses.dataclass |
| 36 | +class ApifyGoogleProcessor(Processor): |
| 37 | + """ |
| 38 | + This class implements the preprocessor for Google and PEP 257 docstrings. It converts |
| 39 | + docstrings formatted in the Google docstyle to Markdown syntax. |
| 40 | +
|
| 41 | + References: |
| 42 | +
|
| 43 | + * https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html |
| 44 | + * https://www.python.org/dev/peps/pep-0257/ |
| 45 | +
|
| 46 | + Example: |
| 47 | +
|
| 48 | + ``` |
| 49 | + Attributes: |
| 50 | + module_level_variable1 (int): Module level variables may be documented in |
| 51 | + either the ``Attributes`` section of the module docstring, or in an |
| 52 | + inline docstring immediately following the variable. |
| 53 | +
|
| 54 | + Either form is acceptable, but the two should not be mixed. Choose |
| 55 | + one convention to document module level variables and be consistent |
| 56 | + with it. |
| 57 | +
|
| 58 | + Todo: |
| 59 | + * For module TODOs |
| 60 | + * You have to also use ``sphinx.ext.todo`` extension |
| 61 | + ``` |
| 62 | +
|
| 63 | + Renders as: |
| 64 | +
|
| 65 | + Attributes: |
| 66 | + module_level_variable1 (int): Module level variables may be documented in |
| 67 | + either the ``Attributes`` section of the module docstring, or in an |
| 68 | + inline docstring immediately following the variable. |
| 69 | +
|
| 70 | + Either form is acceptable, but the two should not be mixed. Choose |
| 71 | + one convention to document module level variables and be consistent |
| 72 | + with it. |
| 73 | +
|
| 74 | + Todo: |
| 75 | + * For module TODOs |
| 76 | + * You have to also use ``sphinx.ext.todo`` extension |
| 77 | +
|
| 78 | + @doc:fmt:google |
| 79 | + """ |
| 80 | + |
| 81 | + _param_res = [ |
| 82 | + re.compile(r"^(?P<param>\S+):\s+(?P<desc>.+)$"), |
| 83 | + re.compile(r"^(?P<param>\S+)\s+\((?P<type>[^)]+)\):\s+(?P<desc>.+)$"), |
| 84 | + re.compile(r"^(?P<param>\S+)\s+--\s+(?P<desc>.+)$"), |
| 85 | + re.compile(r"^(?P<param>\S+)\s+\{\[(?P<type>\S+)\]\}\s+--\s+(?P<desc>.+)$"), |
| 86 | + re.compile(r"^(?P<param>\S+)\s+\{(?P<type>\S+)\}\s+--\s+(?P<desc>.+)$"), |
| 87 | + ] |
| 88 | + |
| 89 | + _keywords_map = { |
| 90 | + "Args:": "Arguments", |
| 91 | + "Arguments:": "Arguments", |
| 92 | + "Attributes:": "Attributes", |
| 93 | + "Example:": "Example", |
| 94 | + "Examples:": "Examples", |
| 95 | + "Keyword Args:": "Arguments", |
| 96 | + "Keyword Arguments:": "Arguments", |
| 97 | + "Methods:": "Methods", |
| 98 | + "Note:": "Notes", |
| 99 | + "Notes:": "Notes", |
| 100 | + "Other Parameters:": "Arguments", |
| 101 | + "Parameters:": "Arguments", |
| 102 | + "Return:": "Returns", |
| 103 | + "Returns:": "Returns", |
| 104 | + "Raises:": "Raises", |
| 105 | + "References:": "References", |
| 106 | + "See Also:": "See Also", |
| 107 | + "Todo:": "Todo", |
| 108 | + "Warning:": "Warnings", |
| 109 | + "Warnings:": "Warnings", |
| 110 | + "Warns:": "Warns", |
| 111 | + "Yield:": "Yields", |
| 112 | + "Yields:": "Yields", |
| 113 | + } |
| 114 | + |
| 115 | + def check_docstring_format(self, docstring: str) -> bool: |
| 116 | + for section_name in self._keywords_map: |
| 117 | + if section_name in docstring: |
| 118 | + return True |
| 119 | + return False |
| 120 | + |
| 121 | + def process(self, modules: t.List[docspec.Module], resolver: t.Optional[Resolver]) -> None: |
| 122 | + docspec.visit(modules, self._process) |
| 123 | + |
| 124 | + def _process(self, node: docspec.ApiObject): |
| 125 | + if not node.docstring: |
| 126 | + return |
| 127 | + |
| 128 | + lines = [] |
| 129 | + sections = [] |
| 130 | + current_lines: t.List[str] = [] |
| 131 | + in_codeblock = False |
| 132 | + keyword = None |
| 133 | + multiline_argument_offset = -1 |
| 134 | + |
| 135 | + def _commit(): |
| 136 | + if keyword: |
| 137 | + sections.append({keyword: list(current_lines)}) |
| 138 | + else: |
| 139 | + lines.extend(current_lines) |
| 140 | + current_lines.clear() |
| 141 | + |
| 142 | + for line in node.docstring.content.split("\n"): |
| 143 | + multiline_argument_offset += 1 |
| 144 | + if line.lstrip().startswith("```"): |
| 145 | + in_codeblock = not in_codeblock |
| 146 | + current_lines.append(line) |
| 147 | + continue |
| 148 | + |
| 149 | + if in_codeblock: |
| 150 | + current_lines.append(line) |
| 151 | + continue |
| 152 | + |
| 153 | + line = line.strip() |
| 154 | + if line in self._keywords_map: |
| 155 | + _commit() |
| 156 | + keyword = self._keywords_map[line] |
| 157 | + continue |
| 158 | + |
| 159 | + if keyword is None: |
| 160 | + lines.append(line) |
| 161 | + continue |
| 162 | + |
| 163 | + for param_re in self._param_res: |
| 164 | + param_match = param_re.match(line) |
| 165 | + if param_match: |
| 166 | + current_lines.append(param_match.groupdict()) |
| 167 | + multiline_argument_offset = 0 |
| 168 | + break |
| 169 | + |
| 170 | + if not param_match: |
| 171 | + if multiline_argument_offset == 1: |
| 172 | + current_lines[-1]["desc"] += "\n" + line |
| 173 | + multiline_argument_offset = 0 |
| 174 | + else: |
| 175 | + current_lines.append(line) |
| 176 | + |
| 177 | + _commit() |
| 178 | + node.docstring.content = json.dumps({ |
| 179 | + "text": "\n".join(lines), |
| 180 | + "sections": sections, |
| 181 | + }, indent=None) |
| 182 | + |
| 183 | + |
0 commit comments