Processor for HTML files using markdownify for conversion.
| 13 | |
| 14 | |
| 15 | class HTMLProcessor(BaseProcessor): |
| 16 | """Processor for HTML files using markdownify for conversion.""" |
| 17 | |
| 18 | def can_process(self, file_path: str) -> bool: |
| 19 | """Check if this processor can handle the given file. |
| 20 | |
| 21 | Args: |
| 22 | file_path: Path to the file to check |
| 23 | |
| 24 | Returns: |
| 25 | True if this processor can handle the file |
| 26 | """ |
| 27 | if not os.path.exists(file_path): |
| 28 | return False |
| 29 | |
| 30 | # Check file extension - ensure file_path is a string |
| 31 | file_path_str = str(file_path) |
| 32 | _, ext = os.path.splitext(file_path_str.lower()) |
| 33 | return ext in ['.html', '.htm'] |
| 34 | |
| 35 | def process(self, file_path: str) -> ConversionResult: |
| 36 | """Process the HTML file and return a conversion result. |
| 37 | |
| 38 | Args: |
| 39 | file_path: Path to the HTML file to process |
| 40 | |
| 41 | Returns: |
| 42 | ConversionResult containing the processed content |
| 43 | |
| 44 | Raises: |
| 45 | FileNotFoundError: If the file doesn't exist |
| 46 | ConversionError: If processing fails |
| 47 | """ |
| 48 | if not os.path.exists(file_path): |
| 49 | raise FileNotFoundError(f"File not found: {file_path}") |
| 50 | |
| 51 | try: |
| 52 | try: |
| 53 | from markdownify import markdownify as md |
| 54 | except ImportError: |
| 55 | raise ConversionError("markdownify is required for HTML processing. Install it with: pip install markdownify") |
| 56 | |
| 57 | metadata = self.get_metadata(file_path) |
| 58 | with open(file_path, 'r', encoding='utf-8') as f: |
| 59 | html_content = f.read() |
| 60 | content = md(html_content, heading_style="ATX") |
| 61 | return ConversionResult(content, metadata) |
| 62 | except Exception as e: |
| 63 | if isinstance(e, (FileNotFoundError, ConversionError)): |
| 64 | raise |
| 65 | raise ConversionError(f"Failed to process HTML file {file_path}: {str(e)}") |
no outgoing calls
no test coverage detected