|
957 | 957 | "You can replace the default markdown show_doc renderer with custom renderers. For instance, nbdev comes with a simple example for rendering with raw HTML."
|
958 | 958 | ]
|
959 | 959 | },
|
| 960 | + { |
| 961 | + "cell_type": "code", |
| 962 | + "execution_count": null, |
| 963 | + "id": "5271cd9a", |
| 964 | + "metadata": {}, |
| 965 | + "outputs": [], |
| 966 | + "source": [ |
| 967 | + "#| export\n", |
| 968 | + "def _create_html_table(table_str):\n", |
| 969 | + " def split_row(row):\n", |
| 970 | + " return re.findall(r'\\|(?:(?:\\\\.|[^|\\\\])*)', row)\n", |
| 971 | + " \n", |
| 972 | + " def unescape_cell(cell): \n", |
| 973 | + " return cell.strip(' *|').replace(r'\\|', '|')\n", |
| 974 | + " \n", |
| 975 | + " lines = table_str.strip().split('\\n')\n", |
| 976 | + " header = [f\"<th>{unescape_cell(cell)}</th>\" for cell in split_row(lines[0])]\n", |
| 977 | + " rows = [[f\"<td>{unescape_cell(cell)}</td>\" for cell in split_row(line)] for line in lines[2:]]\n", |
| 978 | + " \n", |
| 979 | + " return f'''<table>\n", |
| 980 | + " <thead><tr>{' '.join(header)}</tr></thead>\n", |
| 981 | + " <tbody>{''.join(f'<tr>{\" \".join(row)}</tr>' for row in rows)}</tbody>\n", |
| 982 | + " </table>'''" |
| 983 | + ] |
| 984 | + }, |
| 985 | + { |
| 986 | + "cell_type": "code", |
| 987 | + "execution_count": null, |
| 988 | + "id": "a4ae5b2a", |
| 989 | + "metadata": {}, |
| 990 | + "outputs": [], |
| 991 | + "source": [ |
| 992 | + "#| export\n", |
| 993 | + "def _html_link(url, txt): return f'<a href=\"{url}\" target=\"_blank\" rel=\"noreferrer noopener\">{txt}</a>'" |
| 994 | + ] |
| 995 | + }, |
960 | 996 | {
|
961 | 997 | "cell_type": "code",
|
962 | 998 | "execution_count": null,
|
963 | 999 | "id": "147626ee",
|
964 | 1000 | "metadata": {},
|
965 | 1001 | "outputs": [],
|
966 | 1002 | "source": [
|
967 |
| - "#|export\n", |
968 |
| - "def _html_link(url, txt): return f'<a href=\"{url}\" target=\"_blank\" rel=\"noreferrer noopener\">{txt}</a>'\n", |
969 |
| - "\n", |
| 1003 | + "#| export\n", |
970 | 1004 | "class BasicHtmlRenderer(ShowDocRenderer):\n",
|
971 |
| - " \"Simple HTML renderer for `show_doc`\"\n", |
| 1005 | + " \"HTML renderer for `show_doc`\"\n", |
972 | 1006 | " def _repr_html_(self):\n",
|
973 | 1007 | " doc = '<hr/>\\n'\n",
|
| 1008 | + " src = NbdevLookup().code(self.fn)\n", |
974 | 1009 | " doc += f'<h{self.title_level}>{self.nm}</h{self.title_level}>\\n'\n",
|
975 |
| - " doc += f'<blockquote><pre><code>{self.nm}{_fmt_sig(self.sig)}</code></pre></blockquote>'\n", |
976 |
| - " if self.docs: doc += f\"<p><i>{self.docs}</i></p>\"\n", |
| 1010 | + " sig = _fmt_sig(self.sig) if self.sig else ''\n", |
| 1011 | + " # Escape < and > characters in the signature\n", |
| 1012 | + " sig = sig.replace('<', '<').replace('>', '>')\n", |
| 1013 | + " doc += f'<blockquote><pre><code>{self.nm} {sig}</code></pre></blockquote>'\n", |
| 1014 | + " if self.docs:\n", |
| 1015 | + " doc += f\"<p><i>{self.docs}</i></p>\"\n", |
| 1016 | + " if src: doc += f\"<br/>{_html_link(src, 'source')}\"\n", |
| 1017 | + " if self.dm.has_docment: doc += _create_html_table(str(self.dm))\n", |
977 | 1018 | " return doc\n",
|
978 | 1019 | "\n",
|
979 | 1020 | " def doc(self):\n",
|
980 | 1021 | " \"Show `show_doc` info along with link to docs\"\n",
|
981 | 1022 | " from IPython.display import display,HTML\n",
|
982 | 1023 | " res = self._repr_html_()\n",
|
983 |
| - " docs = NbdevLookup().doc(self.fn)\n", |
984 |
| - " if docs is not None: res += '\\n<p>' +_html_link(docs, \"Show in docs\") + '</p>'\n", |
985 | 1024 | " display(HTML(res))"
|
986 | 1025 | ]
|
987 | 1026 | },
|
|
1009 | 1048 | "text/html": [
|
1010 | 1049 | "<hr/>\n",
|
1011 | 1050 | "<h3>show_doc</h3>\n",
|
1012 |
| - "<blockquote><pre><code>show_doc(sym, renderer=None, name:str|None=None, title_level:int=3)</code></pre></blockquote><p>Show signature and docstring for `sym`</p>\n", |
1013 |
| - "<p><a href=\"https://nbdev.fast.ai/api/showdoc.html#show_doc\" target=\"_blank\" rel=\"noreferrer noopener\">Show in docs</a></p>" |
| 1051 | + "<blockquote><pre><code>show_doc (sym, renderer=None, name:str|None=None, title_level:int=3)</code></pre></blockquote><p><i>Show signature and docstring for `sym`</i></p><br/><a href=\"https://github.com/fastai/nbdev/blob/master/nbdev/showdoc.py#L182\" target=\"_blank\" rel=\"noreferrer noopener\">source</a><table>\n", |
| 1052 | + " <thead><tr><th></th> <th>Type</th> <th>Default</th> <th>Details</th> <th></th></tr></thead>\n", |
| 1053 | + " <tbody><tr><td>sym</td> <td></td> <td></td> <td>Symbol to document</td> <td></td></tr><tr><td>renderer</td> <td>NoneType</td> <td>None</td> <td>Optional renderer (defaults to markdown)</td> <td></td></tr><tr><td>name</td> <td>str | None</td> <td>None</td> <td>Optionally override displayed name of `sym`</td> <td></td></tr><tr><td>title_level</td> <td>int</td> <td>3</td> <td>Heading level to use for symbol name</td> <td></td></tr></tbody>\n", |
| 1054 | + " </table>" |
1014 | 1055 | ],
|
1015 | 1056 | "text/plain": [
|
1016 | 1057 | "<IPython.core.display.HTML object>"
|
|
0 commit comments