Code Files

dir2html.awk

Generated on Tue Dec 05 17:39:05 Eastern Standard Time 2006 from dir2html.awk


# Program   : DIR2HTML.AWK
# Purpose   : Convert a directory listing to HTML output with hyperlinks
# Date      : 29 January 2006
# Author    : Bob Jonkman

# Usage     : gawk -f DIR2HTML.AWK -f LIBRARY.AWK [-v NOHTTP=(1|0)]

function remainder(indexstring,contentstring)
    {   return(substr(contentstring,(index(contentstring,indexstring))))
    }


BEGIN	{	QUOTE = "\"" ;
    		FIELD_SEPARATOR = "," ;
	    	FIELD_DELIMITER = "\"" ;
		    RECORD_SEPARATOR = "\n";

        printhtmlhead(txt2html(ARGV[1]) " - Dir2HTML.AWK")

        dirpath = "dir " ARGV[1]

        print("<!-- ")
        print("dirpath= " dirpath)
        for(i=0;i<=ARGC;i++)
            print(i,ARGV[i])
        print("-->")


# This pipes a directory into a temporary file
        tempfile = ENVIRON["TEMP"] "\\dir.txt"
        while(dirpath | getline)
            print($0) > tempfile

        close(tempfile)

# This forces the temporary file to be the only command line file
        ARGV[1] = tempfile
        ARGC    = 2

# Testing....
        print("<!-- ")
        print("tempfile= " tempfile)
        for(i=0;i<=ARGC;i++)
            print(i,ARGV[i])
        print("-->")



}


# Read two header lines


(FNR == 1)  {   getline     # First line is blank
                line1=$0    # "Volume in drive..."
                getline
                line2=$0    # "Volume Serial Number is..."
              # getline     # a blank line
                getline     # "Directory of..."

                path = remainder($3,$0)

                if (substr(path,length(path)) == "\\")  # Lop off trailing backslash
                    path = substr(path,1,length(path)-1)

                print("<body>")
                print("<h1>" txt2html($0) "</h1>")
#                print(line1 "<br />")
#                print(line2)

                print("<table border=1 summary=" QUOTE txt2html($0) QUOTE ">")

                print(" <thead>")
                print("  <tr>")
                print("   <td colspan=" QUOTE "4" QUOTE ">" line1 "</td>")
                print("  </tr>")

                print("  <tr>")
                print("   <td colspan=" QUOTE "4" QUOTE ">" line2 "</td>")

#                print("   <td>Date</td>")
#                print("   <td>Time</td>")
#                print("   <td>Size</td>")
#                print("   <td>Name</td>")


                print("  </tr>")
                print(" </thead>")
                print(" <tbody>")
            
                getline         # a blank line
}


/[Ff]ile[(]s/   {  # getline
                print(" </tbody>")
                print(" <tfoot>")
                print("  <tr>")
                print("   <td colspan=" QUOTE "2" QUOTE ">" txt2html($1 " " $2) "</td>")
                print("   <td colspan=" QUOTE "2" QUOTE ">" txt2html($3 " " $4) "</td>")
                print("  </tr>")

                getline

                print("  <tr>")
                print("   <td colspan=" QUOTE "2" QUOTE ">" txt2html($1 " " $2) "</td>")
                print("   <td colspan=" QUOTE "2" QUOTE ">" txt2html($3 " " $4 " " $5) "</td>")
                print("  </tr>")
                print(" </tfoot>")

                print("</table>")

                nextfile

}

(FNR !=1)   {          
                # getline
                if ($0 == "")
                    next # Skip the headers and blank lines

                print("  <tr>")
                print("   <td>" txt2html($1) "</td>")
                print("   <td>" txt2html($2) "</td>")
                if($3 == "<DIR>")
                {
                    print("   <td><a href=" QUOTE "/cgi-bin/awk/dir2html.awk?" txt2uri(path "\\" remainder($4,$0)) QUOTE ">" txt2html($3) "</a></td>")
                    print("   <td>" txt2html(remainder($4,$0)) "</td>")
                }
                else
                {
                    print("   <td>" txt2html($3) "</td>")
#                    print("   <td>" txt2html(remainder($4,$0)) "</td>")
                    print("   <td><a href=" QUOTE txt2uri(path "\\" remainder($4,$0)) QUOTE ">" txt2html(remainder($4,$0)) "</a></td>")
                }
                print("  </tr>")

}


END {   
        print("</body>")
        print("</html>")
}

# EOF:  DIR2HTML.AWK

   

1 files processed.