|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | def main(): |
| 3 | + import json |
| 4 | + import os.path as path |
| 5 | + import re |
| 6 | + from functools import lru_cache |
| 7 | + |
| 8 | + import pdoc |
3 | 9 | from pdoc import cli |
4 | 10 |
|
| 11 | + # We don't document stuff on the index of the documentation, but pdoc doesn't know that, |
| 12 | + # so we have to patch the function that generates the index. |
| 13 | + def _patched_generate_lunr_search(top_module, index_docstrings, template_config): |
| 14 | + def trim_docstring(docstring): |
| 15 | + return re.sub( |
| 16 | + r""" |
| 17 | + \s+| # whitespace sequences |
| 18 | + \s+[-=~]{3,}\s+| # title underlines |
| 19 | + ^[ \t]*[`~]{3,}\w*$| # code blocks |
| 20 | + \s*[`#*]+\s*| # common markdown chars |
| 21 | + \s*([^\w\d_>])\1\s*| # sequences of punct of the same kind |
| 22 | + \s*</?\w*[^>]*>\s* # simple HTML tags |
| 23 | + """, |
| 24 | + " ", |
| 25 | + docstring, |
| 26 | + flags=re.VERBOSE | re.MULTILINE, |
| 27 | + ) |
| 28 | + |
| 29 | + def recursive_add_to_index(dobj): |
| 30 | + url = to_url_id(dobj.module) |
| 31 | + if url != 0: # 0 is index.html |
| 32 | + info = { |
| 33 | + "ref": dobj.refname, |
| 34 | + "url": url, |
| 35 | + } |
| 36 | + if index_docstrings: |
| 37 | + info["doc"] = trim_docstring(dobj.docstring) |
| 38 | + if isinstance(dobj, pdoc.Function): |
| 39 | + info["func"] = 1 |
| 40 | + |
| 41 | + index.append(info) |
| 42 | + |
| 43 | + for member_dobj in getattr(dobj, "doc", {}).values(): |
| 44 | + recursive_add_to_index(member_dobj) |
| 45 | + |
| 46 | + @lru_cache() |
| 47 | + def to_url_id(module): |
| 48 | + url = module.url() |
| 49 | + if top_module.is_package: # Reference from subfolder if its a package |
| 50 | + _, url = url.split("/", maxsplit=1) |
| 51 | + if url not in url_cache: |
| 52 | + url_cache[url] = len(url_cache) |
| 53 | + return url_cache[url] |
| 54 | + |
| 55 | + index = [] |
| 56 | + url_cache = {} |
| 57 | + recursive_add_to_index(top_module) |
| 58 | + urls = [i[0] for i in sorted(url_cache.items(), key=lambda i: i[1])] |
| 59 | + |
| 60 | + # If top module is a package, output the index in its subfolder, else, in the output dir |
| 61 | + main_path = path.join(cli.args.output_dir, *top_module.name.split(".") if top_module.is_package else "") |
| 62 | + with cli._open_write_file(path.join(main_path, "index.js")) as f: |
| 63 | + f.write("URLS=") |
| 64 | + json.dump(urls, f, indent=0, separators=(",", ":")) |
| 65 | + f.write(";\nINDEX=") |
| 66 | + json.dump(index, f, indent=0, separators=(",", ":")) |
| 67 | + |
| 68 | + # Generate search.html |
| 69 | + with cli._open_write_file(path.join(main_path, "search.html")) as f: |
| 70 | + rendered_template = pdoc._render_template("/search.mako", module=top_module, **template_config) |
| 71 | + f.write(rendered_template) |
| 72 | + |
| 73 | + cli._generate_lunr_search = _patched_generate_lunr_search |
| 74 | + |
5 | 75 | cli.main() |
6 | 76 |
|
7 | 77 |
|
|
0 commit comments