forked from tqdm/tqdm.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpydoc_markdown_tqdm.py
27 lines (22 loc) · 1.03 KB
/
pydoc_markdown_tqdm.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
from pydoc_markdown.contrib.processors.pydocmd import PydocmdProcessor
import re
from functools import partial
sub = partial(re.sub, flags=re.M)
class TqdmProcessor(PydocmdProcessor):
def _process(self, node):
if not getattr(node, "docstring", None):
return
# join long lines ending in escape (\)
c = sub(r"\\\n\s*", "", node.docstring.content)
# escape literal `*`
c = sub(r"^(\w+\s{2,}:.*?)\*(.*?)$", r"\1\\*\2", c)
# convert parameter lists to markdown list
c = sub(r"^(\w+)\s{2,}(:.*?)$", r"* __\1__*\2* ", c)
# convert REPL code blocks to code
c = sub(r"^(>>>|\.\.\.)(.*?)$", r"```\n\1\2\n```", c)
c = sub(r"^(>>>|\.\.\.)(.*?)\n```\n```\n(>>>|\.\.\.)", r"\1\2\n\3", c)
c = sub(r"^(>>>|\.\.\.)(.*?)\n```\n```\n(>>>|\.\.\.)", r"\1\2\n\3", c)
c = sub(r"^(```)(\n>>>)", r"\1python\2", c)
# hide <h2> from `nav`
node.docstring.content = sub(r"^(.+?)\n[-]{4,}$", r"__\1__\n", c)
return super()._process(node)