Hash :
ecbfb5fa
Author :
Date :
2000-01-10T15:49:01
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
#!/usr/bin/env python
#
# DocMaker is a very simple program used to generate HTML documentation
# from the source files of the FreeType packages.
#
import fileinput, sys, string
# This function is used to parse a source file, and extract comment blocks
# from it. The following comment block formats are recognized :
#
# /**************************
# *
# * FORMAT1
# *
# *
# *
# *
# *************************/
#
# /**************************/
# /* */
# /* FORMAT2 */
# /* */
# /* */
# /* */
# /* */
#
# /**************************/
# /* */
# /* FORMAT3 */
# /* */
# /* */
# /* */
# /* */
# /**************************/
#
# Each block is modeled as a simple list of text strings
# The function returns a list of blocks, i.e. a list of strings lists
#
def make_block_list():
"""parse a file and extract comments blocks from it"""
list = []
block = []
format = 0
for line in fileinput.input():
line = string.strip(line)
l = len(line)
if format == 0:
if l > 3 and line[0:3] == '/**':
i = 3
while (i < l) and (line[i] == '*'):
i = i+1
if i == l:
# this is '/**' followed by any number of '*', the
# beginning of a Format 1 block
#
block = [];
format = 1;
elif (i == l-1) and (line[i] == '/'):
# this is '/**' followed by any number of '*', followed
# by a '/', i.e. the beginning of a Format 2 or 3 block
#
block = [];
format = 2;
##############################################################
#
# FORMAT 1
#
elif format == 1:
# if the line doesn't begin with a "*", something went
# wrong, and we must exit, and forget the current block..
if (l == 0) or (line[0] != '*'):
block = []
format = 0
# otherwise, we test for an end of block, which is an
# arbitrary number of '*', followed by '/'
else:
i = 1
while (i < l) and (line[i] == '*'):
i = i+1
# test for the end of the block
if (i < l) and (line[i] == '/'):
if block != []: list.append( block )
format = 0
block = []
else:
block.append( line[1:] )
##############################################################
#
# FORMAT 2
#
elif format == 2:
# if the line doesn't begin with '/*' and end with '*/',
# this is the end of the format 2 format..
if (l < 4 ) or (line[:2] != '/*') or (line[-2:] != '*/'):
if block != []: list.append(block)
block = []
format = 0
continue
# remove the start and end comment delimiters, then right-strip
# the line
line = string.rstrip(line[2:-2])
# check for end of a format2 block, i.e. a run of '*'
if string.count(line,'*') == l-4:
if block != []: list.append(block)
block = []
format = 0
else:
# otherwise, add the line to the current block
block.append(line)
return list
# This function is only used for debugging
#
def dump_block_list( list ):
"""dump a comment block list"""
for block in list:
print "----------------------------------------"
for line in block:
print line
print "---------the end-----------------------"
######################################################################################
#
#
# The DocParagraph is used to store either simple text paragraph or
# source code lines
#
#
# If the paragraph contains source code (use code=1 when initializing the
# object), self.lines is a list of source code strings
#
# Otherwise, self.lines is simply a list of words for the paragraph
#
class DocParagraph:
def __init__(self,code=0,margin=0):
self.lines = []
self.code = code
self.margin = margin
def add(self,line):
if self.code==0:
# get rid of unwanted spaces in the paragraph
self.lines.extend( string.split(line) )
else:
# remove margin whitespace
if string.strip( line[:self.margin] ) == "": line = line[self.margin:]
self.lines.append(line)
def dump(self):
max_width = 50
if self.code == 0:
cursor = 0
line = ""
for word in self.lines:
if cursor+len(word)+1 > max_width:
print line
cursor = 0
line = ""
line = line + word + " "
cursor = cursor + len(word) + 1
if cursor > 0:
print line
else:
for line in self.lines:
print "--" + line
print ""
######################################################################################
#
#
# The DocContent class is used to store the content of a given marker
# Each DocContent can have its own text, plus a list of fields. Each field
# has its own text too
#
# Hence, the following lines :
#
# """
# Some arbitraty text:
#
# fieldone :: some arbitraty text for this field,
# note that the text stretches to several lines
#
# fieldtwo :: some other text
#
# """
#
# will be stored as (each text is a list of string:
#
# self.fields = [ "", "fieldone", "fieldtwo" ]
# self.texts = [
# [ "some arbitraty text for this field," ,
# "note that the text stretches to several lines" ],
#
# [ "some other text" ]
# ]
#
#
class DocContent:
def __init__(self, paragraph_lines=[]):
self.fields = []
self.texts = []
code_mode = 0
code_margin = 0
field = ""
text = []
paragraph = None
for aline in paragraph_lines:
if code_mode == 0:
line = string.lstrip(aline)
l = len(line)
margin = len(aline) - l
# if the line is empty, this is the end of the current
# paragraph
if line == "":
if paragraph:
text.append(paragraph)
paragraph = None
continue
# test for the beginning of a code block, i.e.'{' is the first
# and only character on the line..
#
if line == '{':
code_mode = 1
code_margin = margin
if paragraph:
text.append(paragraph)
paragraph = DocParagraph( 1, margin )
continue
words = string.split(line)
# test for a field delimiter on the start of the line, i.e.
# the oken `::'
#
if len(words) >= 2 and words[1] == "::":
if paragraph:
text.append(paragraph)
paragraph = None
self.fields.append(field)
self.texts.append(text)
field = words[0]
text = []
words = words[2:]
if len(words) > 0:
line = string.join(words)
if not paragraph:
paragraph = DocParagraph()
paragraph.add( line )
else:
line = aline
# the code block ends with a line that has a single '}' on it
if line == " "*code_margin+'}':
text.append(paragraph)
paragraph = None
code_mode = 0
code_margin = 0
# otherwise, add the line to the current paragraph
else:
paragraph.add(line)
if paragraph:
text.append(paragraph)
self.fields.append( field )
self.texts.append( text )
def dump(self):
for i in range(len(self.fields)):
field = self.fields[i]
if field: print "<field "+field+">"
for paras in self.texts[i]:
paras.dump()
if field: print "</field> "
######################################################################################
#
#
# The DocBlock class is used to store a given comment block. It contains
# a list of markers, as well as a list of contents for each marker.
#
#
class DocBlock:
def __init__(self, block_line_list=[]):
self.markers = []
self.contents = []
marker = ""
content = []
alphanum = string.letters + string.digits + "_"
for line in block_line_list:
line2 = string.lstrip(line)
l = len(line2)
margin = len(line) - l
if l > 3 and line2[0] == '<':
i = 1
while i < l and line2[i] in alphanum: i = i+1
if i < l and line2[i] == '>':
if marker or content:
self.add( marker, content )
marker = line2[1:i]
content = []
line2 = string.lstrip(line2[i+1:])
l = len(line2)
line = " "*margin + line2
content.append(line)
if marker or content:
self.add( marker, content )
def add( self, marker, lines ):
# remove the first and last empty lines from the content list
l = len(lines)
if l > 0:
i = 0
while l > 0 and string.strip(lines[l-1]) == "": l = l-1
while i < l and string.strip(lines[i]) == "": i = i+1
lines = lines[i:l]
l = len(lines)
# add a new marker only if its marker and its content list aren't empty
if l > 0 and marker:
self.markers.append(marker)
self.contents.append(lines)
def dump( self ):
for i in range( len(self.markers) ):
print "["+self.markers[i]+"]"
for line in self.contents[i]:
print "-- "+line
def doc_contents(self):
contents = []
for item in self.contents:
contents.append( DocContent(item) )
return contents
def dump_doc_blocks( block_list ):
for block in block_list:
docblock = DocBlock(block)
docblock.dump()
print "<<------------------->>"
#
#
#
def dump_single_content( block_list ):
block = block_list[0]
docblock = DocBlock(block)
print "<block>"
for i in range(len(docblock.markers)):
marker = docblock.markers[i]
contents = docblock.contents[i]
print "<marker "+marker+">"
doccontent = DocContent( contents )
doccontent.dump()
print "</marker>"
print "</block>"
def dump_doc_contents( block_list ):
for block in block_list:
docblock = DocBlock(block)
print "<block>"
for i in range(len(docblock.markers)):
print "<marker "+docblock.markers[i]+">"
content = DocContent( docblock.contents[i] )
content.dump()
print "</marker>"
print "</block>"
def main(argv):
"""main program loop"""
print "extracting comment blocks from sources .."
list = make_block_list()
# dump_block_list( list )
# dump_doc_blocks( list )
# print "dumping block contents .."
dump_doc_contents(list)
# dump_single_content(list)
# If called from the command line
if __name__=='__main__': main(sys.argv)