"""Join Dolphin's per-page Markdown into one text file and close the stray spaces around punctuation.

Run: python3 tidy.py out/markdown/*.md > book.txt   (standard library only; pages in the order given)
Dolphin writes `" Who's`, `“ Well` and `it ?`: a space after an opening quote, before a closing one, and before
? ! ; : , . (straight and curly quotes, page by page).
In each paragraph the 1st, 3rd, 5th... straight quote opens and the others close; a quote that starts the
paragraph opens and one that ends it closes. A heuristic: it can misplace a quote that the page itself leaves open.
"""

import re
import sys


def tidy(par):
    par, n = par.strip(), 0

    def quote(m):
        nonlocal n
        n = 1 if m.start() == 0 else n + 1  # a quote that starts the paragraph opens
        opens = n % 2 and m.end() < len(par)  # a quote that ends the paragraph closes
        return ' "' if opens else '" '

    # curly quotes say which way they face
    par = re.sub(r"“\s+", "“", re.sub(r"\s+”", "”", par))
    par = re.sub(r'\s*"\s*', quote, par)
    par = re.sub(r"\s+([?!;:,.])(?=\s|$|\")", r"\1", par)
    return par.strip()


if __name__ == "__main__":
    for path in sys.argv[1:]:
        with open(path, encoding="utf-8") as f:
            text = f.read()
        print("\n\n".join(tidy(p) for p in text.split("\n\n")))
