code_snippet_to_doc
code_snippet_to_doc keeps code snippets in documentation files in sync with source code. It replaces the content between special comments with the corresponding lines from source files, ensuring that code examples in documentation never go stale. It can be invoked as a pre-commit hook or as a standalone script. Documentation in Markdown and RestructuredText formats is supported.
How it works
Markdown
Add HTML comments to your Markdown file indicating which source file and line range to include:
Using line numbers:
<!-- code_snippet_start:path/to/file.c:10:20 -->
<!-- code_snippet_end -->
Using search patterns (glob-style):
<!-- code_snippet_start:path/to/file.c:/int main/:/return/ -->
<!-- code_snippet_end -->
Using regular expressions (prefix with r):
<!-- code_snippet_start:path/to/file.c:r/^int main/:r/^\}/ -->
<!-- code_snippet_end -->
Regex patterns support the full Python re syntax, including anchors like ^ (start of line) and $ (end of line).
Including or excluding the end line — by default, the end line is excluded from the snippet. Add a + suffix to include it:
<!-- code_snippet_start:path/to/file.c:r/^int main/:r/^\}/+ -->
<!-- code_snippet_end -->
The + suffix works with all end line specification types: line numbers, glob patterns, and regular expressions. It is convenient for extracting complete blocks like C function bodies, where the closing } should be included.
Colons inside search patterns must be escaped with \:.
File paths are resolved relative to the document file.
RestructuredText
Add RST comments to your .rst file using the same colon-separated syntax:
Using line numbers:
.. code_snippet_start:path/to/file.c:10:20
.. code_snippet_end
Using search patterns and regular expressions:
.. code_snippet_start:path/to/file.c:r/^int main/:r/^\}/+
.. code_snippet_end
The generated output uses RST's code-block directive with proper indentation:
.. code_snippet_start:path/to/file.c:r/^int main/:r/^\}/+
.. code-block:: c
int main(int argc, char *argv[]) {
printf("Hello, World!\n");
return 0;
}
.. code_snippet_end
All line specification types (line numbers, glob patterns, regex, + suffix) work the same way in both Markdown and RST files. The format is auto-detected from the file extension.
Then run code_snippet_to_doc — the area between the comments will be populated with a code block containing the specified source lines. The language for syntax highlighting is detected automatically from the file extension.
Usage as a pre-commit hook
Add to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/igrr/code_snippet_to_doc.git
rev: v0.1.0
hooks:
- id: code_snippet_to_doc
To update additional files beyond README.md, specify them in args::
repos:
- repo: https://github.com/igrr/code_snippet_to_doc.git
rev: v0.1.0
hooks:
- id: code_snippet_to_doc
args: [--input=README.md, --input=docs/GUIDE.md, --input=docs/api.rst]
Command-line usage
def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(prog="code_snippet_to_doc")
parser.add_argument(
"-i",
"--input",
nargs="+",
type=argparse.FileType("r+"),
help="Documentation file to update (can be specified multiple times).",
)
parser.add_argument(
"--check",
action="store_true",
help="Check if the files need to be updated, but don't modify them. "
"Non-zero exit code is returned if any file needs to be updated.",
)
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
return parser
Options:
-i, --input: Documentation files to update.--check: Check if files need updating without modifying them. Returns exit code 2 if changes are needed.--version: Show version.
Example
Given a source file example.c:
#include <stdio.h>
// Main function
int main(int argc, char *argv[]) {
printf("Hello, World!\n");
return 0;
}
And a Markdown file containing:
<!-- code_snippet_start:example.c:r/^int main/:r/^\}/+ -->
<!-- code_snippet_end -->
Running code_snippet_to_doc -i README.md will populate the snippet block with the main function body from example.c.
License
This tool is Copyright (c) 2026 Ivan Grokhotkov and distributed under the MIT License.
Metadata
Release files for code-snippet-to-doc 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| code_snippet_to_doc-0.1.0.tar.gz | 14.2 kB | With provenance | Details for code_snippet_to_doc-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| code_snippet_to_doc-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for code_snippet_to_doc-0.1.0-py3-none-any.whl |
Total release size: 23.9 kB
Release files / code_snippet_to_doc-0.1.0.tar.gz
| Download URL | code_snippet_to_doc-0.1.0.tar.gz |
|---|---|
| Size | 14.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b5027868cec205ec71975b13fe392c8906c53d3f5c25ee8472b194ee59d2df0
|
|
BLAKE2b-256 checksum How to use checksums |
2822a24821f8bd4b1db7fb7e2ddcfc71d21aebcb61ba43b14b4b3382b66cf706
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 17, 2026.
Transparency logRelease files / code_snippet_to_doc-0.1.0-py3-none-any.whl
| Download URL | code_snippet_to_doc-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5bebcd943e613046bb00a2005c75b158e2e885213e488e5dd4571cd15dffc050
|
|
BLAKE2b-256 checksum How to use checksums |
eb5344266dbd9e75ed42eed6716805bcfdc13055b3e737c429e006d9764d51d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 17, 2026.
Transparency logSecurity for code-snippet-to-doc 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for code_snippet_to_doc-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 17, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.