#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
""" textout to HTML converter for the command line. """

import sys, argparse
import textoutpc

def parse_args():
	""" Parse the arguments. """

	ap = argparse.ArgumentParser(prog='textout2html',
		description='Convert textout BBcode to HTML.')
	ap.add_argument('-o', dest='output', default=sys.stdout,
		type=lambda x: sys.stdout if x == '-' else open(x, 'w'),
		help='the output source, stdout by default.')
	ap.add_argument('--inline', dest='inline', action='store_true',
		help='only inline tags will be interpreted')
	ap.add_argument('--obsolete-tags', dest='obs_tags', action='store_true',
		help='whether to use obsolete HTML tags such as <b>')
	ap.add_argument('--label-prefix', dest='lbl_prefix',
		help='prefix to use for label tags, e.g. "msg55459-"')
	ap.add_argument('input', nargs='?', default=sys.stdin,
		type=lambda x: sys.stdin if x == '-' else open(x, 'r'),
		help='the input source, stdin by default.')

	args = ap.parse_args()
	return args

def main():
	""" Main function of the script. """

	args = parse_args()
	print(textoutpc.tohtml(args.input.read(), inline=args.inline,
		obsolete_tags=args.obs_tags, label_prefix=args.lbl_prefix),
		file=args.output, end='')

if __name__ == '__main__':
	try:
		main()
	except e:
		print("textout2html: error: " + str(e))

# End of file.
