|
1 | | -"""Generate the code reference pages and navigation. |
| 1 | +"""Generate the code reference pages. |
2 | 2 |
|
3 | | -Copied from: |
4 | | -https://github.com/pawamoy/copier-pdm/blob/79135565c4c7f756204a5f460e87129649f8b704/project/docs/gen_ref_nav.py |
| 3 | +Adapted without navigation from: |
| 4 | +https://github.com/pawamoy/copier-pdm/blob/adff9b64887d0b4c9ec0b42de1698b34858a511e/project/scripts/gen_ref_nav.py |
5 | 5 |
|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from pathlib import Path |
9 | 9 |
|
10 | 10 | import mkdocs_gen_files |
| 11 | +from corallium.tomllib import tomllib |
11 | 12 |
|
12 | | -nav = mkdocs_gen_files.Nav() |
13 | 13 |
|
14 | | -for path in sorted(Path('tail_jsonl').rglob('*.py')): |
| 14 | +def has_public_code(line: str) -> bool: |
| 15 | + """Determine if a given line contains code that will be documented. |
| 16 | +
|
| 17 | + Returns: |
| 18 | + bool: True if line appears to be a public function, class, or method |
| 19 | +
|
| 20 | + """ |
| 21 | + for key in ('def', 'async def', 'class'): |
| 22 | + starts = line.startswith(f'{key} ') |
| 23 | + if starts and not line.startswith(f'{key} _'): |
| 24 | + return True |
| 25 | + if starts: |
| 26 | + break |
| 27 | + return False |
| 28 | + |
| 29 | + |
| 30 | +_config = tomllib.loads(Path('pyproject.toml').read_text(encoding='utf-8')) |
| 31 | +_pkg_name = _config['tool']['poetry']['name'] |
| 32 | +src = Path(_pkg_name) |
| 33 | +for path in sorted(src.rglob('*.py')): |
| 34 | + for line in path.read_text().split('\n'): |
| 35 | + if has_public_code(line): |
| 36 | + break |
| 37 | + else: |
| 38 | + continue # Do not include the file in generated documentation |
| 39 | + |
15 | 40 | module_path = path.with_suffix('') |
16 | 41 | doc_path = path.with_suffix('.md') |
17 | 42 | full_doc_path = Path('reference', doc_path) |
18 | 43 |
|
19 | 44 | parts = tuple(module_path.parts) |
20 | | - |
21 | 45 | if parts[-1] == '__init__': |
22 | 46 | parts = parts[:-1] |
23 | 47 | doc_path = doc_path.with_name('index.md') |
24 | 48 | full_doc_path = full_doc_path.with_name('index.md') |
25 | | - elif parts[-1] == '__main__': |
| 49 | + elif parts[-1].startswith('_'): |
26 | 50 | continue |
27 | 51 |
|
28 | | - nav[parts] = doc_path.as_posix() |
29 | | - |
30 | 52 | with mkdocs_gen_files.open(full_doc_path, 'w') as fd: |
31 | 53 | ident = '.'.join(parts) |
32 | 54 | fd.write(f'::: {ident}') |
33 | 55 |
|
34 | 56 | mkdocs_gen_files.set_edit_path(full_doc_path, path) |
35 | | - |
36 | | -# FYI, to add pages manually, use: `nav["package", "module"] = "path/to/file.md"` |
37 | | - |
38 | | -with mkdocs_gen_files.open('reference/SUMMARY.md', 'w') as nav_file: |
39 | | - nav_file.writelines(nav.build_literate_nav()) |
|
0 commit comments