#!/usr/bin/python
import sys
import re
import htmlentitydefs
import string
import textwrap
def main ():
global output
global title
global wrap_width
global what
output = ""
title = ""
wrap_width = 75
text = file (sys.argv[1]).read ()
what = sys.argv[2]
# remove trailing whitespace
text = re.sub ("(?m)[ \t]+$", "", text)
# remove multiple empty lines
text = re.sub ("([^\n]+)\n{3,}", "\\1\n\n", text)
# extract title (first line)
text = re.sub ("(?m)^(.*)$", add_title, text, 1)
# extract sections (title + contents)
text = re.sub ("(?s)\n(=.*?)\n\n(.*?)(?=\n=|$)", add_section, text)
if what == "html":
print "<html>"
print "<head>"
print "<title>%s</title>" % title
print "<style>"
print "body {margin-left: 10px; background-color: #fffefd; width: 750px;}"
print "p {margin-left: 20px; }"
print "pre {margin-left: 30px; background-color: #ffefdf}"
print "</style>"
print "</head>"
print "<body>"
print output
print "</body>"
print "</html>"
else:
lines = textwrap.wrap ("This is the text version of the documentation, " +
"which is autogenerated from the HTML version.", wrap_width)
for line in lines:
print line
print "\n"
print output
def add_title (matchob):
global title
text = matchob.group (0)
title = text
return ""
def beautify (text):
global what
if what == "html":
#replace html special chars
text = re.sub ("&", "&", text)
text = re.sub ("<", "<", text)
text = re.sub (">", ">", text)
#replace links
text = re.sub ("(http://\\S+\\w)", "<a href=\"\\1\">\\1</a>", text)
#remove leading and superfluous whitespace
text = re.sub ("\n", " ", text)
text = re.sub (" {2,}", " ", text)
text = re.sub ("^ ", "", text)
return text
def put_list_item (matchob):
global what
text = matchob.group (1)
text = beautify (text)
if what == "html":
return "<li>" + text + "</li>"
else:
return "* " + text + "\n"
def put_list (matchob):
global what
text = matchob.group (0)
if what == "html":
return "<ul>\n" + re.sub ("(?m)^-(.*)$", put_list_item, text) + "</ul>\n"
else:
return re.sub ("(?m)\n*-(.*)", put_list_item, text)
def put_code (matchob):
global what
text = matchob.group (0)
if text in ["+", "+\n"]:
return ""
if what == "html":
#replace html special chars
text = re.sub ("&", "&", text)
text = re.sub ("<", "<", text)
text = re.sub (">", ">", text)
return "<pre>" + re.sub ("(?m)^\\+", "", text) + "</pre>\n"
else:
text = re.sub ("^\n*", "", text)
text = re.sub ("(?m)^\\+", "", text)
return text + "\n"
def put_paragraph (matchob):
global wrap_width, what
text = matchob.group (0)
text = beautify (text)
#output text
lines = textwrap.wrap (text, wrap_width)
if what == "html":
text = "<p>\n"
else:
text = ""
for line in lines:
text += line + "\n"
text += "\n"
if what == "html":
text += "</p>\n"
return text
def add_text (text):
#replace paragraphs (sequences of lines not starting with + or -)
text = re.sub ("(^|\n)([^+\\-\n][^\n]*(\n|$))+", put_paragraph, text)
#replace lists (- at beginning of line)
text = re.sub ("(^|\n)(-[^\n]*(\n|$))+", put_list, text)
#replace preformatted text (+ at beginning of line)
text = re.sub ("(^|\n)(\\+[^\n]*(\n|$))+", put_code, text)
return text
def add_section (matchob):
global what
global output
global wrap_width
header = matchob.group (1)
level = string.count (header, "=")
header = beautify (header[level:])
if what == "html":
output += ("<h%i>" % level) + header + "</h%i>\n" % level
else:
if level == 1:
output += "," + "-" * (wrap_width - 2) + ".\n"
t = "| " + header
t += " " * (wrap_width - len (t) - 2)
output += t + " |\n"
output += "`" + "-" * (wrap_width - 2) + "'\n"
output += "\n"
elif level == 2:
output += "\n" + header + "\n"
output += "~" * wrap_width + "\n\n"
else:
output += header + "\n\n"
output += add_text (matchob.group (2))
return ""
main ()