Add “convert_local_content” method to set failed tests
This commit is contained in:
parent
dbc93dd584
commit
9bbf547517
1 changed files with 18 additions and 0 deletions
|
|
@ -13,6 +13,7 @@ import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import traceback
|
import traceback
|
||||||
import zipfile
|
import zipfile
|
||||||
|
from tempfile import NamedTemporaryFile
|
||||||
from xml.dom import minidom
|
from xml.dom import minidom
|
||||||
from typing import Any, Dict, List, Optional, Union
|
from typing import Any, Dict, List, Optional, Union
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
@ -1431,6 +1432,23 @@ class MarkItDown:
|
||||||
elif isinstance(source, Path):
|
elif isinstance(source, Path):
|
||||||
return self.convert_local(source, **kwargs)
|
return self.convert_local(source, **kwargs)
|
||||||
|
|
||||||
|
def convert_local_content(self, content: str, **kwargs: Any) -> DocumentConverterResult:
|
||||||
|
"""Directly converts file content to Markdown."""
|
||||||
|
ext = kwargs.get("file_extension", ".unknown")
|
||||||
|
encoding = kwargs.get("encoding", "utf-8")
|
||||||
|
|
||||||
|
# Note: NamedTemporaryFile is not used because the file is not closed until the context manager exits
|
||||||
|
fd, temp_path = tempfile.mkstemp(suffix=ext, text=True)
|
||||||
|
try:
|
||||||
|
with os.fdopen(fd, "w", encoding=encoding) as temp_file:
|
||||||
|
temp_file.write(content)
|
||||||
|
|
||||||
|
result = self.convert_local(temp_path, **kwargs)
|
||||||
|
finally:
|
||||||
|
os.remove(temp_path)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
def convert_local(
|
def convert_local(
|
||||||
self, path: Union[str, Path], **kwargs: Any
|
self, path: Union[str, Path], **kwargs: Any
|
||||||
) -> DocumentConverterResult: # TODO: deal with kwargs
|
) -> DocumentConverterResult: # TODO: deal with kwargs
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue