#!/bin/python3
# -*- coding: utf-8 -*-
#
# This file is part of INGInious. See the LICENSE and the COPYRIGHTS files for
# more information about the licensing of this file.
#
# Tool to indent/de-indent rst block

import sys
import argparse
from inginious_container_api import rst

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, 
                                 description='Indents or de-indents a reStructuredText block.',
                                 epilog='If message is not specified, reads on stdin.\n Negative amount de-indents the block.')
parser.add_argument('-e', '--escape', help="interprets backslash escapes", action='store_true')
parser.add_argument('-c', '--indent-char', dest='indent_char', help="indentation char", default='\t')
parser.add_argument('-a', '--amount', type=int, help="amount of indentation", default=1)
parser.add_argument('-m', '--message' , help="message text", default="")
args = parser.parse_args()

amount = args.amount
message = args.message if args.message else sys.stdin.read()
text = message.encode('utf-8').decode('unicode-escape').encode('latin1').decode('utf-8') if args.escape else message
indent_char = args.indent_char

# Do the real stuff 
print(rst.indent_block(amount, text, indent_char))
