Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efc55b260d | ||
|
|
52432bd228 | ||
|
|
c0a511ecff |
@@ -1,4 +1,4 @@
|
|||||||
# SPDX-FileCopyrightText: 2024-present Adam Fourney <adamfo@microsoft.com>
|
# SPDX-FileCopyrightText: 2024-present Adam Fourney <adamfo@microsoft.com>
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: MIT
|
# SPDX-License-Identifier: MIT
|
||||||
__version__ = "0.1.0a5"
|
__version__ = "0.1.0a6"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
import codecs
|
import codecs
|
||||||
|
import locale
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
from importlib.metadata import entry_points
|
from importlib.metadata import entry_points
|
||||||
from .__about__ import __version__
|
from .__about__ import __version__
|
||||||
@@ -104,6 +105,12 @@ def main():
|
|||||||
help="List installed 3rd-party plugins. Plugins are loaded when using the -p or --use-plugin option.",
|
help="List installed 3rd-party plugins. Plugins are loaded when using the -p or --use-plugin option.",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--keep-data-uris",
|
||||||
|
action="store_true",
|
||||||
|
help="Keep data URIs (like base64-encoded images) in the output. By default, data URIs are truncated.",
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument("filename", nargs="?")
|
parser.add_argument("filename", nargs="?")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -181,9 +188,15 @@ def main():
|
|||||||
markitdown = MarkItDown(enable_plugins=args.use_plugins)
|
markitdown = MarkItDown(enable_plugins=args.use_plugins)
|
||||||
|
|
||||||
if args.filename is None:
|
if args.filename is None:
|
||||||
result = markitdown.convert_stream(sys.stdin.buffer, stream_info=stream_info)
|
result = markitdown.convert_stream(
|
||||||
|
sys.stdin.buffer,
|
||||||
|
stream_info=stream_info,
|
||||||
|
keep_data_uris=args.keep_data_uris,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
result = markitdown.convert(args.filename, stream_info=stream_info)
|
result = markitdown.convert(
|
||||||
|
args.filename, stream_info=stream_info, keep_data_uris=args.keep_data_uris
|
||||||
|
)
|
||||||
|
|
||||||
_handle_output(args, result)
|
_handle_output(args, result)
|
||||||
|
|
||||||
@@ -192,9 +205,14 @@ def _handle_output(args, result: DocumentConverterResult):
|
|||||||
"""Handle output to stdout or file"""
|
"""Handle output to stdout or file"""
|
||||||
if args.output:
|
if args.output:
|
||||||
with open(args.output, "w", encoding="utf-8") as f:
|
with open(args.output, "w", encoding="utf-8") as f:
|
||||||
f.write(result.text_content)
|
f.write(result.markdown)
|
||||||
else:
|
else:
|
||||||
print(result.text_content)
|
# Handle stdout encoding errors more gracefully
|
||||||
|
print(
|
||||||
|
result.markdown.encode(sys.stdout.encoding, errors="replace").decode(
|
||||||
|
sys.stdout.encoding
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _exit_with_error(message: str):
|
def _exit_with_error(message: str):
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ class BingSerpConverter(DocumentConverter):
|
|||||||
slug.extract()
|
slug.extract()
|
||||||
|
|
||||||
# Parse the algorithmic results
|
# Parse the algorithmic results
|
||||||
_markdownify = _CustomMarkdownify()
|
_markdownify = _CustomMarkdownify(**kwargs)
|
||||||
results = list()
|
results = list()
|
||||||
for result in soup.find_all(class_="b_algo"):
|
for result in soup.find_all(class_="b_algo"):
|
||||||
if not hasattr(result, "find_all"):
|
if not hasattr(result, "find_all"):
|
||||||
|
|||||||
@@ -73,5 +73,5 @@ class DocxConverter(HtmlConverter):
|
|||||||
|
|
||||||
style_map = kwargs.get("style_map", None)
|
style_map = kwargs.get("style_map", None)
|
||||||
return self._html_converter.convert_string(
|
return self._html_converter.convert_string(
|
||||||
mammoth.convert_to_html(file_stream, style_map=style_map).value
|
mammoth.convert_to_html(file_stream, style_map=style_map).value, **kwargs
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -56,9 +56,9 @@ class HtmlConverter(DocumentConverter):
|
|||||||
body_elm = soup.find("body")
|
body_elm = soup.find("body")
|
||||||
webpage_text = ""
|
webpage_text = ""
|
||||||
if body_elm:
|
if body_elm:
|
||||||
webpage_text = _CustomMarkdownify().convert_soup(body_elm)
|
webpage_text = _CustomMarkdownify(**kwargs).convert_soup(body_elm)
|
||||||
else:
|
else:
|
||||||
webpage_text = _CustomMarkdownify().convert_soup(soup)
|
webpage_text = _CustomMarkdownify(**kwargs).convert_soup(soup)
|
||||||
|
|
||||||
assert isinstance(webpage_text, str)
|
assert isinstance(webpage_text, str)
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class _CustomMarkdownify(markdownify.MarkdownConverter):
|
|||||||
|
|
||||||
def __init__(self, **options: Any):
|
def __init__(self, **options: Any):
|
||||||
options["heading_style"] = options.get("heading_style", markdownify.ATX)
|
options["heading_style"] = options.get("heading_style", markdownify.ATX)
|
||||||
|
options["keep_data_uris"] = options.get("keep_data_uris", False)
|
||||||
# Explicitly cast options to the expected type if necessary
|
# Explicitly cast options to the expected type if necessary
|
||||||
super().__init__(**options)
|
super().__init__(**options)
|
||||||
|
|
||||||
@@ -101,7 +102,7 @@ class _CustomMarkdownify(markdownify.MarkdownConverter):
|
|||||||
return alt
|
return alt
|
||||||
|
|
||||||
# Remove dataURIs
|
# Remove dataURIs
|
||||||
if src.startswith("data:"):
|
if src.startswith("data:") and not self.options["keep_data_uris"]:
|
||||||
src = src.split(",")[0] + "..."
|
src = src.split(",")[0] + "..."
|
||||||
|
|
||||||
return "" % (alt, src, title_part)
|
return "" % (alt, src, title_part)
|
||||||
|
|||||||
@@ -140,13 +140,20 @@ class PptxConverter(DocumentConverter):
|
|||||||
alt_text = re.sub(r"[\r\n\[\]]", " ", alt_text)
|
alt_text = re.sub(r"[\r\n\[\]]", " ", alt_text)
|
||||||
alt_text = re.sub(r"\s+", " ", alt_text).strip()
|
alt_text = re.sub(r"\s+", " ", alt_text).strip()
|
||||||
|
|
||||||
# A placeholder name
|
# If keep_data_uris is True, use base64 encoding for images
|
||||||
filename = re.sub(r"\W", "", shape.name) + ".jpg"
|
if kwargs.get("keep_data_uris", False):
|
||||||
md_content += "\n\n"
|
blob = shape.image.blob
|
||||||
|
content_type = shape.image.content_type or "image/png"
|
||||||
|
b64_string = base64.b64encode(blob).decode("utf-8")
|
||||||
|
md_content += f"\n\n"
|
||||||
|
else:
|
||||||
|
# A placeholder name
|
||||||
|
filename = re.sub(r"\W", "", shape.name) + ".jpg"
|
||||||
|
md_content += "\n\n"
|
||||||
|
|
||||||
# Tables
|
# Tables
|
||||||
if self._is_table(shape):
|
if self._is_table(shape):
|
||||||
md_content += self._convert_table_to_markdown(shape.table)
|
md_content += self._convert_table_to_markdown(shape.table, **kwargs)
|
||||||
|
|
||||||
# Charts
|
# Charts
|
||||||
if shape.has_chart:
|
if shape.has_chart:
|
||||||
@@ -193,7 +200,7 @@ class PptxConverter(DocumentConverter):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _convert_table_to_markdown(self, table):
|
def _convert_table_to_markdown(self, table, **kwargs):
|
||||||
# Write the table as HTML, then convert it to Markdown
|
# Write the table as HTML, then convert it to Markdown
|
||||||
html_table = "<html><body><table>"
|
html_table = "<html><body><table>"
|
||||||
first_row = True
|
first_row = True
|
||||||
@@ -208,7 +215,10 @@ class PptxConverter(DocumentConverter):
|
|||||||
first_row = False
|
first_row = False
|
||||||
html_table += "</table></body></html>"
|
html_table += "</table></body></html>"
|
||||||
|
|
||||||
return self._html_converter.convert_string(html_table).markdown.strip() + "\n"
|
return (
|
||||||
|
self._html_converter.convert_string(html_table, **kwargs).markdown.strip()
|
||||||
|
+ "\n"
|
||||||
|
)
|
||||||
|
|
||||||
def _convert_chart_to_markdown(self, chart):
|
def _convert_chart_to_markdown(self, chart):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -28,6 +28,10 @@ CANDIDATE_FILE_EXTENSIONS = [
|
|||||||
class RssConverter(DocumentConverter):
|
class RssConverter(DocumentConverter):
|
||||||
"""Convert RSS / Atom type to markdown"""
|
"""Convert RSS / Atom type to markdown"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self._kwargs = {}
|
||||||
|
|
||||||
def accepts(
|
def accepts(
|
||||||
self,
|
self,
|
||||||
file_stream: BinaryIO,
|
file_stream: BinaryIO,
|
||||||
@@ -82,6 +86,7 @@ class RssConverter(DocumentConverter):
|
|||||||
stream_info: StreamInfo,
|
stream_info: StreamInfo,
|
||||||
**kwargs: Any, # Options to pass to the converter
|
**kwargs: Any, # Options to pass to the converter
|
||||||
) -> DocumentConverterResult:
|
) -> DocumentConverterResult:
|
||||||
|
self._kwargs = kwargs
|
||||||
doc = minidom.parse(file_stream)
|
doc = minidom.parse(file_stream)
|
||||||
feed_type = self._feed_type(doc)
|
feed_type = self._feed_type(doc)
|
||||||
|
|
||||||
@@ -166,7 +171,7 @@ class RssConverter(DocumentConverter):
|
|||||||
try:
|
try:
|
||||||
# using bs4 because many RSS feeds have HTML-styled content
|
# using bs4 because many RSS feeds have HTML-styled content
|
||||||
soup = BeautifulSoup(content, "html.parser")
|
soup = BeautifulSoup(content, "html.parser")
|
||||||
return _CustomMarkdownify().convert_soup(soup)
|
return _CustomMarkdownify(**self._kwargs).convert_soup(soup)
|
||||||
except BaseException as _:
|
except BaseException as _:
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|||||||
@@ -76,11 +76,11 @@ class WikipediaConverter(DocumentConverter):
|
|||||||
main_title = title_elm.string
|
main_title = title_elm.string
|
||||||
|
|
||||||
# Convert the page
|
# Convert the page
|
||||||
webpage_text = f"# {main_title}\n\n" + _CustomMarkdownify().convert_soup(
|
webpage_text = f"# {main_title}\n\n" + _CustomMarkdownify(
|
||||||
body_elm
|
**kwargs
|
||||||
)
|
).convert_soup(body_elm)
|
||||||
else:
|
else:
|
||||||
webpage_text = _CustomMarkdownify().convert_soup(soup)
|
webpage_text = _CustomMarkdownify(**kwargs).convert_soup(soup)
|
||||||
|
|
||||||
return DocumentConverterResult(
|
return DocumentConverterResult(
|
||||||
markdown=webpage_text,
|
markdown=webpage_text,
|
||||||
|
|||||||
@@ -86,7 +86,9 @@ class XlsxConverter(DocumentConverter):
|
|||||||
md_content += f"## {s}\n"
|
md_content += f"## {s}\n"
|
||||||
html_content = sheets[s].to_html(index=False)
|
html_content = sheets[s].to_html(index=False)
|
||||||
md_content += (
|
md_content += (
|
||||||
self._html_converter.convert_string(html_content).markdown.strip()
|
self._html_converter.convert_string(
|
||||||
|
html_content, **kwargs
|
||||||
|
).markdown.strip()
|
||||||
+ "\n\n"
|
+ "\n\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -146,7 +148,9 @@ class XlsConverter(DocumentConverter):
|
|||||||
md_content += f"## {s}\n"
|
md_content += f"## {s}\n"
|
||||||
html_content = sheets[s].to_html(index=False)
|
html_content = sheets[s].to_html(index=False)
|
||||||
md_content += (
|
md_content += (
|
||||||
self._html_converter.convert_string(html_content).markdown.strip()
|
self._html_converter.convert_string(
|
||||||
|
html_content, **kwargs
|
||||||
|
).markdown.strip()
|
||||||
+ "\n\n"
|
+ "\n\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -25,8 +25,11 @@ GENERAL_TEST_VECTORS = [
|
|||||||
"# Abstract",
|
"# Abstract",
|
||||||
"# Introduction",
|
"# Introduction",
|
||||||
"AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
|
"AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
|
||||||
|
"data:image/png;base64...",
|
||||||
|
],
|
||||||
|
must_not_include=[
|
||||||
|
"data:image/png;base64,iVBORw0KGgoAAAANSU",
|
||||||
],
|
],
|
||||||
must_not_include=[],
|
|
||||||
),
|
),
|
||||||
FileTestVector(
|
FileTestVector(
|
||||||
filename="test.xlsx",
|
filename="test.xlsx",
|
||||||
@@ -65,8 +68,9 @@ GENERAL_TEST_VECTORS = [
|
|||||||
"AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
|
"AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
|
||||||
"a3f6004b-6f4f-4ea8-bee3-3741f4dc385f", # chart title
|
"a3f6004b-6f4f-4ea8-bee3-3741f4dc385f", # chart title
|
||||||
"2003", # chart value
|
"2003", # chart value
|
||||||
|
"",
|
||||||
],
|
],
|
||||||
must_not_include=[],
|
must_not_include=["data:image/jpeg;base64,/9j/4AAQSkZJRgABAQE"],
|
||||||
),
|
),
|
||||||
FileTestVector(
|
FileTestVector(
|
||||||
filename="test_outlook_msg.msg",
|
filename="test_outlook_msg.msg",
|
||||||
@@ -230,3 +234,45 @@ GENERAL_TEST_VECTORS = [
|
|||||||
must_not_include=[],
|
must_not_include=[],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
DATA_URI_TEST_VECTORS = [
|
||||||
|
FileTestVector(
|
||||||
|
filename="test.docx",
|
||||||
|
mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||||
|
charset=None,
|
||||||
|
url=None,
|
||||||
|
must_include=[
|
||||||
|
"314b0a30-5b04-470b-b9f7-eed2c2bec74a",
|
||||||
|
"49e168b7-d2ae-407f-a055-2167576f39a1",
|
||||||
|
"## d666f1f7-46cb-42bd-9a39-9a39cf2a509f",
|
||||||
|
"# Abstract",
|
||||||
|
"# Introduction",
|
||||||
|
"AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
|
||||||
|
"data:image/png;base64,iVBORw0KGgoAAAANSU",
|
||||||
|
],
|
||||||
|
must_not_include=[
|
||||||
|
"data:image/png;base64...",
|
||||||
|
],
|
||||||
|
),
|
||||||
|
FileTestVector(
|
||||||
|
filename="test.pptx",
|
||||||
|
mimetype="application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||||
|
charset=None,
|
||||||
|
url=None,
|
||||||
|
must_include=[
|
||||||
|
"2cdda5c8-e50e-4db4-b5f0-9722a649f455",
|
||||||
|
"04191ea8-5c73-4215-a1d3-1cfb43aaaf12",
|
||||||
|
"44bf7d06-5e7a-4a40-a2e1-a2e42ef28c8a",
|
||||||
|
"1b92870d-e3b5-4e65-8153-919f4ff45592",
|
||||||
|
"AutoGen: Enabling Next-Gen LLM Applications via Multi-Agent Conversation",
|
||||||
|
"a3f6004b-6f4f-4ea8-bee3-3741f4dc385f", # chart title
|
||||||
|
"2003", # chart value
|
||||||
|
"![This phrase of the caption is Human-written.]", # image caption
|
||||||
|
"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQE",
|
||||||
|
],
|
||||||
|
must_not_include=[
|
||||||
|
"",
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|||||||
@@ -7,9 +7,17 @@ import locale
|
|||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from _test_vectors import GENERAL_TEST_VECTORS, FileTestVector
|
from _test_vectors import (
|
||||||
|
GENERAL_TEST_VECTORS,
|
||||||
|
DATA_URI_TEST_VECTORS,
|
||||||
|
FileTestVector,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
from ._test_vectors import GENERAL_TEST_VECTORS, FileTestVector
|
from ._test_vectors import (
|
||||||
|
GENERAL_TEST_VECTORS,
|
||||||
|
DATA_URI_TEST_VECTORS,
|
||||||
|
FileTestVector,
|
||||||
|
)
|
||||||
|
|
||||||
from markitdown import (
|
from markitdown import (
|
||||||
MarkItDown,
|
MarkItDown,
|
||||||
@@ -149,6 +157,39 @@ def test_convert_url(shared_tmp_dir, test_vector):
|
|||||||
assert test_string not in stdout
|
assert test_string not in stdout
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("test_vector", DATA_URI_TEST_VECTORS)
|
||||||
|
def test_output_to_file_with_data_uris(shared_tmp_dir, test_vector) -> None:
|
||||||
|
"""Test CLI functionality when keep_data_uris is enabled"""
|
||||||
|
|
||||||
|
output_file = os.path.join(shared_tmp_dir, test_vector.filename + ".output")
|
||||||
|
result = subprocess.run(
|
||||||
|
[
|
||||||
|
"python",
|
||||||
|
"-m",
|
||||||
|
"markitdown",
|
||||||
|
"--keep-data-uris",
|
||||||
|
"-o",
|
||||||
|
output_file,
|
||||||
|
os.path.join(TEST_FILES_DIR, test_vector.filename),
|
||||||
|
],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result.returncode == 0, f"CLI exited with error: {result.stderr}"
|
||||||
|
assert os.path.exists(output_file), f"Output file not created: {output_file}"
|
||||||
|
|
||||||
|
with open(output_file, "r") as f:
|
||||||
|
output_data = f.read()
|
||||||
|
for test_string in test_vector.must_include:
|
||||||
|
assert test_string in output_data
|
||||||
|
for test_string in test_vector.must_not_include:
|
||||||
|
assert test_string not in output_data
|
||||||
|
|
||||||
|
os.remove(output_file)
|
||||||
|
assert not os.path.exists(output_file), f"Output file not deleted: {output_file}"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
@@ -156,6 +197,7 @@ if __name__ == "__main__":
|
|||||||
"""Runs this file's tests from the command line."""
|
"""Runs this file's tests from the command line."""
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||||
|
# General tests
|
||||||
for test_function in [
|
for test_function in [
|
||||||
test_output_to_stdout,
|
test_output_to_stdout,
|
||||||
test_output_to_file,
|
test_output_to_file,
|
||||||
@@ -169,4 +211,17 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
test_function(tmp_dir, test_vector)
|
test_function(tmp_dir, test_vector)
|
||||||
print("OK")
|
print("OK")
|
||||||
|
|
||||||
|
# Data URI tests
|
||||||
|
for test_function in [
|
||||||
|
test_output_to_file_with_data_uris,
|
||||||
|
]:
|
||||||
|
for test_vector in DATA_URI_TEST_VECTORS:
|
||||||
|
print(
|
||||||
|
f"Running {test_function.__name__} on {test_vector.filename}...",
|
||||||
|
end="",
|
||||||
|
)
|
||||||
|
test_function(tmp_dir, test_vector)
|
||||||
|
print("OK")
|
||||||
|
|
||||||
print("All tests passed!")
|
print("All tests passed!")
|
||||||
|
|||||||
BIN
packages/markitdown/tests/test_files/test.docx
vendored
Normal file → Executable file
BIN
packages/markitdown/tests/test_files/test.docx
vendored
Normal file → Executable file
Binary file not shown.
@@ -6,9 +6,9 @@ import codecs
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from _test_vectors import GENERAL_TEST_VECTORS
|
from _test_vectors import GENERAL_TEST_VECTORS, DATA_URI_TEST_VECTORS
|
||||||
else:
|
else:
|
||||||
from ._test_vectors import GENERAL_TEST_VECTORS
|
from ._test_vectors import GENERAL_TEST_VECTORS, DATA_URI_TEST_VECTORS
|
||||||
|
|
||||||
from markitdown import (
|
from markitdown import (
|
||||||
MarkItDown,
|
MarkItDown,
|
||||||
@@ -124,10 +124,52 @@ def test_convert_url(test_vector):
|
|||||||
assert string not in result.markdown
|
assert string not in result.markdown
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("test_vector", DATA_URI_TEST_VECTORS)
|
||||||
|
def test_convert_with_data_uris(test_vector):
|
||||||
|
"""Test API functionality when keep_data_uris is enabled"""
|
||||||
|
markitdown = MarkItDown()
|
||||||
|
|
||||||
|
# Test local file conversion
|
||||||
|
result = markitdown.convert(
|
||||||
|
os.path.join(TEST_FILES_DIR, test_vector.filename),
|
||||||
|
keep_data_uris=True,
|
||||||
|
url=test_vector.url,
|
||||||
|
)
|
||||||
|
|
||||||
|
for string in test_vector.must_include:
|
||||||
|
assert string in result.markdown
|
||||||
|
for string in test_vector.must_not_include:
|
||||||
|
assert string not in result.markdown
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("test_vector", DATA_URI_TEST_VECTORS)
|
||||||
|
def test_convert_stream_with_data_uris(test_vector):
|
||||||
|
"""Test the conversion of a stream with no stream info."""
|
||||||
|
markitdown = MarkItDown()
|
||||||
|
|
||||||
|
stream_info = StreamInfo(
|
||||||
|
extension=os.path.splitext(test_vector.filename)[1],
|
||||||
|
mimetype=test_vector.mimetype,
|
||||||
|
charset=test_vector.charset,
|
||||||
|
)
|
||||||
|
|
||||||
|
with open(os.path.join(TEST_FILES_DIR, test_vector.filename), "rb") as stream:
|
||||||
|
result = markitdown.convert(
|
||||||
|
stream, stream_info=stream_info, keep_data_uris=True, url=test_vector.url
|
||||||
|
)
|
||||||
|
|
||||||
|
for string in test_vector.must_include:
|
||||||
|
assert string in result.markdown
|
||||||
|
for string in test_vector.must_not_include:
|
||||||
|
assert string not in result.markdown
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
"""Runs this file's tests from the command line."""
|
"""Runs this file's tests from the command line."""
|
||||||
|
|
||||||
|
# General tests
|
||||||
for test_function in [
|
for test_function in [
|
||||||
test_guess_stream_info,
|
test_guess_stream_info,
|
||||||
test_convert_local,
|
test_convert_local,
|
||||||
@@ -141,4 +183,17 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
test_function(test_vector)
|
test_function(test_vector)
|
||||||
print("OK")
|
print("OK")
|
||||||
|
|
||||||
|
# Data URI tests
|
||||||
|
for test_function in [
|
||||||
|
test_convert_with_data_uris,
|
||||||
|
test_convert_stream_with_data_uris,
|
||||||
|
]:
|
||||||
|
for test_vector in DATA_URI_TEST_VECTORS:
|
||||||
|
print(
|
||||||
|
f"Running {test_function.__name__} on {test_vector.filename}...", end=""
|
||||||
|
)
|
||||||
|
test_function(test_vector)
|
||||||
|
print("OK")
|
||||||
|
|
||||||
print("All tests passed!")
|
print("All tests passed!")
|
||||||
|
|||||||
Reference in New Issue
Block a user