home *** CD-ROM | disk | FTP | other *** search
/ HTML - Publishing on the Internet / html_cdrom.iso / tools / html / linux / check / makemenu.awk < prev    next >
Text File  |  1995-01-21  |  6KB  |  136 lines

  1. #makemenu.awk -- Makes simple menu for HTML files, based on each file's <title>,
  2. #             and can make simple table of contents based on <h1>-<h6> headings.
  3. #
  4. #Typical use:
  5. #
  6. # awk -f makemenu.awk [options] infiles.html > menu.html
  7. #
  8. #    Where command-line options have the form "option=value".  The possible
  9. # options are title="...", toc=1, and dirprefix="..." and should appear on the
  10. # command line _before_ the names of files to be processed.
  11. #
  12. #    The menu contains a list of <A HREF="file.html">....</A> elements: the
  13. # content of each of these menu items is taken from that of the
  14. # <TITLE>...</TITLE> element of the corresponding file.
  15. #
  16. #    The title="..." option specifies the title of the menu itself:
  17. #
  18. # awk -f makemenu.awk title="Menu for HTML files" *.html > menu.html
  19. #
  20. #    To make a menu of all the files in one's personal hierarchy under Unix:
  21. #
  22. # cd $HOME/public_html
  23. # awk -f makemenu.awk title="My Files" `find . -name \*.html -print` > menu.html
  24. #
  25. #    The toc=1 command-line option attempts to construct a table of contents
  26. # for each file as part of the menu, based on the <H1>-<H6> headings in the
  27. # file.  If there are links inside headings, then makemenu.awk will attempt to
  28. # preserve the validity of <A HREF="..."> references, and transform an
  29. # <A NAME="..."> into an <A HREF="..."> link to the heading from the menu file;
  30. # however, makemenu.awk is limited by the fact that it does not examine each
  31. # <A> tag in a heading individually, but only does global search-and-replace
  32. # operations on the whole <Hn>...</Hn> element (for this reason, the values
  33. # of <A HREF=> and <A NAME=> are only operated on if they are quoted).
  34. #
  35. #    A dirprefix="..."  option can also be specified on the command line; this
  36. # specifies a string which is prefixed to filenames, and which can be used to
  37. # convert local filesystem references (relative URL's) to absolute URL's.
  38. # An example:
  39. #
  40. # awk -f makemenu.awk dirprefix=http://myhost.edu/~myself/ *.html > menu.html
  41. #
  42. #    This program is rather simple-minded; if an HTML file does not have a
  43. # <TITLE>...</TITLE> element, it will not appear in the menu.  If the closing
  44. # </TITLE> tag is not present, it will try to stuff the whole remaining text of
  45. # the file into the menu.  The closing `>' character of the <TITLE>, </TITLE>,
  46. # <H1>-<H6> and </H1>-</H6> tags should not be on a different line from the
  47. # rest of the tag.  Also, multiple headings should not be contained on a single
  48. # line.  This is not an error-checking program, and illegal HTML input may
  49. # result in incorrect HTML output.
  50. #
  51. #    On some systems, non-archaic awk may actually be named ``nawk''.  The
  52. # ``gawk'' interpreter freely-available from the FSF GNU project is more robust
  53. # than some vendor-supplied awk/nawk interpreters.
  54. #
  55. # Copyright 1995 by H. Churchyard, churchh@uts.cc.utexas.edu -- freely
  56. # redistributable.
  57. #
  58. #  Version 1.0 12/94?? -- Was for my personal use only.
  59. #  Version 1.1 1/8/95 -- Made more general, added documentation comments.
  60. #  Version 1.2 1/12/95 -- Added heading-to-Table-of-Contents stuff.  Included
  61. # in htmlchek 4.0 release.
  62. #
  63. BEGIN{accum="";haccum=""}
  64. #
  65. {if (FNR==1)
  66.    {if (NR==1)
  67.       {if (!title) {title="Menu for HTML files"};
  68.        print "<html><head><title>" title "</title></head>";
  69.        print "<body><h1>" title "</h1><hr><ul>"}
  70.      else {if (toc) {liout()}};
  71.     hlevel=0}}
  72. #
  73. /<[Tt][Ii][Tt][Ll][Ee][^<>]*>/,/<\/[Tt][Ii][Tt][Ll][Ee][^<>]*>/{
  74.  line=$0;
  75.  sub(/^.*<[Tt][Ii][Tt][Ll][Ee][^<>]*>/,"",line);
  76.  x=sub(/<\/[Tt][Ii][Tt][Ll][Ee][^<>]*>.*$/,"",line);
  77.  accum=(accum " " line);
  78.  if (x) {
  79.    if (toc) {liout()};
  80.    fn=FILENAME;sub(/^\.\//,"",fn);
  81.    sub(/^  */,"",accum);sub(/  *$/,"",accum);
  82.    print " <LI><A HREF=\042" dirprefix fn "\042>" accum "</A> <tt>(" fn ")</tt>";
  83.    accum="";}}
  84. #
  85. /<[Hh][1-6][^<>]*>/,/<\/[Hh][1-6][^<>]*>/{
  86.  if (toc)
  87.    {if (match($0,/<[Hh][1-6]/)!=0)
  88.       {newhlevel=substr($0,(RSTART+2),1);
  89.        if (newhlevel>hlevel)
  90.          {printf "%" ((newhlevel*2)+1) "s","";
  91.           for (i=(newhlevel-hlevel);i>=1;--i) {printf "<UL>"};
  92.           printf "\n"}
  93.         else {if (newhlevel<hlevel)
  94.                 {printf "%" ((hlevel*2)+1) "s","";
  95.                  for (i=(hlevel-newhlevel);i>=1;--i) {printf "</UL>"};
  96.                  printf "\n"}};
  97.        hlevel=newhlevel};
  98.     line=$0;
  99.     sub(/^.*<[Hh][1-6][^<>]*>/,"",line);
  100.     x=sub(/<\/[Hh][1-6][^<>]*>.*$/,"",line);
  101.     haccum=(haccum " " line);
  102.     if (x)
  103.       {fn=FILENAME;sub(/^\.\//,"",fn);
  104.        sub(/^  */,"",haccum);sub(/  *$/,"",haccum);
  105.        # The following code attempts to preserve the validity of HREF's,
  106.        # and transform <A NAME>'s into HREF's where possible, but it's kind
  107.        # of lame because it doesn't examine each <A> tag individually.
  108.        if (haccum~/<[Aa]/) {
  109.        gsub(/[ \t]*=[ \t]*\042/,"=\042",haccum);
  110.        z=gsub(/[Hh][Rr][Ee][Ff]=\042\043/,("HREF=\042" dirprefix fn "\043"),haccum);
  111.        if ((!z)&&(haccum!~/[Hh][Rr][Ee][Ff]=\042[^\042]*[:\057][^\042]*\042/))
  112.          {gsub(/[Hh][Rr][Ee][Ff]=\042/,("HREF=\042" dirprefix),haccum)};
  113.        xxx=0;if (haccum!~/[Hh][Rr][Ee][Ff]=\042/) {xxx=1}
  114. else {if (haccum!~/<[Aa][^<>]*[Hh][Rr][Ee][Ff][ \t]*=[^<>]*[Nn][Aa][Mm][Ee][ \t]*=[^<>]*>/)
  115. {if (haccum!~/<[Aa][^<>]*[Nn][Aa][Mm][Ee][ \t]*=[^<>]*[Hh][Rr][Ee][Ff][ \t]*=[^<>]*>/)
  116.                  {xxx=1}}};
  117.        if (xxx)
  118.          {gsub(/[Nn][Aa][Mm][Ee]=\042/,("HREF=\042" dirprefix fn "\043"),haccum)}};
  119.        # </lame>
  120.        printf "%" ((hlevel*2)+1) "s<LI>%s\n","",haccum;
  121.        haccum=""}}}
  122. #
  123. END{if (NR>0)
  124.       {if (toc) {liout()};
  125.        print "</ul>";
  126.        print "<!-- Replace this comment with your signature stuff -->";
  127.        print "</body></html>"}}
  128. #
  129. function liout() {
  130.  if (hlevel>0)
  131.    {printf "%" ((hlevel*2)+1) "s","";
  132.     for (i=hlevel;i>=1;--i) {printf "</UL>"};
  133.     printf "\n"};
  134.  hlevel=0}
  135. ##EOF
  136.