Process code file
(self, file_path: str)
| 20 | } |
| 21 | |
| 22 | def process(self, file_path: str) -> StructuredDocument: |
| 23 | """Process code file""" |
| 24 | file_ext = Path(file_path).suffix.lower()[1:] |
| 25 | doc_type = DocumentType.from_file_extension(file_path) |
| 26 | |
| 27 | document = StructuredDocument( |
| 28 | title=Path(file_path).stem, |
| 29 | source_file=file_path, |
| 30 | doc_type=doc_type |
| 31 | ) |
| 32 | |
| 33 | try: |
| 34 | # Read code file |
| 35 | with open(file_path, 'r', encoding='utf-8') as f: |
| 36 | code_content = f.read() |
| 37 | |
| 38 | # Get language for syntax highlighting |
| 39 | language = self.LANGUAGE_MAP.get(file_ext, file_ext) |
| 40 | |
| 41 | # Create markdown representation |
| 42 | markdown = f"# {Path(file_path).name}\n\n" |
| 43 | markdown += f"```{language}\n{code_content}\n```\n" |
| 44 | |
| 45 | # Create document structure |
| 46 | main_section = DocumentSection(title=Path(file_path).name) |
| 47 | main_section.add_element(DocumentElement( |
| 48 | content=code_content, |
| 49 | element_type="code", |
| 50 | metadata={"language": language} |
| 51 | )) |
| 52 | document.add_section(main_section) |
| 53 | |
| 54 | # Store markdown |
| 55 | document.metadata["markdown"] = markdown |
| 56 | |
| 57 | except Exception as e: |
| 58 | print(f"Error processing code file: {e}") |
| 59 | error_section = DocumentSection(title="Error") |
| 60 | error_section.add_element(DocumentElement( |
| 61 | content=f"Failed to process code file: {str(e)}", |
| 62 | element_type="paragraph" |
| 63 | )) |
| 64 | document.add_section(error_section) |
| 65 | |
| 66 | return document |
nothing calls this directly
no test coverage detected