|
| 1 | +"""Thin wrapper around the MarkItDown library.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +from dataclasses import dataclass, field |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +from markitdown import ( |
| 10 | + MarkItDown, |
| 11 | + StreamInfo, |
| 12 | + MarkItDownException, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class ConversionItem: |
| 18 | + """A single input to convert: either a local file path or a URL.""" |
| 19 | + |
| 20 | + source: str |
| 21 | + is_url: bool = False |
| 22 | + |
| 23 | + # Filled in after conversion. |
| 24 | + markdown: Optional[str] = None |
| 25 | + title: Optional[str] = None |
| 26 | + error: Optional[str] = None |
| 27 | + |
| 28 | + @property |
| 29 | + def display_name(self) -> str: |
| 30 | + if self.is_url: |
| 31 | + return self.source |
| 32 | + return os.path.basename(self.source) or self.source |
| 33 | + |
| 34 | + @property |
| 35 | + def succeeded(self) -> bool: |
| 36 | + return self.markdown is not None and self.error is None |
| 37 | + |
| 38 | + def suggested_filename(self) -> str: |
| 39 | + if self.is_url: |
| 40 | + base = "".join(c if c.isalnum() else "_" for c in self.display_name) |
| 41 | + base = base.strip("_")[:60] or "output" |
| 42 | + else: |
| 43 | + base = os.path.splitext(os.path.basename(self.source))[0] or "output" |
| 44 | + return base + ".md" |
| 45 | + |
| 46 | + |
| 47 | +@dataclass |
| 48 | +class ConverterOptions: |
| 49 | + enable_plugins: bool = False |
| 50 | + keep_data_uris: bool = False |
| 51 | + |
| 52 | + |
| 53 | +def build_markitdown(options: ConverterOptions) -> MarkItDown: |
| 54 | + """Construct a MarkItDown instance for the given options.""" |
| 55 | + return MarkItDown(enable_plugins=options.enable_plugins) |
| 56 | + |
| 57 | + |
| 58 | +def convert_item( |
| 59 | + md: MarkItDown, item: ConversionItem, options: ConverterOptions |
| 60 | +) -> ConversionItem: |
| 61 | + """Convert a single item in place, capturing markdown or an error string.""" |
| 62 | + try: |
| 63 | + result = md.convert(item.source, keep_data_uris=options.keep_data_uris) |
| 64 | + item.markdown = result.markdown |
| 65 | + item.title = result.title |
| 66 | + item.error = None |
| 67 | + except MarkItDownException as exc: |
| 68 | + item.markdown = None |
| 69 | + item.error = _format_exception(exc) |
| 70 | + except FileNotFoundError: |
| 71 | + item.markdown = None |
| 72 | + item.error = "File not found." |
| 73 | + except Exception as exc: # noqa: BLE001 - surface anything to the UI |
| 74 | + item.markdown = None |
| 75 | + item.error = f"{type(exc).__name__}: {exc}" |
| 76 | + return item |
| 77 | + |
| 78 | + |
| 79 | +def _format_exception(exc: Exception) -> str: |
| 80 | + msg = str(exc).strip() |
| 81 | + if not msg: |
| 82 | + return type(exc).__name__ |
| 83 | + # Keep it to the first few lines so the UI stays readable. |
| 84 | + lines = [ln for ln in msg.splitlines() if ln.strip()] |
| 85 | + return "\n".join(lines[:6]) |
0 commit comments