import sys, re

text = sys.stdin.read().strip()
raw = text.replace("<", "&lt;").replace(">", "&gt;")

# TODO do a combination of line-based and regexps.  lines for the lists and
# headings and whatnot.

# pull out the title
title, text = map(str.strip, text.split("\n", 1))

# ! -> h2, !! -> h3, !!! -> h4
text = ''.join(['\n', text]) #blarg, need to match beginning/end of line
text = re.sub(r"\n!!!\s*(.*?)\s*\n", r"\n<h4>\1</h4>\n\n", text)
text = re.sub( r"\n!!\s*(.*?)\s*\n", r"\n<h3>\1</h3>\n\n", text)
text = re.sub(  r"\n!\s*(.*?)\s*\n", r"\n<h2>\1</h2>\n\n", text)

# * foo -> <li>foo</li>
# TODO <ul> and </ul> -- in the meantime, browsers deal with this fine
text = re.sub(r"\n((\*\s+.*?\s*\n)+)", r"\n<ul>\n\1\n</ul>\n", text)
text = re.sub(r"\n\*\s+([^\n]*)", r"\n<li>\1</li>", text)
text = re.sub("<ul>\s+<li>", "<ul>\n<li>", text)
text = re.sub("</li>\s+</ul>", "</li>\n</ul>", text)

# [[name | url]] -> <a href="url">name</a>
text = re.sub(r"\[\[\s*([^\s\]]+)\s*\|\s*([^\s\]]+)\s*\]\]",
              r'<a href="\2">\1</a>', text)

# TODO insert elipses for long urls?
# [[url]] -> <a href="url">url</a>
text = re.sub(r"\[\[\s*([^\s\]]+)\s*\]\]",
              r'<a href="\1">\1</a>', text)

# one \n -> space
# multiple \n -> <p>
text = re.sub("[ \t]*\n([^\n])", " \\1", text)
text = re.sub("[ \t]*\n+[ \t]*", "\n\n<p>\n\n", text)

##################################

body = text

template = \
"""
<html>
<head>
    <title>%(title)s</title>
    <style type="text/css"><!--
        h1 { color: #006; }
        h2 { color: #900; }
        h3 { color: #006; }
        h4 { color: #006; }
        pre
        {
            border: 1px solid #ccc;
            padding: 10px;
            background: #f8f8f8;
        }
        code { color: #080; }
    --></style>
</head>

<body>

<h1>%(title)s</h1>

%(body)s

<!-- RAW INPUT

%(raw)s

-->

<hr>

<address>Generated by statiki</address>

</body>
</html>
"""

if len(sys.argv) > 1:
    template = file(sys.argv).read()

sys.stdout.write(template % vars())



