chunking¶
Split a large document into smaller pages or JSON data files for static site generators and web components.
opm.chunking ¶
Document chunking
Splits large TEI documents into smaller HTML pages with metadata and fragments for static site generation.
ChunkProcessor ¶
ChunkProcessor(
module_path: Path,
xml_root: _Element,
config: ChunkingConfig,
project_root: Path,
project_config: ProjectConfig | None = None,
webcomponents: bool = False,
xpath_env: XPathEnvironment | None = None,
source_dir: Path | None = None,
documents: Collection[str] | None = None,
document: str | None = None,
)
Splits one document into chunks and writes them in one output format.
Most callers want opm.project.Project.chunk or
chunk_document, which build one of these. Use it directly to
select chunks (select_chunks) or read their metadata without
writing anything.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module_path
|
Path
|
The compiled transform module for chunk content. |
required |
xml_root
|
_Element
|
Root element of the parsed document. |
required |
config
|
ChunkingConfig
|
The |
required |
project_root
|
Path
|
The directory |
required |
project_config
|
ProjectConfig | None
|
The project settings (parameters, template context). |
None
|
webcomponents
|
bool
|
Enable web-component mode. |
False
|
xpath_env
|
XPathEnvironment | None
|
The XPath environment to evaluate in; see
|
None
|
source_dir
|
Path | None
|
Directory of the source file, for copying images. |
None
|
documents
|
Collection[str] | None
|
Names of every document in the run, for the templates. |
None
|
document
|
str | None
|
Name of this document's source file. |
None
|
Source code in src/opm/chunking.py
odd_name
property
¶
ODD name to advertise to pb-view.
Read from the ODD_NAME baked into the generated module, i.e. the ODD
it was actually compiled from, so the index and CSS reference the right
ODD. pb-view sends odd=<name>.odd and loads css/<name>.css.
select_chunks ¶
Find chunk elements.
If config.selector is set to a dotted Python path it is imported
and called as selector(root, config). Otherwise the xpath
expression from the chunking config is evaluated.
Selectors that rebuild a region as a detached tree record copy → source
in opm.runtime.source_map while they build, which is what lets
$get() in an ODD step back to the stored document. The map holds
both trees alive, so it is reset here — once per document, before the
selector runs.
Source code in src/opm/chunking.py
shared_root ¶
Return the directory holding output shared across documents.
Each document gets its own subdirectory, so stylesheets and assets
belong one level up, beside the collection index — one copy for the
whole edition. Without link_doc the output directory is itself the
root.
Source code in src/opm/chunking.py
url_prefix ¶
Return the relative path from a chunk page back to shared_root.
write_shared_files ¶
Write stylesheets and copy assets into shared_root.
Chunking always produces several pages sharing one stylesheet, so the
stylesheets are always written as files — the same thing
--format pb-view has always done. Templates receive
odd_css_url pointing at it, alongside the odd_css string, which
stays available so a template that inlines it keeps working. Stylesheets
among config.assets are listed in asset_styles, in declared
order. index_url points back at the collection index written at
shared_root (see build_index) — empty when the caller sets
no link_doc, as there is then no such page to link to.
Safe to call once per document — the writes are idempotent.
Source code in src/opm/chunking.py
copy_referenced_images ¶
Copy the local images html references into output_dir.
An img/@src is written relative to the source document, and the
chunk pages sit flat in the output directory, so each image goes to the
same relative path there. Files are looked up the way EPUB output does:
next to the source document, then in a sibling images/ directory.
Remote and root-relative URLs are left alone, as are paths that would
land outside the output directory and images that cannot be found.
Source code in src/opm/chunking.py
entry_file ¶
entry_href ¶
Return the link to this document's entry point, relative to the output root.
config.link_doc names the per-document subdirectory, giving
quickstart.xml/001.html; without it there is no prefix.
Source code in src/opm/chunking.py
generate_chunk_metadata ¶
Generate metadata for a chunk.
Source code in src/opm/chunking.py
build_anchor_index ¶
Map source xml:id (or plain @id) values to the chunk file that owns them.
JATS and other no-namespace vocabularies identify elements with @id,
so both are indexed; xml:id wins where an element carries both.
Source code in src/opm/chunking.py
process_fragment ¶
process_fragment(
fragment: FragmentConfig,
context_node: _Element | None = None,
_cache: dict[tuple, str] | None = None,
chunk_index: int | None = None,
) -> str
Process a fragment transform.
The xpath is evaluated with the chunk as context node, so a
chunk-relative expression (../../text[@type='translation']/div)
picks that chunk's counterpart. An absolute expression that yields one
element per chunk (//body/div[@xml:lang='en']) is instead aligned by
position — the parallel-column case — using chunk_index. Both formats
pass the index, so --format pb-view and the HTML output resolve
fragments identically.
Source code in src/opm/chunking.py
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | |
process_chunk ¶
Process a single chunk with its fragments.
Source code in src/opm/chunking.py
process_global_fragments ¶
Process global fragments once.
Source code in src/opm/chunking.py
render_chunk_template ¶
render_chunk_template(
chunk_result: ChunkResult,
global_fragments: dict[str, str],
template_path: Path | None = None,
) -> str
Render a chunk with its template context.
Source code in src/opm/chunking.py
generate_manifest ¶
Generate JSON manifest for static site builders.
Source code in src/opm/chunking.py
process_all ¶
process_all(
template_path: Path | None = None,
on_progress: Callable[[int, int], None] | None = None,
output_format: str = "html",
) -> None
Process all chunks and save files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_path
|
Path | None
|
Optional Jinja2 template path (ignored for |
None
|
on_progress
|
Callable[[int, int], None] | None
|
Optional callback |
None
|
output_format
|
str
|
|
'html'
|
Source code in src/opm/chunking.py
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 | |
export_pb_view ¶
export_pb_view(
doc_path: str | None = None,
on_progress: Callable[[int, int], None] | None = None,
) -> None
Export chunks as data consumable by the pb-view web component.
Lays the output out the way pb-view resolves it in static mode, where
the data location is derived from the static root and the document
path (${static}/${path}/...):
<output_dir>/<doc_path>/index.json— lookup table mapping pb-view's computed parameter keys to part files<output_dir>/<doc_path>/<xml:id>.json— one part per chunk, mirroring the response of TEI Publisher's/api/parts/<doc>/jsonendpoint<output_dir>/<doc_path>/<name>.json— one part per global fragment (e.g.toc.json), keyed by the fragment xpath anduser.*params; a sibling<name>.htmlcarries the same content as well-formed XML, for consumers that store it in an XML database (see_wellformed_fragment_xml)<output_dir>/<doc_path>/<name>-<xml:id>.json— per-chunk fragments<output_dir>/css/<odd>.css— stylesheet, shared by every document under the same static root
doc_path should match the path of the consuming pb-document.
When omitted the data is written directly into <output_dir> (single
document at the static root).
Chunks carrying an xml:id are addressed by it; chunks without one get
a stable synthetic id. pb-view navigates via nextId/previousId
(xml:ids) when present; next/previous are kept truthy for
navigate() but root is left null so subscribed dynamic views do
not call the parts API with root=<xml:id>.
on_progress is an optional callback (current, total) invoked after
each chunk is written.
Source code in src/opm/chunking.py
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 | |
IndexEntry
dataclass
¶
chunk_document ¶
chunk_document(
module_path: Path | None,
xml_path: Path,
config: ChunkingConfig,
project_root: Path,
template_path: Path | None = None,
on_progress: Callable[[int, int], None] | None = None,
project_config: ProjectConfig | None = None,
webcomponents: bool = False,
xpath_extensions: tuple[str, ...] | None = None,
output_format: str = "html",
doc_path: str | None = None,
documents: Collection[str] | None = None,
) -> None
Chunk a document using the specified configuration.
The page template gets xml_path's name as document. documents
names every document of the run (serafin01.xml, …) and reaches the
template as documents; pass the same set for each document of a
directory run. It defaults to just xml_path.
ODDs in config (the main one and the fragments') that have no compiled
module yet are compiled here. opm.project.Project.chunk handles
a whole directory the way opm chunk does.
Source code in src/opm/chunking.py
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 | |
resolve_assets ¶
Expand [chunking] assets entries to the paths to copy.
An entry holding *, ? or [ is matched against the filesystem, so
iiif/* copies every document's directory in one line instead of naming
each one — and keeps working when a document is added. Matches are sorted,
which fixes the cascade order of any stylesheets among them. Every other
entry is taken literally.
A literal path that does not exist, or a pattern matching nothing, raises
FileNotFoundError. The alternative is output quietly missing a file a
template or model expects, which surfaces much later as a 404.
Source code in src/opm/chunking.py
collect_index_entries ¶
collect_index_entries(output_dir: Path) -> list[IndexEntry]
Collect one IndexEntry per chunked document under output_dir.
Reads the manifest.json each document run writes, so this works on any
existing output directory without re-chunking. Directories without a
readable manifest are skipped.
Source code in src/opm/chunking.py
build_index ¶
build_index(
output_dir: Path,
*,
template_path: Path | None = None,
title: str | None = None,
odd_css: str | None = None,
module_path: Path | None = None,
project_config: ProjectConfig | None = None,
project_root: Path | None = None,
chunking_config: ChunkingConfig | None = None,
webcomponents: bool = False,
) -> Path | None
Render <output_dir>/index.html listing every chunked document.
http.server serves index.html in preference to a directory listing,
so writing this file is all that is needed for opm serve to show a real
landing page.
project_config also supplies the template context, so the index and
the chunk pages read the same [context] values. Pass webcomponents to
match the run's effective mode, so the index page is given
webcomponents_url exactly when the chunk pages are.
Pass module_path (and optionally project_config) to have the ODD's
generated CSS and the project stylesheet resolved the same way chunk pages
resolve them, so an index template can style a browse record's tei-*
classes exactly as the document pages do. An explicit odd_css wins.
Returns the path written, or None when output_dir holds no chunked documents.
Source code in src/opm/chunking.py
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 | |
build_index_json ¶
Write <output_dir>/index.json listing every chunked document.
The JSON counterpart of build_index. A directory run splits its
documents into one subdirectory each, and nothing at the root says what they
are or what order they belong in — a static site generator would have to
rediscover that by scanning. This writes it once, from the same
collect_index_entries the HTML index is built from, so both agree.
Returns the path written, or None when output_dir holds no chunked documents.