import argparse
import sys
from . import strip_markdown


def main():
    parser = argparse.ArgumentParser(description="Strip markdown")
    parser.add_argument("-i", "--input", type=str,
                        help="Input file to strip markdown from. "
                        "Defaults to stdin.")
    parser.add_argument("-o", "--output", type=str,
                        help="Output file to write the stripped "
                        "text to. Defaults to stdout.")
    parser.add_argument("--mask", type=str, default=None,
                        help="Comma-separated elements to strip: table, link,"
                             " image, code. Default: all")
    parser.add_argument("-t", "--timeout", type=float, default=None,
                        help="Timeout in seconds for the stripping operation.")
    args = parser.parse_args()

    if args.input:
        with open(args.input, "r") as f:
            text = f.read()
    else:
        text = sys.stdin.read()

    mask = None
    if args.mask is not None:
        mask = [s.strip() for s in args.mask.split(',') if s.strip()]

    stripped_text = strip_markdown(text, mask=mask, timeout=args.timeout)

    if args.output:
        with open(args.output, "w") as f:
            f.write(stripped_text)
    else:
        sys.stdout.write(stripped_text)


if __name__ == "__main__":
    main()
