Code Files

code2html.awk

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


# Program    : CODE2HTML.AWK
# Purpose    : Convert various program and code files to an HTML page with <code> constructs
# Date       : 13 October 2006
# Author     : Bob Jonkman

# Usage      : gawk -f CODE2HTML.AWK -f LIBRARY.AWK [-v TITLE=title] [-v LINK=(0|1)] 1*(filename.csv)
#               http:\\hostname\code2html.awk?FILENAME=filename.ext[1*(|filename.ext)][&TITLE=title][&LINK=(0|1)]
# Note       : Use a literal | to separate filenames in URI

# Variables  :   TITLE = HTML <title> and <h1> elements for the page
#                LINK  = Creates link to FILENAME for each table
#                NOHTTP    = Suppress  a "Content-type: text/html" line for dynamic pages


function makemenu(topofpage)    {
    # Create header/footer menu
    # parameter==TRUE then include "Top of page" link
            print("   <div class=" QUOTE "menu" QUOTE ">") 
            print("    <p>In this page:</p>") ;
            print("    <ul>" )

            if (topofpage)  
                print("     <li>[<a href=" QUOTE "#top-of-page" QUOTE " class=" QUOTE "internal" QUOTE ">Top of this page</a>] </li>")

            for(i=1; i<ARGC; i++)
            {
                print("     <li>[<a class=" QUOTE "internal" QUOTE " href=" QUOTE "#table" i QUOTE ">" txt2html(ARGV[i]) "</a>] </li>") 
            }
            print("   </ul>")
            print("  </div> <!-- end of menu  -->")
}




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

            parsecgi(ENVIRON["QUERY_STRING"],qstring)

# print("<!--                         ##### DEBUG #####")
# print("ARGC= " ARGC)                ##### DEBUG #####
# for(i in ARGV)                      ##### DEBUG #####
#     print("ARGV[" i "]= " ARGV[i])  ##### DEBUG #####
# print("-->")                        ##### DEBUG #####


            if(ARGC == 1)   # no files on the command line
            {
                if(qstring["FILENAME"])
                {
                    ARGC = 1 + split(qstring["FILENAME"],ARGV,"|")
                }
                NOHTTP = 0      # if there are filenames in qstring["FILENAME"] then always emit an HTTP style header
            }
            else
                NOHTTP = 1  # if there are filenames on the command line then do not emit an HTTP style header

            if(!TITLE)
                TITLE = qstring["TITLE"]

            if(!TITLE)          # if there is still no title then...
                TITLE = "Code Files"

            if(!LINK)
                LINK = qstring["LINK"]


        printhtmlhead(TITLE)

        filenum = 0 


        print(" <body><a name=" QUOTE "top-of-page" QUOTE " />")
        print("  <h1>" makehtml(TITLE) "</h1>")

        if (ARGC == 1)
        {   print("  <div class=" QUOTE "content" QUOTE ">")
            print("   <p>No files specified, nothing to do!</p>")

#            print("<!-- ##### DEBUG ##### ARGC= " ARGC)
#            for(i in ARGV)  ##### DEBUG #####
#                print("ARGV[" i "]= " ARGV[i])  ##### DEBUG ##### -->")

            exit    # this will process END{}
        }

        if (ARGC > 2)       # If there is more than one file on the page
            makemenu(0) ;   #   create header menu without "Top of page" link

        print("  <div class=" QUOTE "content" QUOTE ">")

    }

(FNR == 1)    {     
# print("<!-- ##### DEBUG ##### FILENAME= " FILENAME " (FILENAME != oldfilename) FILENAME= " FILENAME " oldfilename= " oldfilename " -->")

        if(filenum)        # check if we've processed multiple files
                {   print("   </code></pre>") 
                    print("") 
                }

        filenum++ 

        print("   <h2 id=" QUOTE "table" filenum QUOTE)
        if (filenum != 1)   #  Suppress page-break-before on the first file
            print("        style=" QUOTE "page-break-before: always ; " QUOTE )

        print("       >")

        if (ARGC > 2)   # Display "Prev, Top, Next" navigational aids
        {
            print("    <span class=" QUOTE "internal navaid" QUOTE ">")

            if (filenum != 1)       # suppress "Previous" for first file    
            {
                print("     <a href=" QUOTE "#table" filenum-1 QUOTE)
                print("        title=" QUOTE "Previous File: " txt2html(ARGV[filenum-1]) QUOTE)
                print("     >&lt;</a>")
            } else
            {   print("     <span class=" QUOTE "disabled" QUOTE ">&lt;</span>")
            }

            print("     <a href=" QUOTE "#top-of-page" QUOTE)
            print("        title=" QUOTE "Go to the top of this page" QUOTE)
            print("     >^</a>")

            if (filenum < ARGC-1 )    # suppress "Next" for last file
            {
                print("     <a href=" QUOTE "#table" filenum+1 QUOTE)
                print("        title=" QUOTE "Next File: " txt2html(ARGV[filenum+1]) QUOTE)
                print("     >&gt;</a>")
            } else
            {   print("     <span class=" QUOTE "disabled" QUOTE ">&gt;</span>")
            }
        print("    </span>")
        }
# print("<!-- ##### DEBUG #####  filenum=" filenum ", ARGC=" ARGC " -->")

        print("   " txt2html(FILENAME) "</h2>" )


        print("   <p>Generated on " strftime() " from ") 

        if(LINK)
            print("    <a href=" QUOTE txt2uri(FILENAME) QUOTE ">" txt2html(FILENAME) "</a>") 
        else
            print("    " txt2html(FILENAME))

        print("   </p>")
        print("") 
        if(PLOT)            
        {   print("   <img src=" QUOTE txt2uri(FILENAME) ".png" QUOTE )
            print("        alt=" QUOTE "Plot of " txt2html(FILENAME) QUOTE )
            print("        style=" QUOTE "float: right ; " QUOTE )
            print("        />") 
        }
        print("<pre><code>") 
}




# Main : process for every line in the file
        {   print(txt2html($0))
}
                

END {   if(filenum)    # Tables are ended only if files have been processed
        {    print("   </code></pre>") 
        }
        print("   <p>" filenum " files processed.</p>") 
        print("  </div> <!-- end of content -->") 

        if (ARGC > 2)       # If there is more than one file on the page
        {   print("  <div class=" QUOTE "footer" QUOTE ">")
            makemenu(1) ;   #   Create footer menu with "Top of page" link
            print("  </div> <!-- end of footer -->")
        }

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

# EOF: CODE2HTML.AWK

   

1 files processed.