docstate 0.2.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,305 @@
1
+ Metadata-Version: 2.4
2
+ Name: docstate
3
+ Version: 0.2.3
4
+ Summary: Generate Markdown blocks from JSON facts and fail when the document drifts
5
+ Author: The docstate authors
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/XinBuilds/docstate
8
+ Project-URL: Repository, https://github.com/XinBuilds/docstate
9
+ Project-URL: Issues, https://github.com/XinBuilds/docstate/issues
10
+ Project-URL: Changelog, https://github.com/XinBuilds/docstate/blob/main/CHANGELOG.md
11
+ Keywords: markdown,documentation,generated-content,drift,cli
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: POSIX
16
+ Classifier: Operating System :: MacOS
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Documentation
22
+ Classifier: Topic :: Software Development :: Quality Assurance
23
+ Requires-Python: >=3.11
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Provides-Extra: dev
27
+ Requires-Dist: pytest>=8; extra == "dev"
28
+ Dynamic: license-file
29
+
30
+ # docstate
31
+
32
+ Generate Markdown blocks from JSON facts, and fail when the document drifts from them.
33
+
34
+ [![test](https://github.com/XinBuilds/docstate/actions/workflows/test.yml/badge.svg)](https://github.com/XinBuilds/docstate/actions/workflows/test.yml)
35
+
36
+ `docstate` rewrites the content between two HTML comment markers in your Markdown with values rendered from a JSON file. A second command compares the two and exits non-zero on drift, so stale facts fail CI instead of quietly misleading readers.
37
+
38
+ - No runtime dependencies: Python 3.11+ standard library only.
39
+ - Declarative: templates are plain Markdown files and configuration is TOML. No code lives inside documents.
40
+ - Atomic: `render` validates every target file first and writes nothing if a marker is missing, duplicated, unpaired, or a JSON/template error is found.
41
+ - Table rows: `scalar-v2` repeats a template body for each item in a JSON array.
42
+ - Frontmatter: validate `status` values, owner `project_stage`, and content changes that skipped a status review.
43
+ - Anchors: commit SHAs written in documents must match a configured external repository; live status phrases stay in owner documents.
44
+ - Markdown-aware: markers inside fenced code blocks and YAML frontmatter are ignored.
45
+ - Focused: it checks generated-block equality and inline relative file links. It is not a link checker, a prose linter, or a JSON validator.
46
+
47
+ ## Install
48
+
49
+ Not on PyPI yet. Install the v0.2.3 tag:
50
+
51
+ ```sh
52
+ pipx install "git+https://github.com/XinBuilds/docstate.git@v0.2.3"
53
+ ```
54
+
55
+ Or install a wheel from the v0.2.3 release:
56
+
57
+ ```sh
58
+ pip install https://github.com/XinBuilds/docstate/releases/download/v0.2.3/docstate-0.2.3-py3-none-any.whl
59
+ ```
60
+
61
+ Or install from a checkout:
62
+
63
+ ```sh
64
+ git clone https://github.com/XinBuilds/docstate.git
65
+ pipx install ./docstate
66
+ ```
67
+
68
+ Requires Python 3.11 or newer.
69
+
70
+ ## Quickstart
71
+
72
+ ```sh
73
+ cp -r examples/basic my-docs
74
+ cd my-docs
75
+
76
+ docstate check --config docstate.toml --strict # exits 0: the block matches the JSON
77
+ ```
78
+
79
+ Change a number in `facts/status.json` (for example `runs` from `28` to `31`) and run:
80
+
81
+ ```sh
82
+ docstate check --config docstate.toml --strict # exits 1 and prints file:line with expected/actual
83
+ docstate render --config docstate.toml # rewrites only the marked block
84
+ docstate check --config docstate.toml --strict # exits 0 again
85
+ ```
86
+
87
+ The example project is:
88
+
89
+ ```
90
+ examples/basic/
91
+ docstate.toml # facts, blocks, link-check globs
92
+ facts/status.json # values produced by your own script
93
+ templates/status.md # template for the generated block
94
+ docs/status.md # Markdown that contains the block
95
+ docs/guide.md # relative-link target
96
+ ```
97
+
98
+ For array-driven tables, see `examples/table/`. It uses `dialect = "scalar-v2"` and a `{% for %}` loop.
99
+
100
+ ## How it works
101
+
102
+ 1. A JSON file holds the facts. Your own script produces it; `docstate` does not validate its calculations.
103
+ 2. A Markdown template turns facts into block content with placeholders such as `{{ status.runs }}` or `{{ status.rate : .4f }}`.
104
+ 3. `docstate.toml` maps each block to a document, a fact source, and a template.
105
+ 4. The document contains the block markers:
106
+
107
+ ```markdown
108
+ <!-- BEGIN GENERATED: status-summary -->
109
+ content here is replaced by render
110
+ <!-- END GENERATED: status-summary -->
111
+ ```
112
+
113
+ `render` replaces only the lines between the markers and keeps everything else byte-for-byte. The markers are HTML comments, so they are invisible in rendered Markdown.
114
+
115
+ To make drift fail in CI, add two steps:
116
+
117
+ ```yaml
118
+ - run: pipx install "git+https://github.com/XinBuilds/docstate.git@v0.2.3"
119
+ - run: docstate check --config docstate.toml --strict
120
+ ```
121
+
122
+ ## Configuration
123
+
124
+ `docstate.toml`:
125
+
126
+ | Key | Meaning |
127
+ | --- | --- |
128
+ | `version` | Config format version. Must be `1`. |
129
+ | `dialect` | Template dialect. The only supported value in v0.1 is `scalar-v1`. |
130
+ | `[[facts]]` | `id` (identifier) and `path` (JSON file). Paths are relative to the config file; fact files may live outside the config directory and are read-only. |
131
+ | `[[blocks]]` | `id`, `file` (Markdown document), `facts` (facts id), `template` (Markdown template). |
132
+ | `[check]` | `files`: glob patterns for relative-link checking. `exclude`: glob patterns to skip. Defaults to the block documents when `files` is omitted. |
133
+
134
+ Identifiers match `[A-Za-z0-9][A-Za-z0-9_-]{0,63}`. Paths must be relative and must not escape the config directory, except fact paths, which may point to a read-only JSON file elsewhere. `facts` must end in `.json`; `file` and `template` must end in `.md`. Symlinks are rejected.
135
+
136
+ ## Templates
137
+
138
+ A template is a Markdown file with placeholders:
139
+
140
+ ```markdown
141
+ - Runs: {{ status.runs }}
142
+ - Rate: {{ status.rate : .4f }}
143
+ ```
144
+
145
+ - `{{ a.b.c }}` looks up nested keys in the facts object. Missing keys and null values are errors.
146
+ - `{{ key : spec }}` applies Python's format specification, so `{{ status.rate : .4f }}` renders `0.1071`.
147
+ - Booleans render as `true`/`false`. A scalar placeholder rejects lists and objects.
148
+ - Values are inserted verbatim. Escape HTML yourself when generating HTML documents.
149
+
150
+ `scalar-v2` adds a line-oriented loop:
151
+
152
+ ```markdown
153
+ | 判断 | 状态 |
154
+ |---|---|
155
+ {% for j in judgments %}
156
+ | {{ j.name }} | {{ j.state }} |
157
+ {% endfor %}
158
+ ```
159
+
160
+ - `{% for %}` and `{% endfor %}` must occupy whole lines. The tag lines are not emitted; the lines between them repeat once per array item.
161
+ - The loop source must be a JSON array. The loop variable joins the lookup scope, so `{{ j }}` reads the item itself and `{{ j.name }}` reads a field.
162
+ - Empty arrays emit no rows. Nested loops are rejected in v0.2; `endfor` without `for` is an error.
163
+ - `scalar-v1` rejects loop tags with `LOOP_UNSUPPORTED`. `if`, indexing, expressions, and function calls are not implemented.
164
+
165
+ ## Exit codes
166
+
167
+ | Code | Meaning |
168
+ | --- | --- |
169
+ | `0` | No findings. A non-strict `check` also exits `0` when it reports findings. |
170
+ | `1` | `check --strict` found an error-severity finding: drift, a broken relative link, an invalid status, a stage mismatch, or a stale date. Warnings do not fail strict mode. |
171
+ | `2` | Bad config, unreadable or invalid JSON, unpaired/duplicate/nested markers, template or write error. No files are written. |
172
+
173
+ For automation, `docstate check --json` prints one JSON object instead of text:
174
+
175
+ ```json
176
+ {
177
+ "files": 0,
178
+ "blocks": 0,
179
+ "counts": {"error": 0, "warning": 2},
180
+ "findings": [
181
+ {
182
+ "severity": "warning",
183
+ "kind": "anchor",
184
+ "path": "README.md",
185
+ "line": 12,
186
+ "field": "external HEAD",
187
+ "message": "anchor abc1234 does not match HEAD def5678 (behind 1)",
188
+ "block_id": "",
189
+ "expected": "",
190
+ "actual": "",
191
+ "target": ""
192
+ }
193
+ ]
194
+ }
195
+ ```
196
+
197
+ ## Error codes
198
+
199
+ Errors are printed as `path:line: error[CODE]: message` to stderr.
200
+
201
+ | Group | Codes |
202
+ | --- | --- |
203
+ | Config | `CFG_READ`, `CFG_PARSE`, `CFG_SCHEMA`, `CFG_VERSION`, `CFG_DIALECT`, `CFG_PATH` |
204
+ | Facts | `FACTS_READ`, `FACTS_ENCODING`, `FACTS_JSON`, `FACTS_DUPLICATE_KEY`, `FACTS_TYPE` |
205
+ | Markers | `MARKER_MISSING`, `MARKER_UNKNOWN`, `MARKER_DUPLICATE`, `MARKER_NESTED`, `MARKER_UNPAIRED` |
206
+ | Templates and values | `TPL_READ`, `TPL_PLACEHOLDER`, `VALUE_MISSING`, `VALUE_NULL`, `VALUE_NOT_SCALAR`, `FORMAT_INVALID` |
207
+ | Loops (`scalar-v2`) | `LOOP_UNSUPPORTED`, `LOOP_SYNTAX`, `LOOP_NOT_LIST`, `LOOP_UNPAIRED`, `LOOP_NESTED` |
208
+ | Files | `FILE_READ`, `FILE_ENCODING`, `FILE_SYMLINK`, `FILE_WRITE` |
209
+
210
+ Findings are printed as `path:line: error[kind:field]: message` or `warning[...]`. Fatal project errors (exit 2) keep the error-code table above.
211
+
212
+ ## Frontmatter and freshness checks
213
+
214
+ `check` can also read flat YAML frontmatter. The parser reads scalar `key: value` lines; lists and nested mappings are ignored, and no YAML package is required.
215
+
216
+ ```toml
217
+ [check.frontmatter]
218
+ files = ["docs/**/*.md"]
219
+ require = ["status", "last_reviewed"]
220
+ status_field = "status"
221
+ status_values = ["draft", "review", "decided", "implemented", "superseded", "archived"]
222
+ updated_field = "updated"
223
+ bom = "warn" # ignore | warn | error
224
+ status_review = "warn" # off | warn | error
225
+
226
+ [[check.frontmatter.owners]]
227
+ owner = "docs/README.md"
228
+ field = "project_stage"
229
+ applies_to = ["docs/**/*.md"]
230
+
231
+ [[check.freshness]]
232
+ doc = "docs/README.md"
233
+ field = "last_reviewed"
234
+ changelog = "docs/CHANGELOG.md"
235
+ ```
236
+
237
+ Rules:
238
+
239
+ - `require` fields must exist and be non-empty. `status_values`, when set, rejects other values.
240
+ - `bom` reports a UTF-8 BOM; `warn` is the default, `ignore` disables it.
241
+ - An owner declares the authoritative value of a field (for example `project_stage`). Other matched documents may omit the field, but if they set it, the value must match the owner.
242
+ - `status_review` uses git. If the working tree changed a file, it compares the file with `HEAD`; otherwise it compares `HEAD` with `HEAD~1`. If the body changed but both the status field and `updated` stayed the same, it reports `status` (warning by default). If the status changed but `updated` did not, it reports an error.
243
+ - `[[check.freshness]]` reports when a date field is older than the newest dated heading in a changelog, or when the field is missing or malformed.
244
+ - `status_review = "off"` avoids git entirely. When enabled, the directory must be a git repository; untracked files are skipped.
245
+
246
+ This is a discipline check, not a semantic verdict. It cannot decide that `status: draft` should have become `decided`; it can only prove the field was not reviewed, is outside the vocabulary, or disagrees with its owner. The status check looks at the working tree or the latest commit, not the whole history.
247
+
248
+ ## Commit anchors and forbidden phrases
249
+
250
+ ```toml
251
+ [[check.anchors]]
252
+ files = ["README.md", "docs/status.md"]
253
+ repo = "../external-repo"
254
+ paths = ["projects/example"]
255
+ pattern = 'external HEAD[==]\s*`?([0-9a-f]{7,40})`?'
256
+ require = true
257
+ severity = "error" # warn | error
258
+
259
+ [[check.forbidden]]
260
+ files = ["docs/**/*.md"]
261
+ patterns = ["下一步[=::]", "待拍板", "当前状态[=::]"]
262
+ allow = ["docs/STATUS.md"]
263
+ severity = "warn"
264
+ ```
265
+
266
+ - `[check.anchors]` reads the configured regular expression from each file. The default pattern captures the SHA in `external HEAD=<sha>`. The last anchor must match the latest commit of `repo` limited to `paths`; short and full SHAs both work. A mismatch reports how many commits behind the anchor is. `require = false` allows files without an anchor.
267
+ - `[check.forbidden]` reports the first matching phrase on each line outside the `allow` files. Patterns are regular expressions.
268
+ - Both rules accept `severity = "warn" | "error"`. Use `warn` while migrating a project, then switch to `error` when the baseline is clean.
269
+ - `[check.anchors]` needs the `git` binary and a readable repository. `repo` and `paths` are resolved like fact paths; `repo` may live outside the config directory.
270
+
271
+ ## Limits in v0.1
272
+
273
+ - Link checking covers inline relative file links (`[text](path)`, `![alt](path)`) only. Reference-style links, HTML links, wikilinks, anchors, absolute paths, and external URLs are not checked. Run a separate [`lychee`](https://lychee.cli.rs/) pass for those.
274
+ - `check` never touches the network.
275
+ - Prose rules are out of scope. Run [`Vale`](https://vale.sh/) separately. `templates/vale.ini` and `templates/pre-commit.yaml` are starting points; copy them into your repository and install Vale or pre-commit yourself.
276
+ - `scalar-v2` supports `for` only. `if`, indexing, expressions, and function calls are not implemented; templates never execute code.
277
+ - Generated blocks contain no timestamps. Use your VCS commit time when you need one.
278
+ - There is no lock file or concurrent-render protection. Do not run `render` against the same tree in parallel.
279
+ - `docstate` guarantees that Markdown equals JSON. It does not check that JSON equals reality.
280
+ - Windows is untested; macOS and Linux are the supported v0.1 targets.
281
+
282
+ ## Development
283
+
284
+ ```sh
285
+ python -m venv .venv
286
+ . .venv/bin/activate
287
+ pip install -e ".[dev]"
288
+ pytest
289
+ ```
290
+
291
+ Layout:
292
+
293
+ ```
294
+ src/docstate/ cli.py, config.py, render.py, check.py, ...
295
+ tests/ unit and contract tests plus fixtures
296
+ examples/basic/ runnable example used by the test suite
297
+ examples/table/ scalar-v2 table-row example
298
+ templates/ config schema and optional linter samples
299
+ ```
300
+
301
+ Releases: bump `__version__` in `src/docstate/__init__.py`, update `CHANGELOG.md`, then tag `vX.Y.Z`. See [RELEASING.md](RELEASING.md) for the full sync and release routine, and [CHANGELOG.md](CHANGELOG.md) for the history.
302
+
303
+ ## License
304
+
305
+ MIT. See [LICENSE](LICENSE).
@@ -0,0 +1,17 @@
1
+ docstate/__init__.py,sha256=7qHFJNvuNShqHC220M5YlKaB_0Ruw2HSGDq7MEbyhGo,93
2
+ docstate/__main__.py,sha256=dvqRbpPgrhpsG7UdzAvPKJwBTp92CkcWX4qEQBzPQdc,49
3
+ docstate/check.py,sha256=TTdoWeQuwMZ7GcPM_66moxhOZDma3geF7N5uKrr3OKU,17853
4
+ docstate/cli.py,sha256=RLNSw2hE2_DQe_yoFuSfMqzDzZXhPGGa68PzaUm-YCQ,2832
5
+ docstate/config.py,sha256=0SPQIKLnhNAuXYSj1f96IZQ-4Z3Xt6yRt0TuzN8DGD4,20157
6
+ docstate/errors.py,sha256=Z6xifCnwBwfmam8qbgniaXGW1PZIWaYHJvRW_fCUTqI,908
7
+ docstate/facts.py,sha256=ihPHbgxC_u96JjW_71qukH-0r9g4KwGPH5Zh7hIMiQM,1598
8
+ docstate/frontmatter.py,sha256=LAmdOKGDGWRP80WR5tCwBuYb6q77tQSgkdUv5-FZJUk,1851
9
+ docstate/render.py,sha256=SzTk4sEKKG4D5Pg87QZp_Cu3PL_DKl5rtpabmtq5U8g,6616
10
+ docstate/scan.py,sha256=iSgN-16_bAFSDf9_WQGmVllqi3GE2702zPd_5fAIRO0,3336
11
+ docstate/template.py,sha256=8TXLdZVFOk57pqnG2KXBbLSsuNE5nETbG2ywp9NZOfU,5445
12
+ docstate-0.2.3.dist-info/licenses/LICENSE,sha256=MXklOXPVaZjTMdzF9SOWLb7zI7JbU4QOgBfv_VlWTRc,1077
13
+ docstate-0.2.3.dist-info/METADATA,sha256=tSI7SUkEqKFGOTd6T2nqIRiRbMUuOPa3ygk_5P7JtqQ,13705
14
+ docstate-0.2.3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
15
+ docstate-0.2.3.dist-info/entry_points.txt,sha256=t3XeO2JKJFKWHv63g1F2Bc6GQD_zAn4oaBKQr05dytE,47
16
+ docstate-0.2.3.dist-info/top_level.txt,sha256=hV9RbKo9f2scwqpDClOki3DlD2mNtF1UnXoJNtwfINs,9
17
+ docstate-0.2.3.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ docstate = docstate.cli:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 The docstate authors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ docstate