-
Notifications
You must be signed in to change notification settings - Fork 103
/
markdown.py
562 lines (455 loc) · 21.8 KB
/
markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# -*- coding: utf8 -*-
# Copyright (c) 2019 Niklas Rosenstein
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from __future__ import annotations
import dataclasses
import html
import io
import sys
import typing as t
from pathlib import Path
import docspec
from docspec_python import format_arglist
from yapf.yapflib.yapf_api import FormatCode # type: ignore[import]
from pydoc_markdown.interfaces import (
Context,
Renderer,
Resolver,
ResolverV2,
SingleObjectRenderer,
SinglePageRenderer,
SourceLinker,
)
from pydoc_markdown.util.docspec import ApiSuite, format_function_signature, is_method
def dotted_name(obj: docspec.ApiObject) -> str:
return ".".join(x.name for x in obj.path)
@dataclasses.dataclass
class MarkdownRenderer(Renderer, SinglePageRenderer, SingleObjectRenderer):
"""
Produces Markdown files. This renderer is often used by other renderers, such as
#MkdocsRenderer and #HugoRenderer. It provides a wide variety of options to customize
the generated Markdown files.
### Options
"""
#: The name of the file to render to. If no file is specified, it will
#: render to stdout.
filename: t.Optional[str] = None
#: The encoding of the output file. This is ignored when rendering to
#: stdout.
encoding: str = "utf-8"
#: If enabled, inserts anchors before Markdown headers to ensure that
#: links to the header work. This is enabled by default.
insert_header_anchors: bool = True
#: Generate HTML headers instead of Mearkdown headers. This is disabled
#: by default.
html_headers: bool = False
#: Render names in headers as code (using backticks or `<code>` tags,
#: depending on #html_headers). This is enabled by default.
code_headers: bool = False
#: Generate descriptive class titles by adding the word "Objects" if set to `True`. Otherwise,
#: it can be a string that is appended or prepended (appended if the string begins with `$`).
#: the class name. This is enabled by default.
descriptive_class_title: t.Union[bool, str] = True
#: Generate descriptivie module titles by adding the word "Module" before
#: the module name. This is enabled by default.
descriptive_module_title: bool = False
#: Add the module name as a prefix to class & method names. This module name is
#: also rendered as code if #code_headers is enabled. This is enabled
#: by default.
add_module_prefix: bool = True
#: Add the class name as a prefix to method names. This class name is
#: also rendered as code if #code_headers is enabled. This is enabled
#: by default.
add_method_class_prefix: bool = False
#: Add the class name as a prefix to member names. This is enabled by
#: default.
add_member_class_prefix: bool = False
#: Add the full module name as a prefix to the title of the header.
#: This is disabled by default.
add_full_prefix: bool = False
#: If #add_full_prefix is enabled, this will result in the prefix to
#: be wrapped in a `<sub>` tag.
sub_prefix: bool = False
#: Render the definition of data members as a code block. This is disabled
#: by default.
data_code_block: bool = False
#: Max length of expressions. If this limit is exceeded, the remaining
#: characters will be replaced with three dots. This is set to 100 by
#: default.
data_expression_maxlength: int = 100
#: Render the class signature as a code block. This includes the "class"
#: keyword, the class name and its bases. This is enabled by default.
classdef_code_block: bool = True
#: Render decorators before class definitions.
classdef_with_decorators: bool = True
#: Render classdef and function signature blocks in the Python help()
#: style.
signature_python_help_style: bool = False
#: Render the function signature as a code block. This includes the "def"
#: keyword, the function name and its arguments. This is enabled by
#: default.
signature_code_block: bool = True
#: Render the function signature in the header. This is disabled by default.
signature_in_header: bool = False
#: Render the vertical bar '|' before function signature. This is enabled by default.
signature_with_vertical_bar: bool = False
#: Include the "def" keyword in the function signature. This is enabled
#: by default.
signature_with_def: bool = True
#: Render the class name in the code block for function signature. Note
#: that this results in invalid Python syntax to be rendered. This is
#: disabled by default.
signature_class_prefix: bool = False
#: Render decorators before function definitions.
signature_with_decorators: bool = True
#: Render type hints for data elements in the header.
render_typehint_in_data_header: bool = False
#: Add the string "python" after the backticks for code blocks. This is
#: enabled by default.
code_lang: bool = True
#: Render title of page at the beginning of the file.
render_page_title: bool = False
#: Render a table of contents at the beginning of the file.
render_toc: bool = False
#: The title of the "Table of Contents" header.
render_toc_title: str = "Table of Contents"
#: The maximum depth of the table of contents. Defaults to 2.
toc_maxdepth: int = 2
#: Render module headers. This is enabled by default.
render_module_header: bool = True
#: Custom template for module header.
render_module_header_template: str = ""
#: Render docstrings as blockquotes. This is disabled by default.
docstrings_as_blockquote: bool = False
#: Use a fixed header level for every kind of API object. The individual
#: levels can be defined with #header_level_by_type.
use_fixed_header_levels: bool = True
#: Fixed header levels by API object type.
header_level_by_type: t.Dict[str, int] = dataclasses.field(
default_factory=lambda: {
"Module": 1,
"Class": 2,
"Method": 4,
"Function": 4,
"Variable": 4,
}
)
#: A plugin that implements the #SourceLinker interface to provide links to the
#: source code of API objects. If this field is specified, the renderer will
#: place links to the source code in the generated Markdown files.
source_linker: t.Optional[SourceLinker] = None
#: Allows you to define the position of the "view source" link in the Markdown
#: file if a #source_linker is configured.
# TODO: Validator.choices(["after signature", "before signature"])
source_position: str = "after signature"
#: Allows you to override how the "view source" link is rendered into the Markdown
#: file if a #source_linker is configured. The default is `[[view_source]]({url})`.
source_format: str = "[[view_source]]({url})"
#: Escape html in docstring. Default to False.
escape_html_in_docstring: bool = False
#: Render Novella `@anchor` tags before headings.
render_novella_anchors: bool = False
#: Format code rendered into Markdown code blocks with YAPF.
format_code: bool = True
#: The style to format code as. This can be a YAPF builtin style name or point to
#: a file relative to the context directory (usually the working directory).
format_code_style: str = "pep8"
def __post_init__(self) -> None:
self._resolver = MarkdownReferenceResolver()
def _is_method(self, obj: docspec.ApiObject) -> bool:
return is_method(obj)
def _format_arglist(self, func: docspec.Function) -> str:
args = func.args[:]
if self._is_method(func) and args and args[0].name == "self":
args.pop(0)
return format_arglist(args)
def _render_toc(self, fp: t.TextIO, level: int, obj: docspec.ApiObject):
if level > self.toc_maxdepth:
return
object_id = self._resolver.generate_object_id(obj)
title = self._escape(obj.name)
if not self.add_module_prefix and isinstance(obj, docspec.Module):
title = title.split(".")[-1]
fp.write(" " * level + "* [{}](#{})\n".format(title, object_id))
level += 1
for child in getattr(obj, "members", []):
self._render_toc(fp, level, child)
def _render_header(self, fp: t.TextIO, level: int, obj: docspec.ApiObject):
if self.render_module_header_template and isinstance(obj, docspec.Module):
fp.write(
self.render_module_header_template.format(
module_name=obj.name, relative_module_name=obj.name.rsplit(".", 1)[-1]
)
)
return
object_id = self._resolver.generate_object_id(obj)
if self.use_fixed_header_levels:
# Read the header level based on the API object type. The default levels defined
# in the field will act as a first fallback, the level of the object inside it's
# hierarchy is the final fallback.
header_levels = {
**type(self).__dataclass_fields__["header_level_by_type"].default_factory(), # type: ignore
**self.header_level_by_type,
}
# Backwards compat for when we used "Data" instead of "Variable" which mirrors the docspec API
header_levels["Variable"] = header_levels.get("Data", header_levels["Variable"])
type_name = "Method" if self._is_method(obj) else type(obj).__name__
level = header_levels.get(type_name, level)
if self.insert_header_anchors and not self.html_headers:
fp.write('<a id="{}"></a>\n\n'.format(object_id))
if self.html_headers:
header_template = '<h{0} id="{1}">{{title}}</h{0}>'.format(level, object_id)
else:
header_template = level * "#" + " {title}"
if self.render_novella_anchors:
fp.write(f"@anchor pydoc:" + ".".join(x.name for x in obj.path) + "\n")
fp.write(header_template.format(title=self._get_title(obj)))
fp.write("\n\n")
def _format_decorations(self, decorations: t.List[docspec.Decoration]) -> t.Iterable[str]:
for dec in decorations:
yield "@{}{}\n".format(dec.name, dec.args or "")
def _yapf_code(self, code: str) -> str:
if not self.format_code:
return code
style_file = Path(self._context.directory) / self.format_code_style
style = str(style_file) if style_file.is_file() else self.format_code_style
return FormatCode(code, style_config=style)[0]
def _format_function_signature(
self, func: docspec.Function, override_name: str | None = None, add_method_bar: bool = True
) -> str:
parts: t.List[str] = []
if self.signature_with_decorators:
parts += self._format_decorations(func.decorations or [])
if self.signature_python_help_style and not self._is_method(func):
parts.append("{} = ".format(dotted_name(func)))
parts += [x + " " for x in func.modifiers or []]
if self.signature_with_def:
parts.append("def ")
if self.signature_class_prefix and self._is_method(func):
parent = func.parent
assert parent, func
parts.append(parent.name + ".")
parts.append((override_name or func.name))
parts.append(format_function_signature(func, self._is_method(func)))
result = "".join(parts)
result = self._yapf_code(result + ": pass").rpartition(":")[0].strip()
if add_method_bar and self._is_method(func):
result = "\n".join(" | " + l for l in result.split("\n"))
return result
def _format_classdef_signature(self, cls: docspec.Class) -> str:
bases = ", ".join(map(str, cls.bases or []))
if cls.metaclass:
if cls.bases:
bases += ", "
bases += "metaclass=" + str(cls.metaclass)
code = "class {}({})".format(cls.name, bases)
if self.signature_python_help_style:
code = dotted_name(cls) + " = " + code
code = self._yapf_code(code + ": pass").rpartition(":")[0].strip()
if cls.decorations and self.classdef_with_decorators:
code = "\n".join(self._format_decorations(cls.decorations)) + code
return code
def _format_data_signature(self, data: docspec.Variable) -> str:
expr = str(data.value)
code = self._yapf_code(data.name + " = " + expr).strip()
if len(code) > self.data_expression_maxlength:
code = code[: self.data_expression_maxlength] + " ..."
return code
def _render_signature_block(self, fp: t.TextIO, obj: docspec.ApiObject):
if self.classdef_code_block and isinstance(obj, docspec.Class):
code = self._format_classdef_signature(obj)
elif self.signature_code_block and isinstance(obj, docspec.Function):
code = self._format_function_signature(obj, add_method_bar=self.signature_with_vertical_bar)
elif self.data_code_block and isinstance(obj, docspec.Variable):
code = self._format_data_signature(obj)
else:
return
fp.write("```{}\n".format("python" if self.code_lang else ""))
fp.write(code)
fp.write("\n```\n\n")
def _render_object(self, fp: t.TextIO, level: int, obj: docspec.ApiObject):
if not isinstance(obj, docspec.Module) or self.render_module_header:
self._render_header(fp, level, obj)
render_view_source = not isinstance(obj, (docspec.Module, docspec.Variable))
if render_view_source:
url = self.source_linker.get_source_url(obj) if self.source_linker else None
source_string = self.source_format.replace("{url}", str(url)) if url else None
if source_string and self.source_position == "before signature":
fp.write(source_string + "\n\n")
self._render_signature_block(fp, obj)
if render_view_source:
if source_string and self.source_position == "after signature":
fp.write(source_string + "\n\n")
if obj.docstring:
docstring = html.escape(obj.docstring.content) if self.escape_html_in_docstring else obj.docstring.content
lines = docstring.split("\n")
if self.docstrings_as_blockquote:
lines = ["> " + x for x in lines]
fp.write("\n".join(lines))
fp.write("\n\n")
def _render_recursive(self, fp: t.TextIO, level: int, obj: docspec.ApiObject):
self._render_object(fp, level, obj)
level += 1
for member in getattr(obj, "members", []):
self._render_recursive(fp, level, member)
def _get_title(self, obj: docspec.ApiObject) -> str:
title = obj.name
if (self.add_method_class_prefix and self._is_method(obj)) or (
self.add_member_class_prefix and isinstance(obj, docspec.Variable)
):
title = (obj.parent.name + "." + title) if obj.parent else title
elif self.add_full_prefix and not self._is_method(obj):
title = dotted_name(obj)
if not self.add_module_prefix and isinstance(obj, docspec.Module):
title = title.split(".")[-1]
if isinstance(obj, docspec.Function):
if self.signature_in_header:
title += "(" + self._format_arglist(obj) + ")"
if isinstance(obj, docspec.Variable) and obj.datatype and self.render_typehint_in_data_header:
if self.code_headers:
title += f": {obj.datatype}"
elif self.html_headers:
title += f": <code>{obj.datatype}</code>"
else:
title += f": `{obj.datatype}`"
if self.code_headers:
if self.html_headers or self.sub_prefix:
if self.sub_prefix and "." in title:
prefix, title = title.rpartition(".")[::2]
title = "<sub>{}.</sub>{}".format(prefix, title)
title = "<code>{}</code>".format(title)
else:
title = "`{}`".format(title)
elif not self.html_headers:
title = self._escape(title)
if isinstance(obj, docspec.Module) and self.descriptive_module_title:
title = "Module " + title
if isinstance(obj, docspec.Class) and self.descriptive_class_title:
if self.descriptive_class_title is True:
title += " Objects"
elif self.descriptive_class_title is False:
pass
elif self.descriptive_class_title.startswith("$"):
title += self.descriptive_class_title[1:]
else:
title = self.descriptive_class_title + title
return title
def _escape(self, s):
return s.replace("_", "\\_").replace("*", "\\*")
def render_to_string(self, modules: t.List[docspec.Module]) -> str:
fp = io.StringIO()
self.render_single_page(fp, modules)
return fp.getvalue()
def _render_to_stream(self, modules: t.List[docspec.Module], stream: t.TextIO):
return self.render_single_page(stream, modules)
# SinglePageRenderer
def render_single_page(
self, fp: t.TextIO, modules: t.List[docspec.Module], page_title: t.Optional[str] = None
) -> None:
if self.render_page_title:
fp.write("# {}\n\n".format(page_title))
if self.render_toc:
if self.render_toc_title:
if self.render_page_title:
# set to level2 since level1 is page title
fp.write("## {}\n\n".format(self.render_toc_title))
else:
fp.write("# {}\n\n".format(self.render_toc_title))
for m in modules:
self._render_toc(fp, 0, m)
fp.write("\n")
for m in modules:
self._render_recursive(fp, 1, m)
# SingleObjectRenderer
def render_object(self, fp: t.TextIO, obj: docspec.ApiObject, options: t.Dict[str, t.Any]) -> None:
self._render_recursive(fp, 0, obj)
# Renderer
def get_resolver(self, modules: t.List[docspec.Module]) -> t.Optional[Resolver]:
"""
Returns a simple #Resolver implementation. Finds cross-references in the same file.
"""
return self._resolver
def render(self, modules: t.List[docspec.Module]) -> None:
if self.filename is None:
self._render_to_stream(modules, sys.stdout)
else:
with io.open(self.filename, "w", encoding=self.encoding) as fp:
self._render_to_stream(modules, t.cast(t.TextIO, fp))
# PluginBase
def init(self, context: Context) -> None:
if self.source_linker:
self.source_linker.init(context)
self._context = context
@dataclasses.dataclass
class MarkdownReferenceResolver(Resolver, ResolverV2):
local: bool = True
global_: bool = False
def generate_object_id(self, obj: docspec.ApiObject) -> str:
return ".".join(o.name for o in obj.path)
def _resolve_reference_in_members(
self, obj: t.Optional[docspec.ApiObject], ref: t.List[str]
) -> t.Optional[docspec.ApiObject]:
if not obj:
return None
for part_name in ref:
obj = docspec.get_member(obj, part_name)
if not obj:
return None
return obj
def _resolve_local_reference(
self, scope: docspec.ApiObject, ref_split: t.List[str]
) -> t.Optional[docspec.ApiObject]:
obj: t.Optional[docspec.ApiObject] = scope
while obj:
resolved = self._resolve_reference_in_members(obj, ref_split)
if resolved:
return resolved
obj = obj.parent
return None
# Resolver
def resolve_ref(self, scope: docspec.ApiObject, ref: str) -> t.Optional[str]:
target = self._resolve_local_reference(scope, ref.split("."))
if target:
return "#" + self.generate_object_id(target)
return None
# ResolverV2
def resolve_reference(self, suite: ApiSuite, scope: docspec.ApiObject, ref: str) -> t.Optional[docspec.ApiObject]:
"""Resolves the reference by searching in the members of *scope* or any of its parents."""
# TODO (@NiklasRosenstein): Support resolving indirections
ref_split = ref.split(".")
resolved = self._resolve_local_reference(scope, ref_split)
if resolved:
return resolved
if self.global_:
def _recurse(obj: docspec.ApiObject) -> t.Optional[docspec.ApiObject]:
resolved = self._resolve_reference_in_members(obj, ref_split)
if resolved:
return resolved
if isinstance(obj, docspec.HasMembers):
for member in obj.members:
resolved = _recurse(member)
if resolved:
return resolved
return None
for module in suite:
resolved = _recurse(module)
if resolved:
return resolved
return None