#!/usr/bin/env python

###############################################################################
#   PyBBCodePreview 1.0 - PyBBCodePreview.py
#   Copyright (c) 2006 Andrew J. Bennieston
#   Released under the terms of the BSD license
#   http://www.opensource.org/licenses/bsd-license.php
###############################################################################

import re, string, sys

# Replacements which can be made directly, without context or regexp groups
simple_replacements = [ ('<', '&lt;'),
                        ('>', '&gt;'),
                        (r'\[b]', "<b>"),
                        (r'\[/b]', "</b>"),
                        (r'\[i]', "<i>"),
                        (r'\[/i]', "</i>"),
                        (r'\[u]', "<u>"),
                        (r'\[/u]', "</u>"),
                        (r'\[list]', "<ul>"),
                        (r'\[img]', '<img src="'),
                        (r'\[/img]', '" />'),
                        (r'\[\*]', "<li>"),
                        (r'\[code]', '<pre>'),
                        (r'\[/code]', '</pre>'),
                        ('\n', '<br />\n')
                        ]
def urlreplace1(m):
    return '<a href="' + m.group(1) + '">' + m.group(2) + '</a>'

def urlreplace2(m):
    return '<a href="' + m.group(1) + '">' + m.group(1) + '</a>'

def listreplace(m):
    retstr = ""
    if m.group(1) == "[list=a]":
        retstr = '<ol style="list-style-type: lower-alpha">'
    else:
        retstr = '<ol style="list-style=type: decimal">'
    retstr += m.group(2)
    retstr += '</ol>'
    return retstr

def sizereplace(m):
    # Use <span> to make replacements. It isn't the tidiest way of doing
    # things, but it works
    retstr = '<span style="font-size: '
    retstr += m.group(1) + 'pt">' + m.group(2) + '</span>'
    return retstr

def colorreplace(m):
    # Use <span> to make replacements. It isn't the tidiest way of doing
    # things, but it works
    retstr = '<span style="color: "' + m.group(1) + '">'
    retstr += m.group(2) + '</span>'
    return retstr

# Open the file specified in argv[1], read contents, and close it
if len(sys.argv) != 3:
    print "Usage: " + sys.argv[0] + " infile outfile"
    sys.exit(1)
infile = open(sys.argv[1], "r")
bbcode = infile.read()
infile.close()

# First make the simple replacements
for pattern in simple_replacements:
    obj = re.compile(pattern[0], re.IGNORECASE | re.MULTILINE)
    bbcode = obj.sub(pattern[1], bbcode)

# Replace lists
obj = re.compile(r'(\[list=\w])(.+?)\[/list]', re.IGNORECASE | re.MULTILINE)
bbcode = obj.sub(listreplace, bbcode)

# Replace sizes
obj = re.compile(r'\[size=(\d\d?)](.+?)\[/size]', re.IGNORECASE | re.MULTILINE)
bbcode = obj.sub(sizereplace, bbcode)

# Replace colours
obj = re.compile(r'\[color=(.+?)](.+?)\[/color]', re.IGNORECASE | re.MULTILINE)
bbcode = obj.sub(colorreplace, bbcode)

# Replace URLs
obj = re.compile(r'\[url=(.+?)](.+?)\[/url]', re.IGNORECASE | re.MULTILINE)
bbcode = obj.sub(urlreplace1, bbcode)
obj = re.compile(r'\[url](.+?)\[/url]', re.IGNORECASE | re.MULTILINE)
bbcode = obj.sub(urlreplace2, bbcode)

# Open output file, write bbcode string, close file
outfile = open(sys.argv[2], "w")
header = """<html><head>
<title>BBCode Preview</title>
</head>
<body>
<div style="border: 1px solid #ff0000;
    background-color: #ffaaaa;
    color: #ff0000;"
    align="center">
<b>This preview was generated using PyBBCodePreview 1.0</b><br />
Some formatting may not appear exactly as on the destination forum.
</div>
"""
footer = """
<div style="border: 1px solid #ff0000;
    background-color: #ffaaaa;
    color: #ff0000"
    align="center">
PyBBCodePreview 1.0 is released under the BSD License.
</div>
</body>
</html>"""
outfile.write(header)
outfile.write(bbcode)
outfile.write(footer)
outfile.close()
