[go: up one dir, main page]

Menu

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

Download this file

295 lines (249 with data), 9.1 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/python
"""
- Whitespace and line breaks are ignored.
- The first line is the title
- One or more blank lines start a new paragraph.
- = == == and so on at the beginning are for headers.
- +contenst generates a TOC
- +img inserts an image
- + marks preformated text
- - creates a bulletted list
- http:// starts a link
"""
import sys
import re
import htmlentitydefs
import string
import textwrap
def main ():
global title
global wrap_width
global what
global section_number
global sections
sections = []
section_number = 0
title = ""
wrap_width = 75
if len (sys.argv) == 3:
text = file (sys.argv[1]).read ()
what = sys.argv[2]
else:
print "gendoc.py <filename> text|html"
sys.exit (-1)
# 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)
title_end = str.find (text, "\n\n")
add_title (text[0:title_end])
# extract sections
done = 0
section_heading_start = str.find (text, "\n\n=", title_end)
while not done:
section_heading_end = str.find (text, "\n\n", section_heading_start + 3)
if section_heading_end == -1:
section_head = text[section_heading_start + 2:]
section_text = ""
done = 1
else:
section_head = text[section_heading_start + 2:section_heading_end]
section_end = str.find (text, "\n\n=", section_heading_end)
if section_end == -1:
section_text = text[section_heading_end + 2:]
done = 1
else:
section_text = text[section_heading_end + 2:section_end]
add_section(section_head, section_text)
section_heading_start = section_end
if what == "html":
output_html ()
elif what == "text":
output_text ()
else:
print sections
def output_html ():
global title
global sections
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>"
for section in sections:
sys.stdout.write (("<a name = \"%i\"" % section["number"]) + "</a>\n")
sys.stdout.write (("<h%i>" % section["level"]) + section["header"] + "</h%i>\n" % section["level"])
for paragraph in section["text"]:
if paragraph["type"] == "code":
text = paragraph["data"]
#replace html special chars
text = re.sub ("&", "&amp;", text)
text = re.sub ("<", "&lt;", text)
text = re.sub (">", "&gt;", text)
sys.stdout.write ("<pre>" + text + "</pre>\n")
elif paragraph["type"] == "list":
list = paragraph["data"]
sys.stdout.write ("<ul>\n")
for item in list:
sys.stdout.write ("<li>" + item + "</li>\n")
sys.stdout.write ("</ul>\n")
elif paragraph["type"] == "text":
text = paragraph["data"]
sys.stdout.write ("<p>" + text + "</p>")
elif paragraph["type"] == "contents":
level = 0
for toc in sections:
while level < toc["level"]:
sys.stdout.write ("<ul>\n")
level += 1
while level > toc["level"]:
sys.stdout.write ("</ul>\n")
level -= 1
sys.stdout.write ("<li><a href=\"#%i\">\n" % toc["number"])
sys.stdout.write (toc["header"])
sys.stdout.write ("</li></a>\n")
while level > 0:
sys.stdout.write ("</ul>\n")
level -= 1
elif paragraph["type"] == "img":
img = string.split (paragraph["data"])
if len (img) > 1:
sys.stdout.write ("""<a href="%s">""" % img[1])
sys.stdout.write ("""<img src="%s" />""" % img[0])
if len (img) > 1:
sys.stdout.write ("</a>")
print "</body>"
print "</html>"
def output_text ():
global title
global sections
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
for section in sections:
level = section["level"]
header = section["header"]
if level == 1:
sys.stdout.write ("," + "-" * (wrap_width - 2) + ".\n")
t = "| " + header
t += " " * (wrap_width - len (t) - 2)
sys.stdout.write (t + " |\n")
sys.stdout.write ("`" + "-" * (wrap_width - 2) + "'\n")
sys.stdout.write ("\n")
elif level == 2:
sys.stdout.write ("\n" + header + "\n")
sys.stdout.write ("~" * wrap_width + "\n\n")
else:
sys.stdout.write ("_" * wrap_width + "\n")
sys.stdout.write (header + "\n\n")
for paragraph in section["text"]:
if paragraph["type"] == "list":
list = paragraph["data"]
for item in list:
lines = textwrap.wrap (item, wrap_width - 3)
sys.stdout.write ("\n * " + lines[0] + "\n")
for line in lines[1:]:
sys.stdout.write(" " + line + "\n")
sys.stdout.write ("\n")
elif paragraph["type"] == "text":
text = paragraph["data"]
lines = textwrap.wrap (text, wrap_width)
text = ""
for line in lines:
text += line + "\n"
sys.stdout.write (text)
elif paragraph["type"] == "code":
text = paragraph["data"]
sys.stdout.write (text)
elif paragraph["type"] == "contents":
sys.stdout.write ("Not present in the .txt output.")
sys.stdout.write ("\n")
sys.stdout.write ("\n")
def add_title (text):
global title
title = text
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 (text):
lines = text.splitlines()
list = []
item = None
for line in lines:
if line[0] == "-":
if item:
list += [beautify(item)]
item = line[1:]
else:
item += line
if item:
list += [beautify(item)]
return list
def put_code (text):
lines = string.split (text, "\n")
text = ""
for line in lines:
if line[0:2] == "+ " or line[0:2] == "++":
line = line[2:]
text += line + "\n"
return text
def put_paragraph (text):
text = beautify (text)
return text
def add_text (text):
text = string.lstrip (text, "\n ")
paragraphs = []
paragraph_start = 0
done = 0
while not done:
paragraph_end = string.find (text, "\n\n", paragraph_start)
if paragraph_end == -1:
done = 1
paragraph = text[paragraph_start:]
else:
paragraph = text[paragraph_start:paragraph_end]
paragraph_start = paragraph_end + 2
if len(paragraph):
if paragraph == "+contents":
paragraphs += [{"type": "contents", "data": 0}]
elif paragraph[:5] == "+img ":
paragraphs += [{"type": "img", "data": paragraph[5:]}]
elif paragraph[0:2] == "+ " or paragraph[0:2] == "++":
paragraphs += [{"type": "code", "data": put_code(paragraph)}]
elif paragraph[0] == "-":
paragraphs += [{"type": "list", "data": put_list(paragraph)}]
else:
paragraphs += [{"type": "text", "data": put_paragraph(paragraph)}]
return paragraphs
def add_section(header, text):
global what
global wrap_width
global sections
global section_number
level = string.count(header, "=")
header = beautify(header[level:])
text = add_text(text)
section = {"level":level, "header":header, "text":text, "number":section_number}
sections += [section]
section_number += 1
return ""
main ()