document_processor
zocrates.rag.document_processor ¶
Document processor: converts PDF/HTML attachments to Markdown and caches them.
Supports pluggable converter strategies via BaseDocumentConverter.
Default converters include:
- DirectReadConverter: Fast UTF-8 reader for .md and .txt files.
- MarkItDownConverter: Uses MarkItDown for document conversion with page-by-page
PDF image extraction via system utility pdfimages.
The cache directory lives in
Each item gets an item-centric bundle directory:
corpus/
CONVERTER_REGISTRY
module-attribute
¶
CONVERTER_REGISTRY = {'direct_read': DirectReadConverter, 'markitdown': MarkItDownConverter, 'docling_local': DoclingLocalConverter, 'datalab': DatalabConverter}
_META_SCHEMA
module-attribute
¶
_META_SCHEMA = '\nCREATE TABLE IF NOT EXISTS conversion_meta (\n item_key TEXT PRIMARY KEY,\n title TEXT,\n converter_id TEXT NOT NULL,\n converter_version TEXT NOT NULL,\n source_path TEXT NOT NULL,\n content_type TEXT NOT NULL,\n parent_item_type TEXT,\n converted_at TEXT NOT NULL,\n indexed_at TEXT,\n source_hash TEXT NOT NULL, -- SHA-256 of the source file\n char_count INTEGER NOT NULL,\n parent_item_key TEXT,\n parse_quality_score REAL,\n checkpoint_id TEXT,\n abstract TEXT,\n abstract_source TEXT,\n keywords TEXT,\n updated_at TEXT\n);\n\nCREATE TABLE IF NOT EXISTS conversion_failures (\n item_key TEXT NOT NULL,\n converter_id TEXT NOT NULL,\n error_message TEXT NOT NULL,\n source_hash TEXT NOT NULL,\n failed_at TEXT NOT NULL,\n PRIMARY KEY (item_key, converter_id)\n);\n\nCREATE TABLE IF NOT EXISTS item_relations (\n source_key TEXT NOT NULL,\n target_key TEXT NOT NULL,\n score REAL NOT NULL,\n relation_type TEXT NOT NULL,\n match_evidence TEXT,\n updated_at TEXT NOT NULL,\n PRIMARY KEY (source_key, target_key, relation_type)\n);\nCREATE INDEX IF NOT EXISTS idx_relations_source_score ON item_relations (source_key, score DESC);\nCREATE INDEX IF NOT EXISTS idx_relations_target ON item_relations (target_key);\n'
BaseDocumentConverter ¶
Bases: ABC
Abstract base class for document conversion engines.
converter_id
abstractmethod
property
¶
Unique identifier for the converter strategy (e.g., 'markitdown', 'direct_read').
converter_version
property
¶
Version string for cache invalidation. Override in subclasses when needed.
is_remote
property
¶
Indicate whether this converter relies on an external/remote network service.
convert
abstractmethod
¶
Convert source document to Markdown text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key used for identification. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
content_type
|
str
|
MIME type of the source file. |
required |
cache_dir
|
Path
|
Item-level bundle directory ( |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Converted Markdown text, or None if conversion failed or produced empty content. |
Source code in zocrates/rag/document_converters.py
supports
abstractmethod
¶
Return True if this converter can process the given document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str
|
MIME type of the document (e.g., 'application/pdf'). |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
Source code in zocrates/rag/document_converters.py
DatalabConverter ¶
Bases: BaseDocumentConverter
Remote document conversion engine via Datalab OCR API.
Initialize the remote Datalab converter instance.
Source code in zocrates/rag/document_converters.py
convert ¶
Convert document via Datalab API with adaptive retry and asset unpacking.
Includes an extension safeguard for valid PDFs lacking a .pdf extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
source_path
|
Path
|
Source file path on disk. |
required |
content_type
|
str
|
MIME type string. |
required |
cache_dir
|
Path
|
Bundle directory (or temporary staging directory). |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Converted Markdown string with |
Source code in zocrates/rag/document_converters.py
get_metadata ¶
Return the latest conversion metadata for an item key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing parse_quality_score, checkpoint_id, etc. |
Source code in zocrates/rag/document_converters.py
supports ¶
Check if document format is a PDF supported by Datalab.
Strictly limits conversion to PDF documents only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str
|
MIME type string. |
required |
source_path
|
Path
|
File path on disk. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if file is a PDF, False otherwise. |
Source code in zocrates/rag/document_converters.py
DirectReadConverter ¶
Bases: BaseDocumentConverter
Direct UTF-8 reader for Markdown and plain text files (<1ms bypass).
convert ¶
Read Markdown or text content directly from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
source_path
|
Path
|
Path to the source file. |
required |
content_type
|
str
|
MIME type of the file. |
required |
cache_dir
|
Path
|
Item-level bundle directory (unused by this converter). |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
UTF-8 content string, or None if empty. |
Source code in zocrates/rag/document_converters.py
supports ¶
Check if content type or file extension corresponds to plain text or Markdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str
|
MIME type string. |
required |
source_path
|
Path
|
File path on disk. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if file is plain text or Markdown, False otherwise. |
Source code in zocrates/rag/document_converters.py
DoclingLocalConverter ¶
Bases: BaseDocumentConverter
Document converter using IBM Docling for deep layout analysis and table extraction.
Initialize the DoclingLocalConverter strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
converter_instance
|
Any | None
|
Optional pre-configured Docling DocumentConverter instance. |
None
|
Source code in zocrates/rag/document_converters.py
converter_version
property
¶
Return the installed docling package version with mode indicator.
convert ¶
Convert document using Docling and save AST, images, and tables to bundle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
content_type
|
str
|
MIME type of the file. |
required |
cache_dir
|
Path
|
Item-level bundle directory ( |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Converted Markdown string, or None if conversion failed or returned empty text. |
Source code in zocrates/rag/document_converters.py
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 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 | |
supports ¶
Check if content type or file extension is supported by Docling.
Supports PDF, HTML, DOCX, PPTX, XLSX, etc.
Source code in zocrates/rag/document_converters.py
DocumentProcessor ¶
DocumentProcessor(cache_dir, converters=None, document_converter_id=None, datalab_non_pdf_fallback='markitdown')
Converts attachments (PDF, HTML, etc.) to Markdown with local caching and image extraction. Manages cache persistence and delegates conversion to registered BaseDocumentConverters.
Initialize the DocumentProcessor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Directory used to store converted Markdown files and the
|
required |
converters
|
list[BaseDocumentConverter] | None
|
Optional list of document converters. If None, defaults to [DirectReadConverter(), MarkItDownConverter(), DoclingLocalConverter(), DatalabConverter()]. |
None
|
document_converter_id
|
str | None
|
Global active converter ID from config.
When set, |
None
|
datalab_non_pdf_fallback
|
str
|
Fallback converter ID when active converter is 'datalab' and content_type is not PDF ('markitdown' or 'docling_local'). |
'markitdown'
|
Source code in zocrates/rag/document_processor.py
datalab_non_pdf_fallback
property
writable
¶
Return the fallback converter ID for non-PDF items when Datalab is active.
clear_failure ¶
Clear failure record(s) for an item and optionally a specific converter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
converter_id
|
str | None
|
Specific converter ID to clear, or None to clear all. |
None
|
Source code in zocrates/rag/document_processor.py
convert_with ¶
convert_with(item_key, converter_id, source_path, content_type, parent_item_type=None, title=None, collections=None, parent_item_key=None, authors=None, year=None, publication=None, doi=None, url=None, abstract=None, abstract_source=None, update_root_index=True)
Convert a document with a specific converter engine.
This is the explicit user-action path. No fallback -- if the specified converter doesn't support the content type, an error is raised. Any existing conversion for this item is destroyed and replaced.
The caller is responsible for re-indexing the vector store after this method returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
converter_id
|
str
|
ID of the converter to use. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
content_type
|
str
|
MIME type of the source file. |
required |
parent_item_type
|
str | None
|
Zotero parent item type. |
None
|
title
|
str | None
|
Parent item title. |
None
|
collections
|
list[Any] | None
|
Parent item collections. |
None
|
parent_item_key
|
str | None
|
Parent Zotero item key string. |
None
|
authors
|
list[str] | None
|
Optional author names list. |
None
|
year
|
str | int | None
|
Optional publication year. |
None
|
publication
|
str | None
|
Optional publication venue string. |
None
|
doi
|
str | None
|
Optional DOI string. |
None
|
url
|
str | None
|
Optional source URL string. |
None
|
abstract
|
str | None
|
Optional item abstract. |
None
|
abstract_source
|
str | None
|
Optional source of item abstract. |
None
|
update_root_index
|
bool
|
If True (default), updates the root index.md. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The converted Markdown text (with YAML frontmatter). |
Raises:
| Type | Description |
|---|---|
UnsupportedConversionError
|
If the converter doesn't support the content type. |
KeyError
|
If no converter with |
Source code in zocrates/rag/document_processor.py
579 580 581 582 583 584 585 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 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 | |
delete_cached ¶
Delete an item bundle from disk and conversion_meta DB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key to remove. |
required |
update_root_index
|
bool
|
If True (default), updates the root index.md catalog. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if an item bundle or DB row was deleted, False if item was not cached. |
Source code in zocrates/rag/document_processor.py
ensure_parent_item_key ¶
Ensure an item in conversion_meta has parent_item_key populated.
Source code in zocrates/rag/document_processor.py
get_all_meta_keys ¶
Return the set of all unique item keys tracked in conversion_meta.
Source code in zocrates/rag/document_processor.py
get_all_metadata ¶
Return metadata for all cached documents in a single fast query.
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
Dictionary mapping |
Source code in zocrates/rag/document_processor.py
get_failures ¶
Retrieve recorded conversion failures for an item key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
Dict mapping converter_id -> failure details dict. |
Source code in zocrates/rag/document_processor.py
get_metadata ¶
Fetch metadata dictionary for an item key from conversion_meta.db.
Source code in zocrates/rag/document_processor.py
get_or_convert ¶
get_or_convert(item_key, source_path, content_type, parent_item_type=None, title=None, collections=None, parent_item_key=None, authors=None, year=None, publication=None, doi=None, url=None, abstract=None, abstract_source=None, update_root_index=True)
Return the Markdown for an attachment, converting if not already cached or if the source file has changed since the last conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
content_type
|
str
|
MIME type of the source file. |
required |
parent_item_type
|
str | None
|
Zotero parent item type (e.g. 'journalArticle'). |
None
|
title
|
str | None
|
Parent item title. |
None
|
collections
|
list[Any] | None
|
Parent item collections list. |
None
|
parent_item_key
|
str | None
|
Parent Zotero item key string. |
None
|
authors
|
list[str] | None
|
Optional author names list. |
None
|
year
|
str | int | None
|
Optional publication year. |
None
|
publication
|
str | None
|
Optional publication venue string. |
None
|
doi
|
str | None
|
Optional DOI string. |
None
|
url
|
str | None
|
Optional source URL string. |
None
|
abstract
|
str | None
|
Optional abstract or summary text. |
None
|
abstract_source
|
str | None
|
Optional source of abstract ('zotero', 'generated', 'extractive'). |
None
|
update_root_index
|
bool
|
If True (default), updates the root index.md catalog. |
True
|
Returns:
| Type | Description |
|---|---|
str | None
|
The converted Markdown text with YAML frontmatter, or None on error. |
Source code in zocrates/rag/document_processor.py
is_cached ¶
Return True if a converted Markdown file exists for item_key.
Does not validate freshness against the source file hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in zocrates/rag/document_processor.py
list_cached_keys ¶
Return the item keys of all documents currently in the Markdown cache.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of item key strings (subdirectories containing |
Source code in zocrates/rag/document_processor.py
read_cached ¶
Read cached Markdown without triggering conversion.
Source code in zocrates/rag/document_processor.py
record_failure ¶
Record or update a failed conversion attempt for an item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
converter_id
|
str
|
Converter engine ID that failed. |
required |
error_message
|
str
|
Error description or exception string. |
required |
source_hash
|
str
|
SHA-256 hex digest of the source file at failure time. |
required |
Source code in zocrates/rag/document_processor.py
regenerate_root_index ¶
register_converter ¶
Register a new document converter strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
converter
|
BaseDocumentConverter
|
Instance of BaseDocumentConverter to register. |
required |
prepend
|
bool
|
If True (default), inserts the converter at the highest priority. |
True
|
Source code in zocrates/rag/document_processor.py
update_abstract ¶
Update abstract and abstract_source in conversion_meta for an item.
Source code in zocrates/rag/document_processor.py
update_indexed_at ¶
Update the indexed_at timestamp for item_key in conversion_meta DB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
timestamp
|
str | None
|
Optional ISO-8601 timestamp string. Defaults to current UTC time. |
None
|
Source code in zocrates/rag/document_processor.py
MarkItDownConverter ¶
Bases: BaseDocumentConverter
Document converter using Microsoft MarkItDown and pdfimages for embedded images.
Initialize the MarkItDownConverter strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markitdown_instance
|
Any | None
|
Optional pre-configured MarkItDown instance. |
None
|
Source code in zocrates/rag/document_converters.py
convert ¶
Convert document using MarkItDown and inject extracted PDF images if applicable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
content_type
|
str
|
MIME type of the file. |
required |
cache_dir
|
Path
|
Item-level bundle directory ( |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Converted Markdown string, or None if conversion failed or returned empty text. |
Source code in zocrates/rag/document_converters.py
supports ¶
MarkItDown serves as the default fallback converter for all supported documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str
|
MIME type of the document. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Always returns True. |
Source code in zocrates/rag/document_converters.py
UnsupportedConversionError ¶
Bases: Exception
Raised when a converter does not support the given content type.
_file_sha256 ¶
Compute the SHA-256 hex digest of a file.
Reads in 64 KiB chunks to keep memory usage constant for large PDFs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Absolute path to the file to hash. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Lowercase hex string of the SHA-256 digest. |
Source code in zocrates/rag/document_processor.py
_migrate_storage_paths_to_relative ¶
Migrate legacy absolute source_path entries to storage-relative paths.
Idempotent and atomic. Upgrades PRAGMA user_version from 0 to 1. Creates a pre-migration snapshot backup if not already present.
Source code in zocrates/rag/document_processor.py
_open_meta_db ¶
Open (or create) the conversion metadata SQLite database.
Creates the conversion_meta table if it does not already exist.
Enables WAL mode for concurrent readers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Directory containing the |
required |
Returns:
| Type | Description |
|---|---|
Connection
|
An open |
Connection
|
|
Source code in zocrates/rag/document_processor.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
_relativize_staging_markdown_links ¶
Rewrite any temporary staging directory paths/URIs to bundle-relative images/ links.
Source code in zocrates/rag/document_processor.py
_sanitize_cached_bundle_image_links ¶
Sanitize legacy file:///.../images/ absolute links in cached document.md files.
Ensures strict line-count invariance to preserve ToC and chunk line offsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Root corpus directory. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Count of sanitized document bundles. |
Source code in zocrates/rag/document_processor.py
build_frontmatter ¶
build_frontmatter(item_key, parent_item_type, title, collections, source_path, source_hash, content_type, converter_id, converter_version, converted_at, parent_item_key=None, parse_quality_score=None, checkpoint_id=None, authors=None, year=None, publication=None, doi=None, url=None, keywords=None, bundle_dir=None)
Build OKF-0.2 YAML frontmatter string.
Args:
item_key: Zotero item key.
parent_item_type: Zotero item type (e.g., 'journalArticle'). Defaults to 'document'.
title: Title of the parent item.
collections: List of ZoteroCollection objects or dicts with name and key/collection_key.
source_path: Absolute path to the source file on disk.
source_hash: SHA-256 hex digest of the source file.
content_type: MIME type of the source file.
converter_id: Converter engine ID.
converter_version: Converter engine version string.
converted_at: ISO-8601 timestamp string.
parent_item_key: Parent item key if item is an attachment.
parse_quality_score: Optional parse quality score (e.g. 0.0 to 5.0 from Datalab).
checkpoint_id: Optional converter checkpoint ID.
authors: Optional list of creator/author name strings.
year: Optional publication year (int or string).
publication: Optional publication venue or publisher string.
doi: Optional normalized DOI string.
url: Optional source URL string.
bundle_dir: Optional bundle directory (e.g. cache_dir / item_key). If provided,
source.path is computed as a relative POSIX path from bundle_dir.
Returns:
Formatted YAML frontmatter block ending with '---
'.
Source code in zocrates/rag/bundle_catalog.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
convert_note_html_to_markdown ¶
Convert Zotero note HTML content to clean, structured Markdown.
Handles headings (h1–h6), paragraphs, ordered/unordered lists, blockquotes, pre/code blocks, tables, inline strong/em/code, anchors, and horizontal rules. Falls back to basic regex stripping if BeautifulSoup is unavailable.
Source code in zocrates/rag/document_processor.py
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 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 | |
generate_item_index_md ¶
Generate item-level index.md catalog inside item_dir.
Catalogs main document as well as all associated image and table assets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_dir
|
Path
|
Bundle directory (corpus/ |
required |
item_key
|
str
|
Zotero item key. |
required |
title
|
str | None
|
Title of the parent item. |
required |
converter_id
|
str
|
Converter engine ID. |
required |
converter_version
|
str
|
Converter engine version string. |
required |
Source code in zocrates/rag/bundle_catalog.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
generate_root_index_md ¶
Regenerate root-level index.md catalog from conversion_meta.db.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Corpus directory containing conversion_meta.db. |
required |
Source code in zocrates/rag/bundle_catalog.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 | |
materialize_note_bundle ¶
Create or refresh an OKF bundle for a Zotero note in the markdown cache.
The bundle is placed at <cache_dir>/<note.note_key>/ and contains
document.md, toc.json, and toc.md. Staleness is detected via
a SHA-256 hash of the raw HTML stored in conversion_meta.db.
Summary policy: never calls an LLM. - Notes < 100 plain-text words → no abstract. - Notes ≥ 100 plain-text words → deterministic extractive lead paragraph (first non-heading paragraph, capped at 300 characters).
Thread-safe: uses a process-wide lock and an atomic directory swap
(write to .tmp_{key} then rename).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
note
|
Any
|
A |
required |
syncer
|
Any
|
The active |
required |
cache_dir
|
Path | None
|
Override for the cache directory; defaults to
|
None
|
Returns:
| Type | Description |
|---|---|
tuple[Path, str]
|
Tuple of |
Source code in zocrates/rag/document_processor.py
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 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 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 | |
normalize_to_storage_relative ¶
Normalize a source document path to storage-relative format or verbatim URI.
Normalizes storage paths to 'storage/
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_path
|
str | Path | None
|
File path on disk, URI, or storage path string. |
required |
zotero_profile_dir
|
Path | None
|
Optional root Zotero profile directory. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Normalized storage-relative path string, URI, or absolute POSIX path. |
Source code in zocrates/rag/bundle_catalog.py
regenerate_item_relations_md ¶
Regenerate item_relations.md and refresh index.md for a given bundle.
Queries item_relations from conversion_meta.db for source_key = item_key. Also queries conversion_meta for target titles (fallback to target_key if not found). Target directory is target_bundle_dir if provided, else cache_dir / item_key. If target directory does not exist, returns None. Writes relations markdown atomically using .tmp_item_relations.md and os.replace (P1-4 pattern). Refreshes index.md catalog with the new/updated item_relations.md link.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
cache_dir
|
Path
|
Markdown cache directory containing conversion_meta.db. |
required |
target_bundle_dir
|
Path | None
|
Optional bundle directory override (e.g. staging dir). |
None
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the materialized item_relations.md, or None if target directory does not exist. |
Source code in zocrates/rag/bundle_catalog.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | |
sanitize_image_links_in_markdown ¶
Sanitize legacy file:///.../images/ absolute links in markdown text.
Ensures strict line-count invariance to preserve ToC and chunk line offsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Raw markdown text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Markdown text with file:///.../images/ |
Source code in zocrates/rag/document_processor.py
strip_existing_frontmatter ¶
Strip leading YAML frontmatter block if present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown
|
str
|
Input Markdown string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Markdown content with leading YAML frontmatter removed. |
Source code in zocrates/rag/bundle_catalog.py
wrap_standalone_markdown_tables ¶
Wrap standalone GFM Markdown tables not already enclosed in
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown_text
|
str
|
Raw or converted Markdown text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Markdown with standalone tables enclosed in |