Retrieve page content for a document. pages format: '5-7', '3,8', or '12' For PDF: pages are physical page numbers (1-indexed). For Markdown: pages are line numbers corresponding to node headers. Returns JSON list of {'page': int, 'content': str}.
(documents: dict, doc_id: str, pages: str)
| 108 | |
| 109 | |
| 110 | def get_page_content(documents: dict, doc_id: str, pages: str) -> str: |
| 111 | """ |
| 112 | Retrieve page content for a document. |
| 113 | |
| 114 | pages format: '5-7', '3,8', or '12' |
| 115 | For PDF: pages are physical page numbers (1-indexed). |
| 116 | For Markdown: pages are line numbers corresponding to node headers. |
| 117 | |
| 118 | Returns JSON list of {'page': int, 'content': str}. |
| 119 | """ |
| 120 | doc_info = documents.get(doc_id) |
| 121 | if not doc_info: |
| 122 | return json.dumps({'error': f'Document {doc_id} not found'}) |
| 123 | |
| 124 | try: |
| 125 | page_nums = _parse_pages(pages) |
| 126 | except (ValueError, AttributeError) as e: |
| 127 | return json.dumps({'error': f'Invalid pages format: {pages!r}. Use "5-7", "3,8", or "12". Error: {e}'}) |
| 128 | |
| 129 | try: |
| 130 | if doc_info.get('type') == 'pdf': |
| 131 | content = _get_pdf_page_content(doc_info, page_nums) |
| 132 | else: |
| 133 | content = _get_md_page_content(doc_info, page_nums) |
| 134 | except Exception as e: |
| 135 | return json.dumps({'error': f'Failed to read page content: {e}'}) |
| 136 | |
| 137 | return json.dumps(content, ensure_ascii=False) |
no test coverage detected