|
1 | 1 | import re
|
2 | 2 | from pathlib import Path
|
3 | 3 |
|
| 4 | +import dash_bootstrap_components as dbc |
4 | 5 | import dash_core_components as dcc
|
5 | 6 | import dash_html_components as html
|
6 | 7 | import markdown
|
7 | 8 |
|
8 |
| -from .api_doc import ApiDoc |
9 | 9 | from .helpers import ExampleContainer, load_source_with_environment
|
10 |
| -from .metadata import get_component_metadata |
11 | 10 |
|
12 | 11 | __all__ = ["parse"]
|
13 | 12 |
|
|
17 | 16 | SPLIT_PATTERN = re.compile(r"{{.*}}")
|
18 | 17 | EXAMPLE_DOC_PATTERN = re.compile(r"{{(.*)}}")
|
19 | 18 |
|
| 19 | +# To format the docstrings |
| 20 | +VERBATIM_PATTERN = re.compile(r"`((\\\[)((.|\n)*?)(\\]))`") |
| 21 | +LINK_PATTERN = re.compile(r"\\\[([\w\.\-:\/]+)\\\]\(([\w\.\-:#\/]+)\)") |
| 22 | +PROP_OPTIONAL_DEFAULT_PATTERN = re.compile(r"""default (.*)\)""") |
| 23 | +PROP_TYPE_PATTERN = re.compile(r"""(\s*- \w+?\s*\()([^;]*);""") |
| 24 | +PROP_NAME_PATTERN = re.compile(r"""(\n- )(\w+?) \(""") |
| 25 | +NESTED_PROP_NAME_PATTERN = re.compile(r"""(\n\s+- )(\w+?) \(""") |
| 26 | + |
20 | 27 |
|
21 | 28 | def parse(app, markdown_path, extra_env_vars=None):
|
22 | 29 | extra_env_vars = extra_env_vars or {}
|
@@ -53,7 +60,9 @@ def _parse_block(block, app, extra_env_vars):
|
53 | 60 | if type_ == "example":
|
54 | 61 | return _parse_example(data, app, extra_env_vars)
|
55 | 62 | elif type_ == "apidoc":
|
56 |
| - return _parse_doc(data) |
| 63 | + _, filename = data.rsplit("/", 1) |
| 64 | + component_name = filename[:-3] |
| 65 | + return component_reference(component_name) |
57 | 66 | elif type_ == "code-example":
|
58 | 67 | return _parse_code_example(data)
|
59 | 68 | raise ValueError("Unable to parse block.")
|
@@ -92,15 +101,43 @@ def _parse_code_example(data):
|
92 | 101 | )
|
93 | 102 |
|
94 | 103 |
|
95 |
| -def _parse_doc(data): |
96 |
| - _, filename = data.rsplit("/", 1) |
97 |
| - return ApiDoc(get_component_metadata(data), component_name=filename[:-3]) |
98 |
| - |
99 |
| - |
100 | 104 | def _interleave(l1, l2):
|
101 | 105 | n = len(l2)
|
102 | 106 | out = []
|
103 | 107 | for x in zip(l1[:n], l2):
|
104 | 108 | out.extend(x)
|
105 | 109 | out.extend(l1[n:])
|
106 | 110 | return out
|
| 111 | + |
| 112 | + |
| 113 | +def component_reference(component_name): |
| 114 | + component = getattr(dbc, component_name) |
| 115 | + component_doc = component.__doc__ |
| 116 | + |
| 117 | + return_div = [ |
| 118 | + dcc.Markdown("### Keyword arguments for {}".format(component_name)) |
| 119 | + ] |
| 120 | + |
| 121 | + docs = component_doc.split("Keyword arguments:")[-1] |
| 122 | + |
| 123 | + docs = re.sub(VERBATIM_PATTERN, r"`[\3]`", docs) |
| 124 | + # format links |
| 125 | + docs = re.sub(LINK_PATTERN, r"[\1](\2)", docs) |
| 126 | + |
| 127 | + # formats the prop defaults |
| 128 | + docs = re.sub(PROP_OPTIONAL_DEFAULT_PATTERN, r"default `\1`)", docs) |
| 129 | + |
| 130 | + # formats the prop type |
| 131 | + docs = re.sub(PROP_TYPE_PATTERN, r"\1*\2*;", docs) |
| 132 | + |
| 133 | + # formats the prop name on first level only |
| 134 | + docs = re.sub(PROP_NAME_PATTERN, r"\1**`\2`** (", docs) |
| 135 | + |
| 136 | + # formats keys of nested dicts |
| 137 | + docs = re.sub(NESTED_PROP_NAME_PATTERN, r"\1**`\2`** (", docs) |
| 138 | + |
| 139 | + # removes a level of nesting |
| 140 | + docs = docs.replace("\n-", "\n") |
| 141 | + |
| 142 | + return_div.append(dcc.Markdown(docs)) |
| 143 | + return html.Div(return_div, className="reference") |
0 commit comments