#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

"""
======================================================
(buml and inline markup)-to-xml command line processor
======================================================

This module is a part of *buml*.

:Author and Copyright:  Bud P. Bruegger
:License:               FreeBSD

This module is a command line converter from buml with inline markup to xml
using the default settings.  
"""
import sys, os

parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parent_dir)

from buml.bumlParser import Parser
from buml.inlineMapper import InlineMapper
from buml.transformUtils import applyInlineMarkup

usage = """
===============================================
bi2x -- buml+inlineMarkup to xml/html converter
===============================================

Usage:  bi2x                   reads from STDIN and writes to STDOUT
        bi2x infile            reads from infile and writes to STDOUT
        bi2x infile outfile    reads from infile and writes to outfile
        bi2x -h                help (prints this message)

Note that the renderingMode is set to "noWhiteSpace" in order to get the
expected result for html viewed by a browser.
"""

if len(sys.argv) >= 2:
    if sys.argv[1].startswith('-'):
        print usage
        sys.exit(1)
    else:
        inf = open(sys.argv[1], 'r')
else:
    inf = sys.stdin
if len(sys.argv) == 3:
    outf = open(sys.argv[2], 'w')
else:
    outf = sys.stdout

parser = Parser()
rootNode = parser.parseBuml(inf.read())
im = InlineMapper()
spec = im.mappingSpec()
applyInlineMarkup(rootNode, spec)
rootNode.setRenderMode('noWhiteSpace')
outf.write(rootNode.xml()) 

