tracecite-docs 0.0.1__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.
- tracecite_docs/__init__.py +35 -0
- tracecite_docs/__main__.py +3 -0
- tracecite_docs/cli.py +580 -0
- tracecite_docs/docs/__init__.py +56 -0
- tracecite_docs/docs/build.py +382 -0
- tracecite_docs/docs/config.py +114 -0
- tracecite_docs/docs/contract.py +17 -0
- tracecite_docs/docs/modes.py +240 -0
- tracecite_docs/docs/stage.py +408 -0
- tracecite_docs/docs/vectorize.py +458 -0
- tracecite_docs/tables/__init__.py +41 -0
- tracecite_docs/tables/_text.py +142 -0
- tracecite_docs/tables/document.py +582 -0
- tracecite_docs/tables/html.py +232 -0
- tracecite_docs/tables/models.py +211 -0
- tracecite_docs/tables/normalise.py +787 -0
- tracecite_docs/tables/pandoc.py +427 -0
- tracecite_docs/tables/publish.py +248 -0
- tracecite_docs/tables/render.py +128 -0
- tracecite_docs/tables/site.py +457 -0
- tracecite_docs-0.0.1.dist-info/METADATA +94 -0
- tracecite_docs-0.0.1.dist-info/RECORD +26 -0
- tracecite_docs-0.0.1.dist-info/WHEEL +5 -0
- tracecite_docs-0.0.1.dist-info/entry_points.txt +2 -0
- tracecite_docs-0.0.1.dist-info/licenses/LICENSE +21 -0
- tracecite_docs-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""TraceCite Docs: source-linked normalisation for executable knowledge documents."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import version
|
|
4
|
+
|
|
5
|
+
from .tables import (
|
|
6
|
+
NORMALISER_VERSION,
|
|
7
|
+
NormalisedTable,
|
|
8
|
+
TableContext,
|
|
9
|
+
TableDiagnostic,
|
|
10
|
+
TableNormalisationError,
|
|
11
|
+
augment_document_with_embedding_text,
|
|
12
|
+
export_embedding_site,
|
|
13
|
+
html_table_to_markdown,
|
|
14
|
+
knowledge_table,
|
|
15
|
+
normalise_document_tables,
|
|
16
|
+
normalise_html_table,
|
|
17
|
+
normalise_pandoc_table,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__version__ = version("tracecite-docs")
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"NORMALISER_VERSION",
|
|
24
|
+
"NormalisedTable",
|
|
25
|
+
"TableContext",
|
|
26
|
+
"TableDiagnostic",
|
|
27
|
+
"TableNormalisationError",
|
|
28
|
+
"augment_document_with_embedding_text",
|
|
29
|
+
"export_embedding_site",
|
|
30
|
+
"html_table_to_markdown",
|
|
31
|
+
"knowledge_table",
|
|
32
|
+
"normalise_document_tables",
|
|
33
|
+
"normalise_html_table",
|
|
34
|
+
"normalise_pandoc_table",
|
|
35
|
+
]
|
tracecite_docs/cli.py
ADDED
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
"""Command-line interface for table normalisation and inspection-site export."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import functools
|
|
7
|
+
import json
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from . import __version__
|
|
12
|
+
from .docs import (
|
|
13
|
+
author_docs,
|
|
14
|
+
build_docs,
|
|
15
|
+
check_docs,
|
|
16
|
+
doctor_docs_index,
|
|
17
|
+
load_docs_contract,
|
|
18
|
+
search_docs_index,
|
|
19
|
+
stage_docs,
|
|
20
|
+
sync_docs_index,
|
|
21
|
+
)
|
|
22
|
+
from .tables import (
|
|
23
|
+
TableContext,
|
|
24
|
+
TableNormalisationError,
|
|
25
|
+
augment_document_with_embedding_text,
|
|
26
|
+
export_embedding_site,
|
|
27
|
+
normalise_document_tables,
|
|
28
|
+
normalise_html_table,
|
|
29
|
+
normalise_pandoc_table,
|
|
30
|
+
render_debug_markdown,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
DEFAULT_LIMIT = 10
|
|
34
|
+
DEFAULT_FTS_LIMIT = 50
|
|
35
|
+
DEFAULT_VECTOR_LIMIT = 50
|
|
36
|
+
|
|
37
|
+
PANDOC_HELP = (
|
|
38
|
+
"Pandoc command or path (default: TRACECITE_PANDOC, then Quarto's bundled "
|
|
39
|
+
"Pandoc, then pandoc on PATH)."
|
|
40
|
+
)
|
|
41
|
+
PIPE_FALLBACK_HELP = (
|
|
42
|
+
"Parse plain pipe tables without Pandoc when Pandoc is unavailable or "
|
|
43
|
+
"rejects the input."
|
|
44
|
+
)
|
|
45
|
+
LIMIT_HELP = "Number of results to return (default: %(default)s)."
|
|
46
|
+
FTS_LIMIT_HELP = "Full-text candidates ranked before fusion (default: %(default)s)."
|
|
47
|
+
VECTOR_LIMIT_HELP = "Vector candidates ranked before fusion (default: %(default)s)."
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _add_pandoc_args(parser: argparse.ArgumentParser) -> None:
|
|
51
|
+
parser.add_argument("--pandoc", help=PANDOC_HELP)
|
|
52
|
+
parser.add_argument(
|
|
53
|
+
"--allow-pipe-fallback", action="store_true", help=PIPE_FALLBACK_HELP
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
58
|
+
parser = argparse.ArgumentParser(prog="tracecite-docs")
|
|
59
|
+
parser.add_argument(
|
|
60
|
+
"--version", action="version", version=f"TraceCite Docs {__version__}"
|
|
61
|
+
)
|
|
62
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
63
|
+
|
|
64
|
+
table = subparsers.add_parser("table", help="Normalise one table")
|
|
65
|
+
table_sub = table.add_subparsers(dest="table_command", required=True)
|
|
66
|
+
normalise = table_sub.add_parser(
|
|
67
|
+
"normalise", help="Normalise one Markdown or HTML table"
|
|
68
|
+
)
|
|
69
|
+
normalise.set_defaults(handler=_table_normalise)
|
|
70
|
+
normalise.add_argument("input", help="Input path or '-' for standard input")
|
|
71
|
+
normalise.add_argument(
|
|
72
|
+
"--from",
|
|
73
|
+
dest="source_format",
|
|
74
|
+
choices=["auto", "pandoc", "html"],
|
|
75
|
+
default="auto",
|
|
76
|
+
help="Input format; auto reads input containing <table as HTML and "
|
|
77
|
+
"anything else as Pandoc Markdown.",
|
|
78
|
+
)
|
|
79
|
+
normalise.add_argument(
|
|
80
|
+
"--to",
|
|
81
|
+
dest="target_format",
|
|
82
|
+
choices=["text", "json", "canonical-markdown", "debug-markdown"],
|
|
83
|
+
default="text",
|
|
84
|
+
help="Output: normalised row text, the full JSON record, canonical "
|
|
85
|
+
"Markdown, or Markdown with diagnostics.",
|
|
86
|
+
)
|
|
87
|
+
normalise.add_argument(
|
|
88
|
+
"--output", type=Path, help="Write to this file instead of standard output."
|
|
89
|
+
)
|
|
90
|
+
normalise.add_argument(
|
|
91
|
+
"--document-path",
|
|
92
|
+
help="Source document named in the normalised text (default: the input path).",
|
|
93
|
+
)
|
|
94
|
+
normalise.add_argument(
|
|
95
|
+
"--source-code-path",
|
|
96
|
+
help="Executable source file named in the normalised text.",
|
|
97
|
+
)
|
|
98
|
+
normalise.add_argument("--caption", help="Table caption for the normalised text.")
|
|
99
|
+
normalise.add_argument(
|
|
100
|
+
"--table-id", help="Table identifier to use instead of a generated one."
|
|
101
|
+
)
|
|
102
|
+
normalise.add_argument(
|
|
103
|
+
"--ordering", help="Description of the row order for the normalised text."
|
|
104
|
+
)
|
|
105
|
+
normalise.add_argument(
|
|
106
|
+
"--strict",
|
|
107
|
+
action="store_true",
|
|
108
|
+
help="Fail when the table has normalisation errors instead of reporting them.",
|
|
109
|
+
)
|
|
110
|
+
_add_pandoc_args(normalise)
|
|
111
|
+
|
|
112
|
+
document = subparsers.add_parser(
|
|
113
|
+
"document", help="Normalise every table in one document"
|
|
114
|
+
)
|
|
115
|
+
document_sub = document.add_subparsers(dest="document_command", required=True)
|
|
116
|
+
document_normalise = document_sub.add_parser(
|
|
117
|
+
"normalise", help="Normalise document tables"
|
|
118
|
+
)
|
|
119
|
+
document_normalise.set_defaults(handler=_document_normalise)
|
|
120
|
+
document_normalise.add_argument("input", type=Path)
|
|
121
|
+
document_normalise.add_argument(
|
|
122
|
+
"--to",
|
|
123
|
+
dest="target_format",
|
|
124
|
+
choices=["jsonl", "embedding-markdown", "summary"],
|
|
125
|
+
default="summary",
|
|
126
|
+
)
|
|
127
|
+
document_normalise.add_argument("--output", type=Path)
|
|
128
|
+
document_normalise.add_argument("--source-code-path")
|
|
129
|
+
document_normalise.add_argument("--strict", action="store_true")
|
|
130
|
+
_add_pandoc_args(document_normalise)
|
|
131
|
+
|
|
132
|
+
prepare = subparsers.add_parser(
|
|
133
|
+
"prepare",
|
|
134
|
+
help="Prepare table records and optionally keep a complete embedding-Markdown website",
|
|
135
|
+
)
|
|
136
|
+
prepare.set_defaults(handler=_prepare)
|
|
137
|
+
prepare.add_argument(
|
|
138
|
+
"source_root",
|
|
139
|
+
type=Path,
|
|
140
|
+
help="Rendered site directory containing Quarto's retained *.html.md files.",
|
|
141
|
+
)
|
|
142
|
+
prepare.add_argument(
|
|
143
|
+
"--project-config",
|
|
144
|
+
type=Path,
|
|
145
|
+
help="Quarto project configuration (_quarto.yml) whose theme and "
|
|
146
|
+
"navigation the copied site keeps.",
|
|
147
|
+
)
|
|
148
|
+
prepare.add_argument(
|
|
149
|
+
"--project-profile",
|
|
150
|
+
help="Merge _quarto-<profile>.yml into the copied inspection-site configuration",
|
|
151
|
+
)
|
|
152
|
+
prepare.add_argument(
|
|
153
|
+
"--source-project",
|
|
154
|
+
type=Path,
|
|
155
|
+
help="Documentation source project; its extensions and assets are copied "
|
|
156
|
+
"and each page's executable source is named in the table text.",
|
|
157
|
+
)
|
|
158
|
+
prepare.add_argument(
|
|
159
|
+
"--keep-embedding-markdown",
|
|
160
|
+
type=Path,
|
|
161
|
+
metavar="DIR",
|
|
162
|
+
required=True,
|
|
163
|
+
help="Copy the complete retained-Markdown website to DIR and append normalised table text",
|
|
164
|
+
)
|
|
165
|
+
prepare.add_argument(
|
|
166
|
+
"--render-embedding-site",
|
|
167
|
+
action="store_true",
|
|
168
|
+
help="Render the copied site with Quarto after writing it.",
|
|
169
|
+
)
|
|
170
|
+
prepare.add_argument(
|
|
171
|
+
"--debug-tables",
|
|
172
|
+
action="store_true",
|
|
173
|
+
help="Print each table's size, source format, and diagnostics.",
|
|
174
|
+
)
|
|
175
|
+
prepare.add_argument(
|
|
176
|
+
"--quarto",
|
|
177
|
+
help="Quarto command for --render-embedding-site (default: quarto on PATH).",
|
|
178
|
+
)
|
|
179
|
+
_add_pandoc_args(prepare)
|
|
180
|
+
prepare.add_argument(
|
|
181
|
+
"--strict-tables",
|
|
182
|
+
action="store_true",
|
|
183
|
+
help="Fail when any table has normalisation errors.",
|
|
184
|
+
)
|
|
185
|
+
prepare.add_argument(
|
|
186
|
+
"--no-clean",
|
|
187
|
+
action="store_true",
|
|
188
|
+
help="Write into DIR without first deleting a previous TraceCite output there.",
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
docs_build = subparsers.add_parser(
|
|
192
|
+
"build", help="Build a Quarto documentation project"
|
|
193
|
+
)
|
|
194
|
+
docs_build.set_defaults(handler=_docs_build)
|
|
195
|
+
docs_build.add_argument("project", type=Path)
|
|
196
|
+
docs_build.add_argument("--docs-config", type=Path, default=None)
|
|
197
|
+
docs_build.add_argument("--repo-root", type=Path, default=Path.cwd())
|
|
198
|
+
docs_build.add_argument("--only", choices=["python", "julia"])
|
|
199
|
+
docs_build.add_argument("--quarto")
|
|
200
|
+
docs_build.add_argument("--no-embedding-site", action="store_true")
|
|
201
|
+
docs_build.add_argument("--no-render-embedding-site", action="store_true")
|
|
202
|
+
docs_build.add_argument("--strict-tables", action="store_true")
|
|
203
|
+
docs_build.add_argument("--check-retained", action="store_true")
|
|
204
|
+
docs_stage = subparsers.add_parser(
|
|
205
|
+
"stage", help="Stage local or public source links"
|
|
206
|
+
)
|
|
207
|
+
docs_stage.set_defaults(handler=_docs_stage)
|
|
208
|
+
docs_stage.add_argument("--docs-config", type=Path, required=True)
|
|
209
|
+
docs_stage.add_argument("--repo-root", type=Path, default=Path.cwd())
|
|
210
|
+
docs_stage.add_argument("--target", choices=["local", "public"], required=True)
|
|
211
|
+
for name, operation in (("author", author_docs), ("check", check_docs)):
|
|
212
|
+
mode = subparsers.add_parser(name, help=f"{name.title()} documentation outputs")
|
|
213
|
+
mode.set_defaults(handler=functools.partial(_docs_mode, operation=operation))
|
|
214
|
+
mode.add_argument("--docs-config", type=Path, required=True)
|
|
215
|
+
mode.add_argument("--repo-root", type=Path, default=Path.cwd())
|
|
216
|
+
for name, help_text, handler in (
|
|
217
|
+
(
|
|
218
|
+
"index",
|
|
219
|
+
"Build the index-input mirror and synchronise the documentation index",
|
|
220
|
+
_docs_index,
|
|
221
|
+
),
|
|
222
|
+
(
|
|
223
|
+
"search",
|
|
224
|
+
"Hybrid FTS + vector search over the documentation index",
|
|
225
|
+
_docs_search,
|
|
226
|
+
),
|
|
227
|
+
(
|
|
228
|
+
"doctor",
|
|
229
|
+
"Integrity checks for the documentation index profile",
|
|
230
|
+
_docs_doctor,
|
|
231
|
+
),
|
|
232
|
+
):
|
|
233
|
+
command = subparsers.add_parser(name, help=help_text)
|
|
234
|
+
command.set_defaults(handler=handler)
|
|
235
|
+
command.add_argument("--docs-config", type=Path, required=True)
|
|
236
|
+
command.add_argument("--repo-root", type=Path, default=Path.cwd())
|
|
237
|
+
docs_search = subparsers.choices["search"]
|
|
238
|
+
docs_search.add_argument("query")
|
|
239
|
+
docs_search.add_argument(
|
|
240
|
+
"--limit", type=int, default=DEFAULT_LIMIT, help=LIMIT_HELP
|
|
241
|
+
)
|
|
242
|
+
docs_search.add_argument(
|
|
243
|
+
"--fts-limit", type=int, default=DEFAULT_FTS_LIMIT, help=FTS_LIMIT_HELP
|
|
244
|
+
)
|
|
245
|
+
docs_search.add_argument(
|
|
246
|
+
"--vector-limit", type=int, default=DEFAULT_VECTOR_LIMIT, help=VECTOR_LIMIT_HELP
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
check = table_sub.add_parser(
|
|
250
|
+
"check", help="Strictly validate generated Markdown tables"
|
|
251
|
+
)
|
|
252
|
+
check.set_defaults(handler=_check)
|
|
253
|
+
check.add_argument(
|
|
254
|
+
"path",
|
|
255
|
+
type=Path,
|
|
256
|
+
help="Markdown file, or directory whose *.md files are checked recursively.",
|
|
257
|
+
)
|
|
258
|
+
_add_pandoc_args(check)
|
|
259
|
+
check.add_argument(
|
|
260
|
+
"--debug-tables",
|
|
261
|
+
action="store_true",
|
|
262
|
+
help="Print each table's size, source format, and diagnostics.",
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
return parser
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def main(argv: list[str] | None = None) -> int:
|
|
269
|
+
args = build_parser().parse_args(argv)
|
|
270
|
+
try:
|
|
271
|
+
return args.handler(args)
|
|
272
|
+
except ModuleNotFoundError as error:
|
|
273
|
+
if _is_missing_evidence_dependency(error):
|
|
274
|
+
print(
|
|
275
|
+
"tracecite-docs: evidence commands require the optional evidence dependencies; "
|
|
276
|
+
"install with tracecite-docs[evidence]",
|
|
277
|
+
file=sys.stderr,
|
|
278
|
+
)
|
|
279
|
+
return 2
|
|
280
|
+
raise
|
|
281
|
+
except (
|
|
282
|
+
OSError,
|
|
283
|
+
RuntimeError,
|
|
284
|
+
TableNormalisationError,
|
|
285
|
+
ValueError,
|
|
286
|
+
) as error:
|
|
287
|
+
print(f"tracecite-docs: {error}", file=sys.stderr)
|
|
288
|
+
return 2
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _is_missing_evidence_dependency(error: ModuleNotFoundError) -> bool:
|
|
292
|
+
if "tracecite[evidence]" in str(error):
|
|
293
|
+
return True
|
|
294
|
+
return (error.name or "").split(".", 1)[0] in {
|
|
295
|
+
"pymupdf",
|
|
296
|
+
"huggingface_hub",
|
|
297
|
+
"sentence_transformers",
|
|
298
|
+
"sqlite_vec",
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _docs_build(args: argparse.Namespace) -> int:
|
|
303
|
+
if args.docs_config is not None:
|
|
304
|
+
load_docs_contract(args.docs_config, repo_root=args.repo_root)
|
|
305
|
+
result = build_docs(
|
|
306
|
+
args.project,
|
|
307
|
+
only=args.only,
|
|
308
|
+
quarto=args.quarto,
|
|
309
|
+
inspection=not args.no_embedding_site,
|
|
310
|
+
render_inspection=not args.no_render_embedding_site,
|
|
311
|
+
strict_tables=args.strict_tables,
|
|
312
|
+
check_retained=args.check_retained,
|
|
313
|
+
)
|
|
314
|
+
print(f"Documentation build: {result.selection.variant}")
|
|
315
|
+
print(f"Documentation site: {result.output_root}")
|
|
316
|
+
print(f"Retained Markdown pages: {result.retained_count}")
|
|
317
|
+
if result.inspection:
|
|
318
|
+
print(f"Embedding Markdown site: {result.inspection.output_root}")
|
|
319
|
+
print(f"Pages copied: {result.inspection.page_count}")
|
|
320
|
+
print(f"Tables normalised: {result.inspection.table_count}")
|
|
321
|
+
if result.inspection.rendered_site:
|
|
322
|
+
print(f"Rendered inspection site: {result.inspection.rendered_site}")
|
|
323
|
+
if args.check_retained:
|
|
324
|
+
if result.changed_retained:
|
|
325
|
+
print("Retained Markdown changed:")
|
|
326
|
+
print("\n".join(f"- {path}" for path in result.changed_retained))
|
|
327
|
+
return 1
|
|
328
|
+
print("Retained Markdown is fresh.")
|
|
329
|
+
return 0
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def _docs_stage(args: argparse.Namespace) -> int:
|
|
333
|
+
contract = load_docs_contract(args.docs_config, repo_root=args.repo_root)
|
|
334
|
+
result = stage_docs(contract, target=args.target, repo_root=args.repo_root)
|
|
335
|
+
print(f"Documentation staging target: {result.target}")
|
|
336
|
+
print(f"Documentation staging root: {result.staged_root}")
|
|
337
|
+
print(f"Changed Markdown files: {len(result.changed_files)}")
|
|
338
|
+
return 0
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def _docs_mode(args: argparse.Namespace, operation) -> int:
|
|
342
|
+
contract = load_docs_contract(args.docs_config, repo_root=args.repo_root)
|
|
343
|
+
result = operation(contract, config_path=args.docs_config, repo_root=args.repo_root)
|
|
344
|
+
print(f"Documentation mode: {result.mode}")
|
|
345
|
+
print(f"Manifest: {result.manifest_path}")
|
|
346
|
+
if result.diagnostics:
|
|
347
|
+
print("\n".join(f"- {item}" for item in result.diagnostics), file=sys.stderr)
|
|
348
|
+
return 0 if result.ok else 1
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _docs_index(args: argparse.Namespace) -> int:
|
|
352
|
+
contract = load_docs_contract(args.docs_config, repo_root=args.repo_root)
|
|
353
|
+
result = sync_docs_index(contract, repo_root=args.repo_root)
|
|
354
|
+
report = result.sync_report
|
|
355
|
+
print(f"Documentation index input: {result.profile.input_root}")
|
|
356
|
+
print(f"Documentation index database: {result.profile.database_path}")
|
|
357
|
+
print(f"Tables normalised: {result.tables_normalized}")
|
|
358
|
+
from tracecite.evidence.commands import cli_sync_status
|
|
359
|
+
|
|
360
|
+
status = cli_sync_status(report)
|
|
361
|
+
print(f"status: {status}")
|
|
362
|
+
print(f"added: {report.sources_added}")
|
|
363
|
+
for path, reason in report.skipped_sources.items():
|
|
364
|
+
print(f"skipped: {path}: {reason}")
|
|
365
|
+
print(f"unchanged: {len(report.sources_unchanged)} source(s)")
|
|
366
|
+
print(f"embeddings generated: {report.embeddings_generated}")
|
|
367
|
+
return 0 if status == "ok" else 1
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def _docs_search(args: argparse.Namespace) -> int:
|
|
371
|
+
contract = load_docs_contract(args.docs_config, repo_root=args.repo_root)
|
|
372
|
+
results = search_docs_index(
|
|
373
|
+
contract,
|
|
374
|
+
args.query,
|
|
375
|
+
limit=args.limit,
|
|
376
|
+
fts_limit=args.fts_limit,
|
|
377
|
+
vector_limit=args.vector_limit,
|
|
378
|
+
)
|
|
379
|
+
print(json.dumps(results, indent=2, ensure_ascii=False))
|
|
380
|
+
return 0
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def _docs_doctor(args: argparse.Namespace) -> int:
|
|
384
|
+
contract = load_docs_contract(args.docs_config, repo_root=args.repo_root)
|
|
385
|
+
issues = doctor_docs_index(contract)
|
|
386
|
+
for issue in issues:
|
|
387
|
+
print(f"ISSUE: {issue}")
|
|
388
|
+
if not issues:
|
|
389
|
+
print("doctor: no issues found")
|
|
390
|
+
return 0 if not issues else 1
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def _table_normalise(args: argparse.Namespace) -> int:
|
|
394
|
+
source = (
|
|
395
|
+
sys.stdin.read()
|
|
396
|
+
if args.input == "-"
|
|
397
|
+
else Path(args.input).read_text(encoding="utf-8")
|
|
398
|
+
)
|
|
399
|
+
context = TableContext(
|
|
400
|
+
document_path=args.document_path
|
|
401
|
+
or ("<stdin>" if args.input == "-" else args.input),
|
|
402
|
+
source_code_path=args.source_code_path,
|
|
403
|
+
caption=args.caption,
|
|
404
|
+
table_id=args.table_id,
|
|
405
|
+
ordering=args.ordering,
|
|
406
|
+
)
|
|
407
|
+
source_format = args.source_format
|
|
408
|
+
if source_format == "auto":
|
|
409
|
+
source_format = "html" if "<table" in source.lower() else "pandoc"
|
|
410
|
+
|
|
411
|
+
if source_format == "html":
|
|
412
|
+
table = normalise_html_table(
|
|
413
|
+
source,
|
|
414
|
+
context=context,
|
|
415
|
+
strict=args.strict,
|
|
416
|
+
pandoc=args.pandoc,
|
|
417
|
+
)
|
|
418
|
+
else:
|
|
419
|
+
table = normalise_pandoc_table(
|
|
420
|
+
source,
|
|
421
|
+
context=context,
|
|
422
|
+
strict=args.strict,
|
|
423
|
+
pandoc=args.pandoc,
|
|
424
|
+
allow_pipe_fallback=args.allow_pipe_fallback,
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
output = {
|
|
428
|
+
"text": table.normalised_text,
|
|
429
|
+
"json": table.to_json(),
|
|
430
|
+
"canonical-markdown": table.canonical_markdown,
|
|
431
|
+
"debug-markdown": render_debug_markdown(table),
|
|
432
|
+
}[args.target_format]
|
|
433
|
+
_write_output(output.rstrip() + "\n", args.output)
|
|
434
|
+
return 0
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
def _document_normalise(args: argparse.Namespace) -> int:
|
|
438
|
+
source = args.input.read_text(encoding="utf-8")
|
|
439
|
+
if args.target_format == "embedding-markdown":
|
|
440
|
+
result = augment_document_with_embedding_text(
|
|
441
|
+
source,
|
|
442
|
+
document_path=args.input.as_posix(),
|
|
443
|
+
source_code_path=args.source_code_path,
|
|
444
|
+
strict=args.strict,
|
|
445
|
+
pandoc=args.pandoc,
|
|
446
|
+
allow_pipe_fallback=args.allow_pipe_fallback,
|
|
447
|
+
)
|
|
448
|
+
_write_output(result.markdown, args.output)
|
|
449
|
+
return 0
|
|
450
|
+
|
|
451
|
+
tables = normalise_document_tables(
|
|
452
|
+
source,
|
|
453
|
+
document_path=args.input.as_posix(),
|
|
454
|
+
source_code_path=args.source_code_path,
|
|
455
|
+
strict=args.strict,
|
|
456
|
+
pandoc=args.pandoc,
|
|
457
|
+
allow_pipe_fallback=args.allow_pipe_fallback,
|
|
458
|
+
)
|
|
459
|
+
if args.target_format == "jsonl":
|
|
460
|
+
output = "".join(
|
|
461
|
+
json.dumps(table.to_dict(), ensure_ascii=False, sort_keys=True) + "\n"
|
|
462
|
+
for table in tables
|
|
463
|
+
)
|
|
464
|
+
else:
|
|
465
|
+
lines = [f"Tables: {len(tables)}"]
|
|
466
|
+
for table in tables:
|
|
467
|
+
warning_count = sum(
|
|
468
|
+
item.severity == "warning" for item in table.diagnostics
|
|
469
|
+
)
|
|
470
|
+
error_count = sum(item.severity == "error" for item in table.diagnostics)
|
|
471
|
+
lines.append(
|
|
472
|
+
f"- {table.table_id}: {len(table.rows)} row(s), {len(table.headers)} column(s), "
|
|
473
|
+
f"{warning_count} warning(s), {error_count} error(s)"
|
|
474
|
+
)
|
|
475
|
+
output = "\n".join(lines) + "\n"
|
|
476
|
+
_write_output(output, args.output)
|
|
477
|
+
return 0
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
def _prepare(args: argparse.Namespace) -> int:
|
|
481
|
+
result = export_embedding_site(
|
|
482
|
+
args.source_root,
|
|
483
|
+
args.keep_embedding_markdown,
|
|
484
|
+
project_config=args.project_config,
|
|
485
|
+
project_profile=args.project_profile,
|
|
486
|
+
source_project=args.source_project,
|
|
487
|
+
strict=args.strict_tables,
|
|
488
|
+
pandoc=args.pandoc,
|
|
489
|
+
allow_pipe_fallback=args.allow_pipe_fallback,
|
|
490
|
+
render=args.render_embedding_site,
|
|
491
|
+
quarto=args.quarto,
|
|
492
|
+
clean=not args.no_clean,
|
|
493
|
+
)
|
|
494
|
+
print(f"Embedding Markdown site: {result.output_root}")
|
|
495
|
+
print(f"Pages copied: {result.page_count}")
|
|
496
|
+
print(f"Tables normalised: {result.table_count}")
|
|
497
|
+
if result.rendered_site:
|
|
498
|
+
print(f"Rendered inspection site: {result.rendered_site}")
|
|
499
|
+
if args.debug_tables:
|
|
500
|
+
_print_table_debug(result.output_root / "_tracecite" / "tables.jsonl")
|
|
501
|
+
return 0
|
|
502
|
+
|
|
503
|
+
|
|
504
|
+
def _check(args: argparse.Namespace) -> int:
|
|
505
|
+
paths = [args.path] if args.path.is_file() else _markdown_paths(args.path)
|
|
506
|
+
check_root = args.path.parent if args.path.is_file() else args.path
|
|
507
|
+
failures = 0
|
|
508
|
+
tables = 0
|
|
509
|
+
for path in paths:
|
|
510
|
+
source = path.read_text(encoding="utf-8")
|
|
511
|
+
try:
|
|
512
|
+
document_path = path.relative_to(check_root).as_posix()
|
|
513
|
+
except ValueError:
|
|
514
|
+
document_path = path.as_posix()
|
|
515
|
+
try:
|
|
516
|
+
found = normalise_document_tables(
|
|
517
|
+
source,
|
|
518
|
+
document_path=document_path,
|
|
519
|
+
strict=True,
|
|
520
|
+
pandoc=args.pandoc,
|
|
521
|
+
allow_pipe_fallback=args.allow_pipe_fallback,
|
|
522
|
+
)
|
|
523
|
+
except TableNormalisationError as error:
|
|
524
|
+
failures += 1
|
|
525
|
+
print(f"FAIL {path}: {error}")
|
|
526
|
+
continue
|
|
527
|
+
tables += len(found)
|
|
528
|
+
print(f"OK {path}: {len(found)} table(s)")
|
|
529
|
+
if args.debug_tables:
|
|
530
|
+
for table in found:
|
|
531
|
+
print(
|
|
532
|
+
f" {table.table_id}: {len(table.rows)} row(s), "
|
|
533
|
+
f"{len(table.headers)} column(s), source={table.source_format}"
|
|
534
|
+
)
|
|
535
|
+
for diagnostic in table.diagnostics:
|
|
536
|
+
print(
|
|
537
|
+
f" {diagnostic.severity}:{diagnostic.code}: "
|
|
538
|
+
f"{diagnostic.message}"
|
|
539
|
+
)
|
|
540
|
+
print(f"Checked {len(paths)} file(s), {tables} table(s), {failures} failure(s)")
|
|
541
|
+
return 1 if failures else 0
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
def _write_output(text: str, path: Path | None) -> None:
|
|
545
|
+
if path is None:
|
|
546
|
+
sys.stdout.write(text)
|
|
547
|
+
return
|
|
548
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
549
|
+
path.write_text(text, encoding="utf-8")
|
|
550
|
+
|
|
551
|
+
|
|
552
|
+
def _markdown_paths(root: Path) -> list[Path]:
|
|
553
|
+
excluded = {".tracecite", "_tracecite", ".quarto", "site_libs", "_site"}
|
|
554
|
+
return [
|
|
555
|
+
path
|
|
556
|
+
for path in sorted(root.rglob("*.md"))
|
|
557
|
+
if not any(part in excluded for part in path.relative_to(root).parts)
|
|
558
|
+
]
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
def _print_table_debug(path: Path) -> None:
|
|
562
|
+
if not path.is_file():
|
|
563
|
+
return
|
|
564
|
+
for line in path.read_text(encoding="utf-8").splitlines():
|
|
565
|
+
if not line.strip():
|
|
566
|
+
continue
|
|
567
|
+
table = json.loads(line)
|
|
568
|
+
print(
|
|
569
|
+
f"DEBUG {table['table_id']}: {len(table['rows'])} row(s), "
|
|
570
|
+
f"{len(table['headers'])} column(s), source={table['source_format']}"
|
|
571
|
+
)
|
|
572
|
+
for diagnostic in table.get("diagnostics", []):
|
|
573
|
+
print(
|
|
574
|
+
f" {diagnostic['severity']}:{diagnostic['code']}: "
|
|
575
|
+
f"{diagnostic['message']}"
|
|
576
|
+
)
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
if __name__ == "__main__":
|
|
580
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Reusable documentation build and evidence-contract APIs."""
|
|
2
|
+
|
|
3
|
+
from .build import (
|
|
4
|
+
BuildSelection,
|
|
5
|
+
BuildVariant,
|
|
6
|
+
DocsBuildResult,
|
|
7
|
+
Language,
|
|
8
|
+
build_docs,
|
|
9
|
+
changed_retained_markdown,
|
|
10
|
+
classify_render_inputs,
|
|
11
|
+
discover_render_inputs,
|
|
12
|
+
select_build_variant,
|
|
13
|
+
snapshot_retained_markdown,
|
|
14
|
+
stage_retained_markdown,
|
|
15
|
+
)
|
|
16
|
+
from .config import load_docs_contract
|
|
17
|
+
from .contract import DocsEvidenceContract
|
|
18
|
+
from .modes import DocsModeResult, author_docs, check_docs
|
|
19
|
+
from .stage import DocsStageResult, stage_docs
|
|
20
|
+
from .vectorize import (
|
|
21
|
+
DocsIndexProfile,
|
|
22
|
+
DocsIndexResult,
|
|
23
|
+
doctor_docs_index,
|
|
24
|
+
prepare_docs_index_input,
|
|
25
|
+
resolve_docs_index_profile,
|
|
26
|
+
search_docs_index,
|
|
27
|
+
sync_docs_index,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"BuildSelection",
|
|
32
|
+
"BuildVariant",
|
|
33
|
+
"DocsBuildResult",
|
|
34
|
+
"DocsEvidenceContract",
|
|
35
|
+
"DocsIndexProfile",
|
|
36
|
+
"DocsIndexResult",
|
|
37
|
+
"DocsModeResult",
|
|
38
|
+
"DocsStageResult",
|
|
39
|
+
"Language",
|
|
40
|
+
"author_docs",
|
|
41
|
+
"build_docs",
|
|
42
|
+
"changed_retained_markdown",
|
|
43
|
+
"check_docs",
|
|
44
|
+
"classify_render_inputs",
|
|
45
|
+
"discover_render_inputs",
|
|
46
|
+
"doctor_docs_index",
|
|
47
|
+
"load_docs_contract",
|
|
48
|
+
"prepare_docs_index_input",
|
|
49
|
+
"resolve_docs_index_profile",
|
|
50
|
+
"search_docs_index",
|
|
51
|
+
"select_build_variant",
|
|
52
|
+
"snapshot_retained_markdown",
|
|
53
|
+
"stage_docs",
|
|
54
|
+
"stage_retained_markdown",
|
|
55
|
+
"sync_docs_index",
|
|
56
|
+
]
|