import sys, re
text = sys.stdin.read().strip()
raw = text.replace("<", "<").replace(">", ">")
# 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
\1
\n\n", text)
text = re.sub( r"\n!!\s*(.*?)\s*\n", r"\n\1
\n\n", text)
text = re.sub( r"\n!\s*(.*?)\s*\n", r"\n\1
\n\n", text)
# * foo -> foo
# TODO -- in the meantime, browsers deal with this fine
text = re.sub(r"\n((\*\s+.*?\s*\n)+)", r"\n\n", text)
text = re.sub(r"\n\*\s+([^\n]*)", r"\n\1", text)
text = re.sub("\s+- ", "
\n- ", text)
text = re.sub("
\s+
", " \n
", text)
# [[name | url]] -> name
text = re.sub(r"\[\[\s*([^\s\]]+)\s*\|\s*([^\s\]]+)\s*\]\]",
r'\1', text)
# TODO insert elipses for long urls?
# [[url]] -> url
text = re.sub(r"\[\[\s*([^\s\]]+)\s*\]\]",
r'\1', text)
# one \n -> space
# multiple \n ->
text = re.sub("[ \t]*\n([^\n])", " \\1", text)
text = re.sub("[ \t]*\n+[ \t]*", "\n\n
\n\n", text)
##################################
body = text
template = \
"""
%(title)s
%(title)s
%(body)s
Generated by statiki
"""
if len(sys.argv) > 1:
template = file(sys.argv).read()
sys.stdout.write(template % vars())