mkdocstrings-matlab 0.3.0__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,294 @@
1
+ from pathlib import Path
2
+ from collections import ChainMap
3
+ from markdown import Markdown
4
+ from mkdocstrings.handlers.base import BaseHandler, CollectorItem, CollectionError
5
+ from mkdocstrings_handlers.python import rendering
6
+ from typing import Any, ClassVar, Mapping
7
+ from pprint import pprint
8
+
9
+
10
+ from mkdocstrings_handlers.matlab.collect import LinesCollection, PathCollection
11
+
12
+
13
+ class MatlabHandler(BaseHandler):
14
+ """The `MatlabHandler` class is a handler for processing Matlab code documentation."""
15
+
16
+ name: str = "matlab"
17
+ """The handler's name."""
18
+ domain: str = "mat" # to match Sphinx's default domain
19
+ """The cross-documentation domain/language for this handler."""
20
+ enable_inventory: bool = True
21
+ """Whether this handler is interested in enabling the creation of the `objects.inv` Sphinx inventory file."""
22
+ fallback_theme = "material"
23
+ """The fallback theme."""
24
+ fallback_config: ClassVar[dict] = {"fallback": True}
25
+ """The configuration used to collect item during autorefs fallback."""
26
+ default_config: ClassVar[dict] = {
27
+ # General options
28
+ "show_bases": True,
29
+ "show_inheritance_diagram": False,
30
+ "show_source": True,
31
+ # Heading options
32
+ "heading_level": 2,
33
+ "show_root_heading": False,
34
+ "show_root_toc_entry": True,
35
+ "show_root_full_path": True,
36
+ "show_root_members_full_path": False,
37
+ "show_object_full_path": False,
38
+ "show_category_heading": False,
39
+ "show_symbol_type_heading": False,
40
+ "show_symbol_type_toc": False,
41
+ # Member options
42
+ "inherited_members": False,
43
+ "members": None,
44
+ "members_order": rendering.Order.alphabetical, # TODO broken
45
+ "filters": [],
46
+ "group_by_category": True, # TODO broken
47
+ "summary": False, # TODO broken
48
+ "show_labels": True,
49
+ # Docstring options
50
+ "docstring_style": "google",
51
+ "docstring_options": {},
52
+ "docstring_section_style": "table",
53
+ "create_from_argument_blocks": False,
54
+ "merge_constructor_into_class": False,
55
+ "show_if_no_docstring": False,
56
+ "show_docstring_attributes": True,
57
+ "show_docstring_functions": True,
58
+ "show_docstring_classes": True,
59
+ "show_docstring_modules": True, # TODO should be replaced with namespaces
60
+ "show_docstring_description": True,
61
+ "show_docstring_examples": True,
62
+ "show_docstring_other_parameters": True, # TODO should be name value pairs
63
+ "show_docstring_parameters": True,
64
+ "show_docstring_raises": True, # TODO need to additional parsing for this
65
+ "show_docstring_returns": True,
66
+ "show_docstring_warns": True, # TODO need to additional parsing for this
67
+ # Signature options
68
+ "annotations_path": "brief",
69
+ "line_length": 60,
70
+ "show_signature": True,
71
+ "show_signature_annotations": False,
72
+ "separate_signature": False,
73
+ }
74
+ """Default handler configuration.
75
+
76
+ Attributes: General options:
77
+ show_bases (bool): Show the base classes of a class. Default: `True`.
78
+ show_inheritance_diagram (bool): Show the inheritance diagram of a class using Mermaid. Default: `False`.
79
+ show_source (bool): Show the source code of this object. Default: `True`.
80
+
81
+
82
+ Attributes: Headings options:
83
+ heading_level (int): The initial heading level to use. Default: `2`.
84
+ show_root_heading (bool): Show the heading of the object at the root of the documentation tree
85
+ (i.e. the object referenced by the identifier after `:::`). Default: `False`.
86
+ show_root_toc_entry (bool): If the root heading is not shown, at least add a ToC entry for it. Default: `True`.
87
+ show_root_full_path (bool): Show the full path for the root object heading. Default: `True`.
88
+ show_root_members_full_path (bool): Show the full path of the root members. Default: `False`.
89
+ show_object_full_path (bool): Show the full path of every object. Default: `False`.
90
+ show_category_heading (bool): When grouped by categories, show a heading for each category. Default: `False`.
91
+ show_symbol_type_heading (bool): Show the symbol type in headings (e.g. mod, class, meth, func and attr). Default: `False`.
92
+ show_symbol_type_toc (bool): Show the symbol type in the Table of Contents (e.g. mod, class, methd, func and attr). Default: `False`.
93
+
94
+ Attributes: Members options:
95
+ inherited_members (list[str] | bool | None): A boolean, or an explicit list of inherited members to render.
96
+ If true, select all inherited members, which can then be filtered with `members`.
97
+ If false or empty list, do not select any inherited member. Default: `False`.
98
+ members (list[str] | bool | None): A boolean, or an explicit list of members to render.
99
+ If true, select all members without further filtering.
100
+ If false or empty list, do not render members.
101
+ If none, select all members and apply further filtering with filters and docstrings. Default: `None`.
102
+ members_order (str): The members ordering to use. Options: `alphabetical` - order by the members names,
103
+ `source` - order members as they appear in the source file. Default: `"alphabetical"`.
104
+ filters (list[str] | None): A list of filters applied to filter objects based on their name.
105
+ A filter starting with `!` will exclude matching objects instead of including them.
106
+ The `members` option takes precedence over `filters` (filters will still be applied recursively
107
+ to lower members in the hierarchy). Default: `["!^_[^_]"]`.
108
+ group_by_category (bool): Group the object's children by categories: attributes, classes, functions, and modules. Default: `True`.
109
+ summary (bool | dict[str, bool]): Whether to render summaries of modules, classes, functions (methods) and attributes.
110
+ show_labels (bool): Whether to show labels of the members. Default: `True`.
111
+
112
+ Attributes: Docstrings options:
113
+ docstring_style (str): The docstring style to use: `google`, `numpy`, `sphinx`, or `None`. Default: `"google"`.
114
+ docstring_options (dict): The options for the docstring parser. See [docstring parsers](https://mkdocstrings.github.io/griffe/reference/docstrings/) and their options in Griffe docs.
115
+ docstring_section_style (str): The style used to render docstring sections. Options: `table`, `list`, `spacy`. Default: `"table"`.
116
+ merge_constructor_into_class (bool): Whether to merge the constructor method into the class' signature and docstring. Default: `False`.
117
+ create_from_argument_blocks (bool): Whether to create sections for inputs and output arguments based on argument validation blocks. Default: `False`.
118
+ relative_crossrefs (bool): Whether to enable the relative crossref syntax. Default: `False`.
119
+ scoped_crossrefs (bool): Whether to enable the scoped crossref ability. Default: `False`.
120
+ show_if_no_docstring (bool): Show the object heading even if it has no docstring or children with docstrings. Default: `False`.
121
+ show_docstring_attributes (bool): Whether to display the "Attributes" section in the object's docstring. Default: `True`.
122
+ show_docstring_functions (bool): Whether to display the "Functions" or "Methods" sections in the object's docstring. Default: `True`.
123
+ show_docstring_classes (bool): Whether to display the "Classes" section in the object's docstring. Default: `True`.
124
+ show_docstring_modules (bool): Whether to display the "Modules" section in the object's docstring. Default: `True`.
125
+ show_docstring_description (bool): Whether to display the textual block (including admonitions) in the object's docstring. Default: `True`.
126
+ show_docstring_examples (bool): Whether to display the "Examples" section in the object's docstring. Default: `True`.
127
+ show_docstring_other_parameters (bool): Whether to display the "Other Parameters" section in the object's docstring. Default: `True`.
128
+ show_docstring_parameters (bool): Whether to display the "Parameters" section in the object's docstring. Default: `True`.
129
+ show_docstring_raises (bool): Whether to display the "Raises" section in the object's docstring. Default: `True`.
130
+ show_docstring_returns (bool): Whether to display the "Returns" section in the object's docstring. Default: `True`.
131
+ show_docstring_warns (bool): Whether to display the "Warns" section in the object's docstring. Default: `True`.
132
+
133
+ Attributes: Signatures/annotations options:
134
+ annotations_path (str): The verbosity for annotations path: `brief` (recommended), or `source` (as written in the source). Default: `"brief"`.
135
+ line_length (int): Maximum line length when formatting code/signatures. Default: `60`.
136
+ show_signature (bool): Show methods and functions signatures. Default: `True`.
137
+ show_signature_annotations (bool): Show the type annotations in methods and functions signatures. Default: `False`.
138
+ separate_signature (bool): Whether to put the whole signature in a code block below the heading.
139
+ """
140
+
141
+ def __init__(
142
+ self,
143
+ *args: Any,
144
+ config_file_path: str | None = None,
145
+ paths: list[str] | None = None,
146
+ paths_recursive: bool = False,
147
+ locale: str = "en",
148
+ **kwargs: Any,
149
+ ) -> None:
150
+ """
151
+ Initialize the handler with the given configuration.
152
+
153
+ Args:
154
+ *args (Any): Variable length argument list.
155
+ config_file_path (str | None, optional): Path to the configuration file. Defaults to None.
156
+ paths (list[str] | None, optional): List of paths to include. Defaults to None.
157
+ paths_recursive (bool, optional): Whether to include paths recursively. Defaults to False.
158
+ locale (str, optional): Locale setting. Defaults to "en".
159
+ **kwargs (Any): Arbitrary keyword arguments.
160
+
161
+ Returns:
162
+ None
163
+ """
164
+ super().__init__(*args, **kwargs)
165
+
166
+ if paths is None or config_file_path is None:
167
+ full_paths = []
168
+ else:
169
+ config_path = Path(config_file_path).parent
170
+ full_paths = [(config_path / path).resolve() for path in paths]
171
+
172
+ self.paths: PathCollection = PathCollection(
173
+ full_paths, recursive=paths_recursive
174
+ )
175
+ self.lines: LinesCollection = self.paths.lines_collection
176
+ self._locale: str = locale
177
+
178
+ def get_templates_dir(self, handler: str | None = None) -> Path:
179
+ # use the python handler templates
180
+ # (it assumes the python handler is installed)
181
+ return super().get_templates_dir("python")
182
+
183
+ def render(self, data: CollectorItem, config: Mapping[str, Any]) -> str:
184
+ """Render a template using provided data and configuration options.
185
+
186
+ Arguments:
187
+ data: The collected data to render.
188
+ config: The handler's configuration options.
189
+
190
+ Returns:
191
+ The rendered template as HTML.
192
+ """
193
+ final_config = ChainMap(config, self.default_config) # type: ignore[arg-type]
194
+
195
+ template_name = rendering.do_get_template(self.env, data)
196
+ template = self.env.get_template(template_name)
197
+
198
+ heading_level = final_config["heading_level"]
199
+
200
+ return template.render(
201
+ **{
202
+ "config": final_config,
203
+ data.kind.value: data,
204
+ "heading_level": heading_level,
205
+ "root": True,
206
+ "locale": self._locale,
207
+ },
208
+ )
209
+
210
+ def update_env(self, md: Markdown, config: dict) -> None:
211
+ """Update the Jinja environment with custom filters and tests.
212
+
213
+ Parameters:
214
+ md: The Markdown instance.
215
+ config: The configuration dictionary.
216
+ """
217
+ super().update_env(md, config)
218
+ self.env.trim_blocks = True
219
+ self.env.lstrip_blocks = True
220
+ self.env.keep_trailing_newline = False
221
+ self.env.filters["split_path"] = rendering.do_split_path
222
+ self.env.filters["crossref"] = rendering.do_crossref
223
+ self.env.filters["multi_crossref"] = rendering.do_multi_crossref
224
+ self.env.filters["order_members"] = rendering.do_order_members
225
+ self.env.filters["format_code"] = rendering.do_format_code
226
+ self.env.filters["format_signature"] = rendering.do_format_signature
227
+ self.env.filters["format_attribute"] = rendering.do_format_attribute
228
+ self.env.filters["filter_objects"] = rendering.do_filter_objects
229
+ self.env.filters["stash_crossref"] = lambda ref, length: ref
230
+ self.env.filters["get_template"] = rendering.do_get_template
231
+ self.env.filters["as_attributes_section"] = rendering.do_as_attributes_section
232
+ self.env.filters["as_functions_section"] = rendering.do_as_functions_section
233
+ self.env.filters["as_classes_section"] = rendering.do_as_classes_section
234
+ self.env.filters["as_modules_section"] = rendering.do_as_modules_section
235
+ self.env.tests["existing_template"] = (
236
+ lambda template_name: template_name in self.env.list_templates()
237
+ )
238
+
239
+ def collect(self, identifier: str, config: Mapping[str, Any]) -> CollectorItem:
240
+ """Collect data given an identifier and user configuration.
241
+
242
+ In the implementation, you typically call a subprocess that returns JSON, and load that JSON again into
243
+ a Python dictionary for example, though the implementation is completely free.
244
+
245
+ Arguments:
246
+ identifier: An identifier for which to collect data.
247
+ config: The handler's configuration options.
248
+
249
+ Returns:
250
+ CollectorItem
251
+ """
252
+ if identifier == "":
253
+ raise CollectionError("Empty identifier")
254
+
255
+ final_config = ChainMap(config, self.default_config) # type: ignore[arg-type]
256
+ return self.paths.resolve(identifier, config=final_config)
257
+
258
+
259
+ def get_handler(
260
+ *,
261
+ theme: str,
262
+ custom_templates: str | None = None,
263
+ config_file_path: str | None = None,
264
+ paths: list[str] | None = None,
265
+ paths_recursive: bool = False,
266
+ **config: Any,
267
+ ) -> MatlabHandler:
268
+ """
269
+ Create and return a MatlabHandler object with the specified configuration.
270
+
271
+ Parameters:
272
+ theme (str): The theme to be used by the handler.
273
+ custom_templates (str | None, optional): Path to custom templates. Defaults to None.
274
+ config_file_path (str | None, optional): Path to the configuration file. Defaults to None.
275
+ paths (list[str] | None, optional): List of paths to include. Defaults to None.
276
+ paths_recursive (bool, optional): Whether to include paths recursively. Defaults to False.
277
+ **config (Any): Additional configuration options.
278
+
279
+ Returns:
280
+ MatlabHandler: An instance of MatlabHandler configured with the provided parameters.
281
+ """
282
+ return MatlabHandler(
283
+ handler="matlab",
284
+ theme=theme,
285
+ custom_templates=custom_templates,
286
+ config_file_path=config_file_path,
287
+ paths=paths,
288
+ paths_recursive=paths_recursive,
289
+ )
290
+
291
+
292
+ if __name__ == "__main__":
293
+ handler = get_handler(theme="material")
294
+ pprint(handler.collect("matlab_startup", {}).docstring.parse("google"))