[go: up one dir, main page]

Menu

[r60]: / agup / trunk / misc / gendoc.py  Maximize  Restore  History

Download this file

170 lines (143 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/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 ("&", "&amp;", text)
text = re.sub ("<", "&lt;", text)
text = re.sub (">", "&gt;", 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 ("&", "&amp;", text)
text = re.sub ("<", "&lt;", text)
text = re.sub (">", "&gt;", 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 ()