'Accepts a string of code or a list of code lines'

def indent_code(self, code):

    if isinstance(code, string_types):
        code_lines = self.indent_code(code.splitlines(True))
        return ''.join(code_lines)
    tab = '        '
    inc_regex = ('^function    ', '^if    ', '^elseif    ', '^else$', '^for    ')
    dec_regex = ('^end$', '^elseif    ', '^else$')
    code = [line.lstrip('     DCTB ') for line in code]
    increase = [int(any([search(re, line) for re in inc_regex])) for line in code]
    decrease = [int(any([search(re, line) for re in dec_regex])) for line in code]
    pretty = []
    level = 0
    for (n, line) in enumerate(code):
        if ((line == '') or (line == '\n')):
            pretty.append(line)
            continue
        level -= decrease[n]
        pretty.append(('%s%s' % ((tab * level), line)))
        level += increase[n]
    return pretty
