snippet¶
Core snippet replacement logic.
snippet ¶
Core snippet replacement logic.
This module contains the main logic for finding and replacing snippet blocks in markdown files. It handles parsing SNIPPET-START/SNIPPET-END markers, comparing hashes to detect changes, and updating file contents.
replace_blocks(md_path, repo_root, snippet_root, snippet_ext, cache, dry_run=False) ¶
Process a markdown file and replace snippet blocks with central repo content.
Walks the file, finds each SNIPPET-START/SNIPPET-END pair, and rewrites the block only when the cached hash differs from the authoritative snippet hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
md_path | Path | Path to the markdown file to process. | required |
repo_root | Path | Root of the git repository. | required |
snippet_root | Path | Root directory of the cloned snippet repo. | required |
snippet_ext | str | File extension for snippet files. | required |
cache | dict[str, str] | Mutable cache dictionary to update. | required |
dry_run | bool | If True, don't write changes to disk. | False |
Returns:
| Type | Description |
|---|---|
bool | True if the file was (or would be) modified, False otherwise. |
Source code in pre_commit_snippet/snippet.py
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |