Boost logo

Boost :

From: Nicolai Josuttis (nicolai.josuttis_at_[hidden])
Date: 2000-01-06 07:43:15


Attached is a new version of cpp2html.

Is has the following improvements:
 - comment in regular font size
 - warnings if source contains @ or tabulators
 - able to create links for system header files (e.g. handling <boost/foo.hpp>)
 - right to use it for everybody
 - no use of style sheets (see separate mail)

The system header file handling is done in the following way:
 - By passing -v HTMLPATH="boost=."
   you set the path for system header files having path boost
   to the local directory. Thus
        #include <boost/foo.hpp>
   gets a link to
        ./foo.hpp.html
 - In general, you can pass a list of such settings separated by commas
 - if no leading path and = is passed a setting for files without a path is made
 - Thus
        -v HTMLPATH="/usr/include,boost=."
   lets
        <foo.hpp> get a link to /usr/include/foo.hpp.html
   and
        <boost/foo.hpp> get a link to ./boost.hpp.html
Cool, isn't it?

As alternative I was thinking about a full qualified mapping,
which allows even more individual mappings.
Something like
  INCPATH="algorithm=/usr/local/include/algorithm,boost/foo.hpp=./foo.hpp.html"
This would also allow to create links directly into source files if no
HTML version is available. But that might be something for version 1.3 ;-)

Feedback is welcome!
Best

-- 
Nicolai M. Josuttis          	http://www.josuttis.de/
Solutions in Time        	mailto:solutions_at_[hidden]

##############################################################
# 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=<path>,<path>,<path>...
# where path is: <codepath>=<browserpath>
# For example:
# -v HTMLPATH="/usr/include,boost=."
# makes for
# <foo.hpp> a link to /usr/include/foo.hpp.html
# <boost/foo.hpp> 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
# mailto:solutions_at_[hidden]
#
# Version 1.1, 06 Jan, 2000
#
# Copyright 1999,2000 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 = "<I><FONT face=\"Arial,Helvetica,sans-serif\"" COMCOL ">"
  COMEND = "</FONT></I>"

  # as style sheets don't work properly with Netscape yet
  #COMBEG = "<SPAN class=\"Comment\">"
  #COMEND = "</SPAN>"

  # currently we are no in a comment
  incomment = 0

  # print heading
  # - title could be passed to awk with option "-v TITLE=..."
  # - STYLE SETTINGS
  print "<HTML>"
  print "<HEAD>"
  if (TITLE != "") {
      print "<TITLE>" TITLE "</TITLE>"
  }

  # as style sheets don't work properly with Netscape yet
  #print "<STYLE type=\"text/css\">"
  #print " SPAN.Source {"
  #print " font-family: monospace;"
  #print " }"
  #print " SPAN.Comment {"
  #print " font-family: sans-serif;"
  #print " font-style: italic;"
  #print " color: blue;"
  #print " }"
  #print "</STYLE>"

  # start body (black text on white background)
  print "</HEAD>"
  print ""
  print "<BODY TEXT=\"#000000\" BGCOLOR=\"#FFFFFF\">"
  print "&nbsp;"

  # print grey title banner if title passed
  if (TITLE != "") {
      print "<TABLE HEIGHT=40 WIDTH=\"100%\">"
      print "<TR> <TD ALIGN=LEFT WIDTH=\"100%\" BGCOLOR=\"#DDDDDD\">"
      print "<FONT face=\"Arial,Helvetica\" size=+2><B>"
      print "&nbsp;" TITLE
      print "</B></FONT>"
      print "</TD></TR></TABLE><BR>"
  }
  print ""

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

  # start code (use source style)
  print "<BR><BR>"
  print "<TT>"
  print "<SPAN class=\"Source\">"
}

##############################################################
# BEGIN: handling HTMLPATH
##############################################################
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 ("<", "\\&lt;", line)
    gsub (">", "\\&gt;", 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 ("/\\*(.*)$", "<Font" COMCOL ">/</FONT>" \
                                                COMBEG "*\\1" COMEND, "g",line)
                incomment = 1
            }
        }
    }

    # replace each @ by explicit space
    gsub ("@","\\&nbsp;",line)
}

##############################################################
# 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]*"/ {
    split (line, tokens, "\"")
    line = gensub ("\"([a-zA-Z]+[a-zA-Z0-9]*[.][cChHpP]*)\"", \
                    "\"<A href=\"\\1.html\">\\1</A>\"", "1", line)
    print "create link to " tokens[2] ".html for header \"" tokens[2] "\"" \
> "/dev/stderr"
}

##############################################################
# Handlgin HTMLPATH
# make #include <path/foo.hpp> a hyperlink to foo.hpp.html
# in a directory specified in HTMLPATH
##############################################################
line ~ /#.*include.*&lt;.*&gt;/ {
    if (HTMLPATH != "") {
        # extract full file name
        file = substr(line,index(line,"&lt;")+4,\
                           index(line,"&gt;")-index(line,"&lt;")-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<n; i++) {
                basename = basename "/" tokens[i]
            }
        }
        filename = tokens[n]
        #print basename > "/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] == basename) {
                line = gensub ("&lt;(.*)&gt;", \
                               "\\&lt;<A href=\"" BROWPATH[i] \
                               "/"filename".html\">\\1</A>\\&gt;", "1", line)
                print "create link to " BROWPATH[i]"/"filename".html" \
                      " for header <" file ">" > "/dev/stderr"
                #print line > "/dev/stderr"
            }
        }
    }
}

##############################################################
# end of line processing, so print it
##############################################################
{
    print line "<BR>"
}

##############################################################
# final HTML output
##############################################################
END {
  print "</SPAN>"
  print "</TT>"
  print "</BODY>"
  print "</HTML>"
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk