root/misc/comment2html.py

Revision 1222, 3.9 kB (checked in by alpt, 1 year ago)

done

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env python
2 #
3 # comment2html.py
4 # http://freaknet.org/alpt/src/utils/comment2html.py
5 # https://dev.hinezumi.org/svnroot/utils/comment2html.py
6 #
7 # Given a list of source files it prints on stdout a html file describing each
8 # file. The description is taken from the first comment found in the file.
9 # If a directory is given, its description is taken from dir/README or dir/readme.
10 #
11 # Usage: comment2html.py *.c *.py > file.html
12 # Usage2: comment2html.py > file.html
13 #
14 # Tested even with python 2.1.3
15 #
16 # Tue Oct  2 02:37:47 CEST 2007
17 # - AlpT (@freaknet.org)
18 #
19
20 import sys
21 import os
22
23 def getfirstcomment(filename):
24         """Prints the first comment found in the file specified in $1"""
25         try:
26                 f=open(filename)
27         except IOError, e:
28                 print >>sys.stderr, e
29                 return
30
31         def dowhile(f):
32                 a=f.readline()
33                 b=a.strip()
34                 return a, b
35
36         lr, ls = dowhile(f)
37         l=ls
38         while not ls and lr:
39                 l=ls
40                 lr, ls = dowhile(f)
41
42         ret=[]
43         dclose=''
44         if not len(l): return ret
45         if (l[0] == '#' or l[0] == '%' or l[0] == "\""):
46                 d=l[0]
47         elif l[:2] == '/*':
48                 d='*'
49                 dclose='*/'
50         elif l[:2] == '//':
51                 d='//'
52         elif l[:3] == "\'\'\'" or l[:3] == "\"\"\"":
53                 d=''
54                 dclose=l[:3]
55         else:
56                 ret=[l]
57                 i=1
58                 while i != 4 and l:
59                         l=f.readline()
60                         if not l: break
61                         i+=1
62                         ret.append(l)
63                 return ret
64
65         for l in f.xreadlines():
66                 if not l: break
67                 l=l.strip()
68                 if dclose and l.find(dclose) != -1:
69                         break
70                 if d == l[:len(d)]:
71                         ret.append(l[len(d):]+'\n')
72                 elif (not dclose and l):
73                         break
74         f.close()
75         return ret
76
77 def print_list(flist):
78         ret=[]
79         ret.append('<a href="..">..</a>')
80         ret.append('<pre>')
81         for f in ["readme", "README"]:
82                 if os.path.isfile(f):
83                         ret.append(open(f).read())
84                         break
85         ret.append('</pre>')
86         for l in flist:
87                 r=0
88                 ret.append("\n<h2><a href=\""+l+"\">"+l+"</a></h2>")
89                 ret.append('<pre>')
90                 if os.path.isdir(l):
91                         for f in ["readme", "README"]:
92                                 f=l+"/"+f
93                                 if os.path.isfile(f):
94                                         c=open(f).readlines()
95                                         ret+=c[:16]
96                                         if len(c) > 16:
97                                                 ret.append("<a href=\""+f+"\">[...]</a>")
98                                         r=1
99                                         break
100                         if not r:
101                                 ret.append('</pre>')
102                                 continue
103                 elif "readme" == l or "README" == l:
104                         pass
105                 else:
106                         print >>sys.stderr, "parsing:", l
107                         c=getfirstcomment(l)
108                         ret+=c[:16]
109                         if len(c) > 16:
110                                 ret.append("<a href=\""+l+"\">[...]</a>")
111                 ret.append('</pre>')
112
113         return ret
114
115 if len(sys.argv) == 1:
116         def no_backups_nor_hidden(s): return not ('~' == s[-1] or '.' == s[0])
117         list=filter(no_backups_nor_hidden, os.listdir('.'))
118         list.sort()
119 else:
120         list=sys.argv[1:]
121
122 print """<html>
123 <body>"""
124 print ''.join(print_list(list))
125 print """<br>--<br><font size='-1'>
126 Generated by <a
127 href="http://freaknet.org/alpt/src/utils/comment2html.sh">comment2html.sh</a>
128 </font>
129 </body>
130 </html>"""
Note: See TracBrowser for help on using the browser.