Skip to content

Commit

Permalink
slight tidy
Browse files Browse the repository at this point in the history
- follow-up to #46
  • Loading branch information
casperdcl committed Dec 22, 2024
1 parent 125e206 commit b9a4f2c
Showing 1 changed file with 14 additions and 40 deletions.
54 changes: 14 additions & 40 deletions src/markitdown/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,41 @@ def main():
description="Convert various file formats to markdown.",
prog="markitdown",
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=dedent(
"""
SYNTAX:
markitdown <OPTIONAL: FILENAME>
If FILENAME is empty, markitdown reads from stdin.
EXAMPLE:
markitdown example.pdf
OR
cat example.pdf | markitdown
OR
markitdown < example.pdf
OR to save to a file use
markitdown example.pdf -o example.md
OR
markitdown example.pdf > example.md
"""
).strip(),
epilog=dedent(
"""\
examples:
markitdown example.pdf
markitdown -o example.md example.pdf
cat example.pdf | markitdown > example.md"""
),
)

parser.add_argument(
"-v",
"--version",
action="version",
version=f"%(prog)s {__version__}",
help="show the version number and exit",
)

parser.add_argument("filename", nargs="?")
parser.add_argument(
"filename", nargs="?", help="if unspecified, defaults to stdin"
)
parser.add_argument(
"-o",
"--output",
help="Output file name. If not provided, output is written to stdout.",
help="if unspecified, defaults to stdout",
)

args = parser.parse_args()
markitdown = MarkItDown()

if args.filename is None:
markitdown = MarkItDown()
result = markitdown.convert_stream(sys.stdin.buffer)
_handle_output(args, result)
else:
markitdown = MarkItDown()
result = markitdown.convert(args.filename)
_handle_output(args, result)


def _handle_output(args, result: DocumentConverterResult):
"""Handle output to stdout or file"""
if args.output:
with open(args.output, "w", encoding="utf-8") as f:
f.write(result.text_content)
print(result.text_content, file=f)
else:
print(result.text_content)

Expand Down

0 comments on commit b9a4f2c

Please sign in to comment.