'''
Wikicreole style parser. Mostly complient.
Written 2010-12-19. danomagnum.com
'''
__version__ = 1.0
import re
import math
def tocmaker(contents):
'''Creates a toc from a list of ids'''
ret = ""
for header in contents:
ret += "" + header + ""
return ret
def parse(text):
'''Parses text and returns html according to (almost) wikicreole
\'\'italic\'\' \'\'\'bold\'\'\' __underline__
[[link]] [[link|linktext]] {{image}} {{image|imagetext}}
*list
**list2
#numbered list
##numbered list 2
==Heading 1
===Heading 2
\\\\ ->
---- ->
\1' #re_italic = re.compile(r"//(.+?)//") #re_italic_s = r'\1' #re_underline = re.compile("__(.+?)__") #re_underline_s = r'\1' for line in text.split('\n'): if in_pre: if line.startswith('}}}'): outstring += '' in_pre = False else: outstring += line + "\n" continue line = line.lstrip() if line == '': for fmt in formattings: if fmt[2]: outstring += '' + fmt[1] + '>' fmt[2] = False outstring += "
" continue if line.startswith('%'): #comments start with %, so just ignore it continue if line.startswith('----'): outstring += "
"
#if you start a line with {{{format, the pre gets its class set to that format
in_pre = True
continue
for h in headings:
if line.startswith(h[0]):
line = line.strip(h[0])
outstring += "<" + h[1] + " id='" + line + "'>"
contents.append(line)
line = line + "" + h[1] + ">"
if line.startswith('#'): #check for ordered lists
if not in_olist:
outstring += ""
in_olist = 1
else:
level = len(line[:in_olist+1].split('#')) - 1
delta = math.fabs(level - in_olist)
while delta:
level = len(line[:in_olist+1].split('#')) - 1
if level > in_olist:
outstring += ""
in_olist += 1
else:
outstring += "
"
in_olist -= 1
delta = math.fabs(level - in_olist)
line = "- " + line[in_olist:] + "
"
elif in_olist:
while in_olist:
outstring += "
"
in_olist -= 1
if line.startswith('*'): #check for unordered lists
if not in_ulist:
outstring += "| " + p[1:] + " | " else: output += "" + p + " | " output += "
|---|