|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (c) 2019 Niklas Rosenstein |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to |
| 6 | +# deal in the Software without restriction, including without limitation the |
| 7 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 | +# sell copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in |
| 12 | +# all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | +# IN THE SOFTWARE. |
| 21 | + |
| 22 | +import dataclasses |
| 23 | +import logging |
| 24 | +import typing as t |
| 25 | + |
| 26 | +import docspec |
| 27 | +import docstring_parser |
| 28 | +from pydoc_markdown.interfaces import Processor, Resolver |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +def sanitize(s: t.Optional[str]) -> str: |
| 34 | + if not s: |
| 35 | + return "" |
| 36 | + |
| 37 | + character_map = {r"<": r"\<", r">": r"\>", r"{": r"\{", r"}": r"\}"} |
| 38 | + |
| 39 | + for before, after in character_map.items(): |
| 40 | + s = s.replace(before, after) |
| 41 | + return s |
| 42 | + |
| 43 | + |
| 44 | +@dataclasses.dataclass |
| 45 | +class _ParamLine: |
| 46 | + """ |
| 47 | + Helper data class for holding details of Sphinx arguments. |
| 48 | + """ |
| 49 | + |
| 50 | + name: str |
| 51 | + docs: str |
| 52 | + type: t.Optional[str] = None |
| 53 | + |
| 54 | + |
| 55 | +def generate_sections_markdown(sections): |
| 56 | + ret = [] |
| 57 | + |
| 58 | + ret.append("<table style={{border: 'none'}}><tbody>\n") |
| 59 | + for key, section in sections.items(): |
| 60 | + if section: |
| 61 | + ret.append("\n<tr style={{border: 'none'}}>\n") |
| 62 | + |
| 63 | + ret.append( |
| 64 | + "\n<td style={{border: 'none', backgroundColor: 'var(--ifm-background-color)', 'verticalAlign': 'top'}}>\n" # noqa: E501 |
| 65 | + ) |
| 66 | + ret.append(f"**{key}**:") |
| 67 | + ret.append("\n</td>\n") |
| 68 | + |
| 69 | + ret.append( |
| 70 | + "\n<td style={{border: 'none', backgroundColor: 'var(--ifm-background-color)', 'verticalAlign': 'top'}}>\n" # noqa: E501 |
| 71 | + ) |
| 72 | + ret.extend(section) |
| 73 | + ret.append("\n</td>\n") |
| 74 | + |
| 75 | + ret.append("\n</tr>\n") |
| 76 | + ret.append("\n</tbody></table>\n") |
| 77 | + |
| 78 | + return ret |
| 79 | + |
| 80 | + |
| 81 | +@dataclasses.dataclass |
| 82 | +class RucioProcessor(Processor): |
| 83 | + _KEYWORDS = { |
| 84 | + "Arguments": [ |
| 85 | + "arg", |
| 86 | + "argument", |
| 87 | + "param", |
| 88 | + "parameter", |
| 89 | + "type", |
| 90 | + ], |
| 91 | + "Returns": [ |
| 92 | + "return", |
| 93 | + "returns", |
| 94 | + "rtype", |
| 95 | + ], |
| 96 | + "Raises": [ |
| 97 | + "raises", |
| 98 | + "raise", |
| 99 | + ], |
| 100 | + } |
| 101 | + |
| 102 | + def check_docstring_format(self, docstring: str) -> bool: |
| 103 | + return any( |
| 104 | + f":{k}" in docstring for _, value in self._KEYWORDS.items() for k in value |
| 105 | + ) |
| 106 | + |
| 107 | + def process( |
| 108 | + self, modules: t.List[docspec.Module], resolver: t.Optional[Resolver] |
| 109 | + ) -> None: |
| 110 | + docspec.visit(modules, self._process) |
| 111 | + |
| 112 | + def _convert_raises( |
| 113 | + self, raises: t.List[docstring_parser.common.DocstringRaises] |
| 114 | + ) -> list: |
| 115 | + """Convert a list of DocstringRaises from docstring_parser to markdown lines |
| 116 | +
|
| 117 | + :return: A list of markdown formatted lines |
| 118 | + """ |
| 119 | + converted_lines = [] |
| 120 | + for entry in raises: |
| 121 | + converted_lines.append( |
| 122 | + "`{}`: {}\n".format( |
| 123 | + sanitize(entry.type_name), sanitize(entry.description) |
| 124 | + ) |
| 125 | + ) |
| 126 | + return converted_lines |
| 127 | + |
| 128 | + def _convert_params( |
| 129 | + self, params: t.List[docstring_parser.common.DocstringParam] |
| 130 | + ) -> list: |
| 131 | + """Convert a list of DocstringParam to markdown lines. |
| 132 | +
|
| 133 | + :return: A list of markdown formatted lines |
| 134 | + """ |
| 135 | + converted = [] |
| 136 | + for param in params: |
| 137 | + if param.type_name is None: |
| 138 | + converted.append( |
| 139 | + "`{name}`: {description}\n".format( |
| 140 | + name=sanitize(param.arg_name), |
| 141 | + description=sanitize(param.description), |
| 142 | + ) |
| 143 | + ) |
| 144 | + else: |
| 145 | + converted.append( |
| 146 | + "`{name}` (`{type}`): {description}\n".format( |
| 147 | + name=sanitize(param.arg_name), |
| 148 | + type=param.type_name, |
| 149 | + description=sanitize(param.description), |
| 150 | + ) |
| 151 | + ) |
| 152 | + return converted |
| 153 | + |
| 154 | + def _convert_returns( |
| 155 | + self, returns: t.Optional[docstring_parser.common.DocstringReturns] |
| 156 | + ) -> str: |
| 157 | + """Convert a DocstringReturns object to a markdown string. |
| 158 | +
|
| 159 | + :return: A markdown formatted string |
| 160 | + """ |
| 161 | + if not returns: |
| 162 | + return "" |
| 163 | + if returns.type_name: |
| 164 | + type_data = "`{}`: ".format(returns.type_name) |
| 165 | + else: |
| 166 | + type_data = "" |
| 167 | + return " " + type_data + (sanitize(returns.description) or "") + "\n" |
| 168 | + |
| 169 | + def _process(self, node: docspec.ApiObject) -> None: |
| 170 | + if not node.docstring: |
| 171 | + return |
| 172 | + |
| 173 | + lines = [] |
| 174 | + components: t.Dict[str, t.List[str]] = {} |
| 175 | + |
| 176 | + parsed_docstring = docstring_parser.parse( |
| 177 | + node.docstring.content, docstring_parser.DocstringStyle.REST |
| 178 | + ) |
| 179 | + components["Arguments"] = self._convert_params(parsed_docstring.params) |
| 180 | + components["Raises"] = self._convert_raises(parsed_docstring.raises) |
| 181 | + return_doc = self._convert_returns(parsed_docstring.returns) |
| 182 | + if return_doc: |
| 183 | + components["Returns"] = [return_doc] |
| 184 | + |
| 185 | + if parsed_docstring.short_description: |
| 186 | + lines.append(sanitize(parsed_docstring.short_description)) |
| 187 | + lines.append("") |
| 188 | + if parsed_docstring.long_description: |
| 189 | + lines.append(sanitize(parsed_docstring.long_description)) |
| 190 | + lines.append("") |
| 191 | + |
| 192 | + lines.extend(generate_sections_markdown(components)) |
| 193 | + node.docstring.content = "\n".join(lines) |
0 commit comments