# convert.sh — turn rich documents (PDF, Office, HTML, CSV, images, …) into clean
# Markdown so their content can be read into an LLM's context cheaply.
# Backed by Microsoft's `markitdown`. Self-bootstraps a dedicated Python venv on
# first run, so it works from a fresh clone with no manual setup.
# convert.sh file1.pdf [file2.docx ...] # writes a sibling .md for each input
# convert.sh --stdout report.pdf # print Markdown to stdout, write nothing
# MARKITDOWN_VENV override the venv location (default: ~/.venvs/markitdown)
VENV="${MARKITDOWN_VENV:-$HOME/.venvs/markitdown}"
BIN="$VENV/bin/markitdown"
# Document-focused extras only — no audio/YouTube/Azure. Keeps install lean and
# offline, covers ~95% of ingestion cases.
EXTRAS='markitdown[pdf,docx,pptx,xlsx,xls,outlook]'
echo "markitdown not found — bootstrapping venv at $VENV (one-time) …" >&2
"$VENV/bin/python" -m pip install --upgrade pip --quiet
"$VENV/bin/pip" install "$EXTRAS" --quiet
[ -x "$BIN" ] || bootstrap
if [ "${1:-}" = "--stdout" ]; then
echo "usage: convert.sh [--stdout] <file> [file ...]" >&2
echo "skip (not a file): $src" >&2
echo "skip (already text): $src" >&2
if [ "$stdout_only" -eq 1 ]; then
# Report the number that actually matters for a context budget: how many tokens
# the resulting Markdown will cost to read (~4 chars/token is a rough proxy).
out_bytes=$(wc -c < "$out" | tr -d ' ')
src_bytes=$(wc -c < "$src" | tr -d ' ')
printf '%s -> %s (source %s B -> markdown %s B, ~%s tokens to read)\n' \
"$src" "$out" "$src_bytes" "$out_bytes" "$(( out_bytes / 4 ))"