#!/usr/bin/env python

# Format a Python program to submit to the OEIS.

import sys

def format(f):
    """Replace leading whitespace with dots. Add "\Q" to the end of each
    line."""
    for line in f:
        if line.endswith('\n'):
            line = line[:-1]
        fmt = []
        for i in range(len(line)):
            if line[i] == ' ':
                fmt.append('.')
            elif line[i] == '\t':
                fmt.append('.' * (8 - len(fmt) % 8))
            else:
                break
        fmt.append(line[i:])
        fmt.append('\Q')
        print "".join(fmt)

if len(sys.argv) == 1:
    format(sys.stdin)
else:
    filenames = sys.argv[1:]
    for filename in filenames:
        f = open(filename)
        if len(filenames) > 1:
            print u"==> %s" % filename
        format(f)
        f.close()
