view textshaper/indent.py @ 9:71fb16088d54

add file for indentation; wth did my work go??? :(
author Jeff Hammel <k0scist@gmail.com>
date Fri, 17 Jan 2014 18:17:59 -0800
parents
children 466386702968
line wrap: on
line source

#!/usr/bin/env python

"""
indentation of text blocks
"""

import optparse
import os
import sys

def indent(text, indentation=4, space=' ', strict=False):
    """
    indent a block of text

    text -- lines of text to indent
    indentation -- number of spaces to indent
    space -- what to indent with
    strict -- whether to enforce required whitespace for negative indentation
    """

    if not indentation:
        # nothing to do
        return text

    if indentation > 0:
        retval = [space * indentation + line for line in text]
    else:
        # negative indentation
        indentation = -indentation
        retval = []
        for line in text:
            prefix = line[:indentation]
            for index, char in enumerate(prefix):
                if not char == space:
                    if strict:
                        raise AssertionError("Found non-'%s' charcter at column %d for indentation -%d" % (space, index, indentation))
                    break
            else:
                index = indentation
            retval.append(line[index:])

def main(args=sys.argv[1:]):

    usage = '%prog [options]'
    parser = optparse.OptionParser(usage=usage, description=__doc__)
    options, args = parser.parse_args(args)


if __name__ == '__main__':
    main()