#!/usr/bin/env python
# 
# comment2html.py
# http://freaknet.org/alpt/src/utils/comment2html.py
# https://dev.hinezumi.org/svnroot/utils/comment2html.py
#
# Given a list of source files it prints on stdout a html file describing each
# file. The description is taken from the first comment found in the file.
# If a directory is given, its description is taken from dir/README or dir/readme.
#
# Usage: comment2html.py *.c *.py > file.html
# Usage2: comment2html.py > file.html
#
# Tested even with python 2.1.3
#
# Tue Oct  2 02:37:47 CEST 2007
# - AlpT (@freaknet.org)
#

import sys
import os

def getfirstcomment(filename):
        """Prints the first comment found in the file specified in $1"""
        try:
                f=open(filename)
        except IOError, e:
                print >>sys.stderr, e
                return

        def dowhile(f):
                a=f.readline()
                b=a.strip()
                return a, b

        lr, ls = dowhile(f)
        l=ls
        while not ls and lr:
                l=ls
                lr, ls = dowhile(f)

        ret=[]
        dclose=''
        if not len(l): return ret
        if (l[0] == '#' or l[0] == '%' or l[0] == "\""):
                d=l[0]
        elif l[:2] == '/*':
                d='*'
                dclose='*/'
        elif l[:2] == '//':
                d='//'
        elif l[:3] == "\'\'\'" or l[:3] == "\"\"\"":
                d=''
                dclose=l[:3]
        else:
                ret=[l]
                i=1
                while i != 4 and l:
                        l=f.readline()
                        if not l: break
                        i+=1
                        ret.append(l)
                return ret

        for l in f.xreadlines():
                if not l: break
                l=l.strip()
                if dclose and l.find(dclose) != -1:
                        break
                if d == l[:len(d)]:
                        ret.append(l[len(d):]+'\n')
                elif (not dclose and l):
                        break
        f.close()
        return ret

def print_list(flist):
        ret=[]
	ret.append('<a href="..">..</a>')
	ret.append('<pre>')
        for f in ["readme", "README"]:
                if os.path.isfile(f):
                        ret.append(open(f).read())
                        break
	ret.append('</pre>')
        for l in flist:
                r=0
		ret.append("\n<h2><a href=\""+l+"\">"+l+"</a></h2>")
		ret.append('<pre>')
                if os.path.isdir(l):
                        for f in ["readme", "README"]:
                                f=l+"/"+f
                                if os.path.isfile(f):
                                        c=open(f).readlines()
                                        ret+=c[:16]
                                        if len(c) > 16:
                                                ret.append("<a href=\""+f+"\">[...]</a>")
                                        r=1
                                        break
                        if not r:
                                ret.append('</pre>')
			        continue
                elif "readme" == l or "README" == l:
                        pass
                else:
                        print >>sys.stderr, "parsing:", l
                        c=getfirstcomment(l)
                        ret+=c[:16]
                        if len(c) > 16:
                                ret.append("<a href=\""+l+"\">[...]</a>")
		ret.append('</pre>')

        return ret

if len(sys.argv) == 1:
        def no_backups_nor_hidden(s): return not ('~' == s[-1] or '.' == s[0])
        list=filter(no_backups_nor_hidden, os.listdir('.'))
        list.sort()
else:
        list=sys.argv[1:]

print """<html>
<body>"""
print ''.join(print_list(list))
print """<br>--<br><font size='-1'>
Generated by <a
href="http://freaknet.org/alpt/src/utils/comment2html.sh">comment2html.sh</a>
</font>
</body>
</html>"""
