############################################################## # cpp2html.awk # # typical usage: # awk -f cpp2html.awk -v TITLE="foo.cpp" \ # -v PREFACE="`cat HTMLHEAD`" \ # -v HTMLPATH="boost=." \ # < foo.cpp > foo.cpp.html # # good: # - makes any local include statement such as # #include "foo.hpp" # a hyperlink to foo.hpp.html # - allows to specify paths for system header files # to create links the same way # SYNTAX: -v HTMLPATH=,,... # where path is: = # For example: # -v HTMLPATH="/usr/include,boost=." # makes for # a link to /usr/include/foo.hpp.html # a link to ./foo.hpp.html # - gives comments a different color and font # # bad: # - can't handle tabulators properly # (expand tabulators to characters first) # - can't process @ in code properly # - can't process nested comments properly # - can't process comment characters inside strings properly # # This script is written by Nicolai M. Josuttis # http://www.josuttis.com/cppcode # # Version 1.4, 18 Mar, 2012 # 1.4: lower case html tags # 1.3: bugfix for path in include path # # Copyright 1999-2012 by Nicolai M. Josuttis. # All rights reserved. # # Permission to use, copy, modify and distribute this software is hereby # granted without fee, provided that this copyright notice appears in # all copies and that this copyright notice appears in supporting # documentation, and that the name of the author is not used in advertising # or publicity pertaining to distribution of the software without specific, # written prior permission. # The author makes no representations about the suitability of this software # for any purpose. It is provided "as is" without express or implied warranty. # ############################################################## ############################################################## # BEGIN # - initial settings and # - initial HTML output # before processing each line ############################################################## BEGIN { # comment settings # COMCOL: color entry for a comment # COMBEG: beginning of comment (starts comment style) # COMEND: end of a comment (end comment style) COMCOL = " color=\"0000FF\" " COMBEG = "" COMEND = "" # as style sheets don't work properly with Netscape yet #COMBEG = "" #COMEND = "" # currently we are no in a comment incomment = 0 # print heading # - title could be passed to awk with option "-v TITLE=..." # - STYLE SETTINGS print "" print "" if (TITLE != "") { print "" TITLE "" } # as style sheets don't work properly with Netscape yet #print "" # start body (black text on white background) print "" print "" print "" print " " # print grey title banner if title passed if (TITLE != "") { print "" print "
" print "" print " " TITLE print "" print "

" } print "" # insert additional HTML preface if passed with "-v PREFACE=..." if (PREFACE != "") { print PREFACE print "" } # start code (use source style) print "

" print "" print "" #} ############################################################## # BEGIN: handling HTMLPATH # - note: only gawk understands more than one BEGIN clause ############################################################## #BEGIN { # handling HTMLPATH option HTMLNUM = 0 if (HTMLPATH != "") { # split into array of settings HTMLNUM = split (HTMLPATH, HTMLARRAY, ",") for (i=1; i<=HTMLNUM; i++) { n = split (HTMLARRAY[i], tmp, "=") if (n == 1) { # global setting CODEPATH[i] = "" BROWPATH[i] = HTMLARRAY[i] } else { CODEPATH[i] = tmp[1] BROWPATH[i] = tmp[2] } #print "CODEPATH[" i "]: " CODEPATH[i] > "/dev/stderr" #print "BROWPATH[" i "]: " BROWPATH[i] > "/dev/stderr" } } } ############################################################## # print warnings ############################################################## /\t/ { print "WARNING: line " NR " contains a tabulator (please expand first)" \ > "/dev/stderr" } /@/ { print "WARNING: line " NR " contains a @ character (converted to space)" \ > "/dev/stderr" } ############################################################## # process each code line # - note: @ will internally be used as spaces # thus any @ in the code will get converted to a space ############################################################## { #process line as "line" line = $0 # replace spaces by @ gsub (" ","@",line) # replace < and > by appropriate statements gsub ("<", "\\<", line) gsub (">", "\\>", line) if (incomment) { # we are in a C++ comment # if comment ends with */ # then # process anything between leading blanks and */ as comment # and leave comment # else # process anything between leading blanks and end of line as comment # fi if (line ~ /\*\//) { line = gensub ("(@*)(.*)\\*/", "\\1" COMBEG "\\2*/" COMEND, "g",line) incomment = 0; } else { line = gensub ("(@*)(.*)$","\\1" COMBEG "\\2" COMEND, "g", line) } } else { # we are in a C++ comment # process anything between // and end of line as comment line = gensub ("//(.*)$", COMBEG "//\\1" COMEND, "g",line) if (line ~ /\/\*/) { # if comment starts with with /* and ends with */ # then # process between /* and */ as comment # if comment starts with with /* and doesn't end end with */ # then # process anything between /* and end of line as comment # and turn incomment flag on # fi if (line ~ /\/\*.*\*\//) { line = gensub ("/\\*(.*)\\*/", \ COMBEG "/*\\1*/" COMEND, "g",line) incomment = 0 } else { line = gensub ("/\\*(.*)$", "/" \ COMBEG "*\\1" COMEND, "g",line) incomment = 1 } } } # replace each @ by explicit space gsub ("@","\\ ",line) } ############################################################## # Handling HTMLPATH # make #include a hyperlink to foo.hpp.html # in a directory specified in HTMLPATH ############################################################## line ~ /#.*include.*<.*>/ { if (line ~ /href/) { #print "schon einmal ersetzt: " line > "/dev/stderr" } else if (HTMLPATH != "") { # extract full file name file = substr(line,index(line,"<")+4,\ index(line,">")-index(line,"<")-4) #print file > "/dev/stderr" # split file name into basename and filename n = split (file, tokens, "/") basename = "" if (n > 1) { basename = tokens[1] for (i=2; i "/dev/stderr" #print filename > "/dev/stderr" # create browser path for each source path passed as option HTMLPATH for (i=1; i<=HTMLNUM; i++) { if (CODEPATH[i] == file) { line = gensub ("<(.*)>", \ "\\<\\1\\>", "1", line) print "create link to " BROWPATH[i] \ " for header <" file ">" > "/dev/stderr" break # THE LOOP #print line > "/dev/stderr" } if (CODEPATH[i] == basename) { line = gensub ("<(.*)>", \ "\\<\\1\\>", "1", line) print "create link to " BROWPATH[i]"/"filename".html" \ " for header <" file ">" > "/dev/stderr" break # THE LOOP #print line > "/dev/stderr" } } } } ############################################################## # make #include "foo.hpp" a hyperlink to foo.hpp.html # - in each line that contains include and a string ending with # something like .h, .hpp, .cpp, .HPP, or so # make string contents a hyperlink to string contents with # .html appended ############################################################## line ~ /#.*include.*".*[.][cChHpP]*"/ { if (line ~ /href/) { #print "schon einmal ersetzt: " line > "/dev/stderr" } else { split (line, tokens, "\"") line = gensub ("\"([a-zA-Z]+[a-zA-Z0-9/]*[.][cChHpP]*)\"", \ "\"\\1\"", "1", line) print "create link to " tokens[2] ".html for header \"" tokens[2] "\"" \ > "/dev/stderr" } } ############################################################## # end of line processing, so print it ############################################################## { print line "
" } ############################################################## # final HTML output ############################################################## END { print "
" print "
" print "" print "" }