Boost logo

Boost :

From: Nicolai Josuttis (nicolai.josuttis_at_[hidden])
Date: 2000-01-04 14:02:42


Hi,

attached you can find an awk script that I use to
generate all my HTML pages of the book and boost examples.
I have cleaned it up so that you should be able to
read and use it easily.
Any feedback and improvement is welcome.

Typical usage:
 awk -f cpp2html.awk -v TITLE="foo.cpp" < foo.cpp > foo.cpp.html

See the (top of the) code mor emore details.

I have no problem if we install it at boost.
I will probably maintain it also on my
local website.

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" < foo.cpp > foo.cpp.html
#
# good:
# - makes any local include statement such as
# #include "foo.hpp"
# a hyperlink to foo.hpp.html
# - gives comment a different color and font
#
# bad:
# - 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.0, 04 Jan, 2000
#
# Copyright 1999 by and Nicolai M. Josuttis.
# All rights reserved.
#
# Permission to use, copy, modify and distribute this software for personal
# and educational use is hereby granted without fee, provided that the above
# copyright notice appears in all copies and that both that copyright notice
# and this permission notice appear in supporting documentation, and that the
# names of the author are 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: settings at the beginning of a comment
  # COMEND: settings at the end of a comment
  COMCOL = " color=\"0000FF\" "
  COMBEG = "<I><FONT face=\"Arial,Helvetica\"" COMCOL "size=-1>"
  COMEND = "</I></FONT>"

  # currently we are no in a comment
  incomment = 0

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

  # start body (black text on white background)
  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 in teletype
  print "<BR><BR><TT>"
}

##############################################################
# 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]*"/ {
    line = gensub ("\"([a-zA-Z]+[a-zA-Z0-9]*[.][cChHpP]*)\"", \
                    "\"<A href=\"\\1.html\">\\1</A>\"", "1", line)
}

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

##############################################################
# final HTML output
##############################################################
END {
  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