A homemade replacement for mkdocstrings
¶
In these days I am tidying up the documentation of a legacy codebase, where "legacy" stands for something in between spaghetti code and "things I wish I've done differently". Since I wish the output of the work to be as much complete as possible, I decided to include also the source code in the new docs built with mkdocs-material.
The go-to choice could have been mkdocstrings, but I wanted to avoid some setup complexity1 so I wrote down a custom source code collector.
source_code_collector.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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
At the end of the day, it boils down to:
- traverse the source code starting from the given
source_code_path
; - replicate the source code folders tree in the
target_dir
docs subfolder; - embed each raw source code file in a generated markdown with the
--8<--
notation.
Some additional tricks are highlighted in the snippet:
- at line 52, the generated markdown is excluded from the scope of the search plugin;
- at line 57, the collected source code is wrapped into an admonition;
- at line 58, the extension of the collected source code file is used for syntax highlighting.
-
For example, the codebase should be available as a proper Python package in the docs build environment for docstrings collection purposes. ↩