3 # This is a script that turns the SGCAL source of Exim's documentation into
4 # HTML. It can be used for both the filter document and the main Exim
5 # specification. The syntax is
7 # g2h [-split no|section|chapter] <source file> <title>
9 # Previously, -split section was used for the filter document, and -split
10 # chapter for the main specification. However, the filter document has gained
11 # some chapters, so they are both split by chapter now. Only one -split can be
14 # A number of assumptions about the style of the input markup are made.
16 # The HTML is written into the directory html/ using the source file base
19 # Written by Philip Hazel
20 # Starting 21-Dec-2001
21 # Last modified 26-Nov-2003
23 #############################################################################
27 ##################################################
28 # Open an output file #
29 ##################################################
32 open (OUT, ">$_[0]") || die "Can't open $_[0]\n";
36 print OUT "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
38 print OUT "<html>\n<head>\n<title>$doctitle" .
39 (($thischapter > 0)? " chapter $thischapter" : "") .
40 (($thissection > 0)? " section $thissection" : "") .
41 "</title>\n</head>\n" .
42 "<body bgcolor=\"#F8F8F8\" text=\"#00005A\" " .
43 "link=\"#FF6600\" alink=\"#FF9933\" vlink=\"#990000\">\n";
45 # Forward/backward links when chapter splitting
49 print OUT "<font size=2>\n";
50 printf OUT ("<a href=\"${file_base}_%s.html\">Previous</a> \n",
51 $thischapter - 1) if $thischapter > 1;
52 printf OUT ("<a href=\"${file_base}_%s.html\">Next</a> \n",
53 $thischapter + 1) if $thischapter < $maxchapter;
54 print OUT "<a href=\"${file_base}_toc.html\">Contents</a>\n";
55 print OUT " " x 6, "($doctitle)\n</font><hr>\n";
58 # Forward/backward links when section splitting
62 print OUT "<font size=2>\n";
63 printf OUT ("<a href=\"${file_base}_%s.html\">Previous</a> \n",
64 $thissection - 1) if $thissection > 1;
65 printf OUT ("<a href=\"${file_base}_%s.html\">Next</a> \n",
66 $thissection + 1) if $thissection < $maxsection;
67 print OUT "<a href=\"${file_base}_toc.html\">Contents</a>\n";
68 print OUT " " x 6, "($doctitle)\n</font><hr>\n";
71 # Save the final component of the current file name (for TOC creation)
73 $_[0] =~ /^(?:.*)\/([^\/]+)$/;
79 ##################################################
80 # Close an output file #
81 ##################################################
83 # The first argument is one of:
85 # "CHAP" a chapter is ending
86 # "SECT" a section is ending
87 # "" the whole thing is ending
89 # In the first two cases $thischapter and $thissection contain the new chapter
90 # and section numbers, respectively. In the third case, we can deduce what is
91 # ending from the flags. The variables contain the current values.
96 print OUT "<hr>\n" if !$lastwasrule;
101 print OUT "<font size=2>\n";
102 printf OUT ("<a href=\"${file_base}_%s.html\">Previous</a> ",
103 $thischapter - 2) if ($thischapter > 2);
104 print OUT "<a href=\"${file_base}_$thischapter.html\">Next</a> ";
105 print OUT "<a href=\"${file_base}_toc.html\">Contents</a>\n";
106 print OUT " " x 6, "($doctitle)\n</font>\n";
111 print OUT "<font size=2>\n";
112 printf OUT ("<a href=\"${file_base}_%s.html\">Previous</a> ",
113 $thissection - 2) if ($thissection > 2);
114 print OUT "<a href=\"${file_base}_$thissection.html\">Next</a> ";
115 print OUT "<a href=\"${file_base}_toc.html\">Contents</a>\n";
116 print OUT " " x 6, "($doctitle)\n</font>\n";
123 print OUT "<font size=2>\n";
124 printf OUT ("<a href=\"${file_base}_%s.html\">Previous</a> ",
125 $thischapter - 1) if ($thischapter > 1);
126 print OUT "<a href=\"${file_base}_toc.html\">Contents</a>\n";
127 print OUT " " x 6, "($doctitle)\n</font>\n";
131 print OUT "<font size=2>\n";
132 printf OUT ("<a href=\"${file_base}_%s.html\">Previous</a> ",
133 $thissection - 1) if ($thissection > 1);
134 print OUT "<a href=\"${file_base}_toc.html\">Contents</a>\n";
135 print OUT " " x 6, "($doctitle)\n</font>\n";
139 print OUT "</body>\n</html>\n";
145 ##################################################
146 # Handle an index line #
147 ##################################################
149 # This function returns an empty string so that it can be called as part
150 # of an s operator when handling index items within paragraphs. The two
153 # the text to index, already converted to HTML
154 # 1 for the concept index, 0 for the options index
158 my($hash) = $_[1]? \%cindex : \%oindex;
161 # Up the index count, and compute the reference to the file and the
166 "${file_base}_$thischapter.html#IX$index_count"
168 "${file_base}_$thissection.html#IX$index_count"
172 # Create the index key, which consists of the text with all the HTML
173 # coding and any leading quotation marks removed. Turn the primary/secondary
174 # splitting string "||" into ":".
179 $key =~ s/<[^>]+>//g;
180 $key =~ s/&#(\d+);/chr($1)/eg;
184 # Turn all spaces in the text into so that they don't ever split.
185 # However, there may be spaces in the HTML that already exists in the
186 # text, so we have to avoid changing spaces inside <>.
188 $text =~ s/ (?=[^<>]*(?:<|$))/ /g;
190 # If this is the first encounter with this index key, we create a
191 # straightforward reference.
193 if (!defined $$hash{$key})
195 $$hash{$key} = "<a href=\"$ref\">$text</a>";
198 # For the second and subsequent encounters, add "[2]" etc. to the
199 # index text. We find out the number by counting occurrences of "<a"
200 # in the existing string.
205 $number++ while $$hash{$key} =~ /<a/g;
206 $$hash{$key} .= " <a href=\"$ref\">[$number]</a>";
209 # Place the name in the current output
211 print OUT "<a name=\"IX$index_count\"></a>\n";
217 ##################################################
218 # Handle emphasis bars #
219 ##################################################
221 # Set colour green for text marked with "emphasis bars", keeping
222 # track in case the matching isn't perfect.
229 return "<font color=green>\n";
241 ##################################################
242 # Convert marked-up text #
243 ##################################################
245 # This function converts text from SGCAL markup to HTML markup, with a couple
248 # 1. We don't touch $t because that is handled by the .display code.
250 # 2. The text may contain embedded .index, .em, and .nem directives. We
251 # handle .em and .nem, but leave .index because it must be done during
252 # paragraph outputting.
254 # In a non-"rm" display, we turn $rm{ into cancelling of <tt>. Otherwise
255 # it is ignored - in practice it is only used in that special case.
257 # The order in which things are done in this function is highly sensitive!
261 my($rmspecial) = $_[1];
263 # Escape all & characters (they aren't involved in markup) but for the moment
264 # use &+ instead of &# so that we can handle # characters in the text.
268 # Turn SGCAL literals into HTML literals that don't look like SGCAL
269 # markup, so won't be touched by what follows. Again, use + instead of #.
272 $s =~ s/@([^@])/"&+".sprintf("%0.3d",ord($1)).";"/eg;
274 # Now turn any #s that are markup into spaces, and convert the previously
275 # created literals to the correct form.
278 $s =~ s/&\+(\d+);/&#$1;/g;
280 # Some simple markup that doesn't involve argument text.
282 $s =~ s/\$~//g; # turn $~ into nothing
283 $s =~ s/__/_/g; # turn __ into _
284 $s =~ s/--(?=$|\s|\d)/–/mg; # turn -- into endash in text or number range
285 $s =~ s/\(c\)/©/g; # turn (c) into copyright symbol
289 # $s =~ s/`([^']+)'/``$1''/g;
291 $s =~ s/`([^']+)'/“$1”/g;
293 # This is a fudge for some specific usages of $<; can't just do a global
294 # is it occurs in things like "$<variable name>" as well.
296 $s =~ s/(\d)\$<-/$1-/g; # turn 0$<- into 0-
297 $s =~ s/\$<//g; # other $< is ignored
299 # Turn <<...>> into equivalent SGCAL markup that doesn't involve the use of
300 # < and >, and then escape the remaining < and > characters in the text.
302 $s =~ s/<<([^>]*?)>>/<\$it{$1}>/g; # turn <<xxx>> into <$it{xxx}>
308 $s =~ s/\$sm\{//g; # turn $sm{ into nothing
309 $s =~ s/\$smc\{//g; # turn $smc{ into nothing
310 $s =~ s/\$smi\{//g; # turn $smi{ into nothing
312 $s =~ s/\$tt\{([^\}]*?)\}/<tt>$1<\/tt>/g; # turn $tt{xxx} into <tt>xxx</tt>
313 $s =~ s/\$it\{([^\}]*?)\}/<em>$1<\/em>/g; # turn $it{xxx} into <em>xxx</em>
314 $s =~ s/\$bf\{([^\}]*?)\}/<b>$1<\/b>/g; # turn $bf{xxx} into <b>xxx</b>
316 $s =~ s/\$cb\{([^\}]*?)\}/<tt><b>$1<\/b><\/tt>/g; # turn $cb{xxx} into
317 # <tt><b>xxx</b></tt>
319 $s =~ s/\\\\([^\\]*?)\\\\/<font size=-1>$1<\/font>/g; # turn \\xxx\\ into
321 $s =~ s/\\\?([^?]*?)\?\\/<a href="$1">$1<\/a>/g; # turn \?URL?\ into URL
323 $s =~ s/\\\(([^)]*?)\)\\/<i>$1<\/i>/g; # turn \(xxx)\ into <i>xxx</i>
324 $s =~ s/\\\"([^\"]*?)\"\\/<tt>$1<\/tt>/g; # turn \"xxx"\ into <tt>xxx</tt>
327 $s =~ s/\\\$([^\$]*?)\$\\/<tt>\$$1<\/tt>/g; # turn \$xxx$\ into <tt>$xxx</tt>
328 $s =~ s/\\\-([^\\]*?)\-\\/<i>-$1<\/i>/g; # turn \-xxx-\ into -italic
329 $s =~ s/\\\*\*([^*]*?)\*\*\\/<b>$1<\/b>/g; # turn \**xxx**\ into <b>xxx</b>
330 $s =~ s/\\\*([^*]*?)\*\\/<i>$1<\/i>/g; # turn \*xxx*\ into italic
331 $s =~ s/\\%([^*]*?)%\\/<b>$1<\/b>/g; # turn \%xxx%\ into bold
332 $s =~ s/\\([^\\]*?)\\/<tt>$1<\/tt>/g; # turn \xxx\ into <tt>xxx</tt>
333 $s =~ s/::([^\$]*?)::/<i>$1:<\/i>/g; # turn ::xxx:: into italic:
334 $s =~ s/\$\*\$/\*/g; # turn $*$ into *
340 $s =~ s/\$rm\{([^\}]*?)\}/<\/tt>$1<tt>/g; # turn $rm{xxx} into </tt>xxx<tt>
344 $s =~ s/\$rm\{([^\}]*?)\}/$1/g; # turn $rm{xxx} into xxx
347 # There is one case where the terminating } of an escape sequence is
348 # in another paragraph - this follows $sm{ - it can be fixed by
349 # removing any stray } in a paragraph that contains no { chars.
351 $s =~ s/\}//g if !/\{/;
353 # Remove any null flags ($$)
357 # If the paragraph starts with $c\b, remove it.
361 # If the paragraph starts with $e\b, indent it slightly.
363 $s =~ s/^\$e\b/ /;
365 # Handle .em, and .nem directives that occur within the paragraph
367 $s =~ s/\.em\s*\n/&setinem(1)/eg;
368 $s =~ s/\.nem\s*\n/&setinem(0)/eg;
370 # Explicitly included HTML
372 $s =~ s/\[\(([^)]+)\)\]/<$1>/g; # turn [(...)] into <...>
374 # Finally, do the substitutions and return the modified text.
376 $s =~ s/~~(\w+)/$var_value{$1}/eg;
383 ##################################################
384 # Start/end a paragraph #
385 ##################################################
387 # We want to leave paragraphs unterminated until we know that a horizontal
388 # rule does not follow, to avoid getting space inserted before the rule,
389 # which doesn't look good. So we have this function to help control things.
390 # If the argument is 1 we are starting a new paragraph; if it is 0 we want
391 # to force the ending of any incomplete paragraph.
408 ##################################################
409 # Handle a "paragraph" #
410 ##################################################
412 # Read a paragraph of text, which may contain many lines and may contain
413 # .index, .em, and .nem directives within it. We may also encounter
414 # ".if ~~html" within paragraphs. Process those directives,
415 # convert the markup, and output the rest as an HTML paragraph.
418 sub handle_paragraph{
423 if (/^\.if\s+~~html\b/)
426 $par =~ s/\s+$//; # lose unwanted whitespace and newlines
429 elsif ($htmlcond && /^\.else\b/)
431 while (<IN>) { last if /^\.fi\b/; }
435 elsif ($htmlcond && /^\.fi\b/)
441 last if /^\s*$/ || (/^\./ && !/^\.index\b/ && !/^\.em\b/ && !/^\.nem\b/);
444 $par = &handle_text($par, 0);
446 # We can't handle .index until this point, when we do it just before
447 # outputting the paragraph.
452 $par =~ s/\.index\s+([^\n]+)\n/&handle_index($1, 1)/eg;
459 ##################################################
460 # Handle a non-paragraph directive #
461 ##################################################
463 # The directives .index, .em, and .nem can also appear within paragraphs,
464 # and are then handled within the handle_paragraph() code.
466 sub handle_directive{
467 my($new_lastwasitem) = 0;
471 if (/^\.r?set\b/ || /^\.(?:\s|$)/) {} # ignore .(r)set and comments
473 elsif (/^\.justify\b/) {} # and .justify
475 elsif (/^\.newline\b/) { print OUT "<br>\n"; }
477 elsif (/^\.blank\b/ || /^\.space\b/) { print OUT "<br>\n"; }
479 elsif (/^\.rule\b/) { &setpar(0); print OUT "<hr>\n"; $lastwasrule = 1; }
481 elsif (/^\.index\s+(.*)/) { &handle_index(&handle_text($1), 1); }
483 # Emphasis is handled by colour
488 print OUT "<font color=green>" if ! $inem;
495 print OUT "</font>" if $inem;
499 # Ignore tab setting stuff - we use tables instead.
501 elsif (/^\.tabs(?:et)?\b/) {}
503 # .tempindent is used only to align some of the expansion stuff nicely;
504 # just ignore it. It is used in conjunction with .push/.pop.
506 elsif (/^\.(tempindent|push|pop)\b/) {}
508 # There are some instances of .if ~~sys.fancy in the source. Some of those
509 # that are not inside displays are two-part things, in which case we just keep
510 # the non-fancy part. For diagrams, however, they are in three parts:
513 # <aspic drawing stuff for PostScript and PDF>
515 # <ascii art for txt and Texinfo>
517 # <HTML instructions for including a gif>
520 # In this case, we skip to the third part.
522 elsif (/^\.if\s+~~sys\.fancy/ || /^\.else\b/)
525 { last if /^\.else\b/ || /^\.elif\s+!\s*~~html/ || /^\.fi\b/; }
529 while (<IN>) { last if /^\.else\b/ || /^\.fi\b/; }
533 # Similarly, for .if !~~sys.fancy, take the non-fancy part.
535 elsif (/^\.if\s+!\s*~~sys.fancy/) {}
537 # There are some explicit tests for ~~html for direct HTML inclusions
539 elsif (/^\.if\s+~~html\b/) {}
541 # There are occasional requirements to do things differently for Texinfo/HTML
542 # and PS/txt versions. The latter are produced by SGCAL, so that's what the
545 elsif (/\.if\s+~~sgcal/)
547 while (<IN>) { last if /\.else\b/ || /\.fi\b/; }
550 # Also there is a texinfo flag
552 elsif (/^\.if\s+~~texinfo\b/)
555 { last if /^\.else\b/ || /^\.elif\s+!\s*~~html/ || /^\.fi\b/; }
558 # Ignore any other .if, .else, or .fi directives
560 elsif (/^\.if\b/ || /^\.fi\b/ || /^\.else\b/) {}
564 elsif (/^\.indent\b/) {}
566 # Various flavours of numberpars map to corresponding list types.
568 elsif (/^\.numberpars\b/)
573 if ($rest =~ /(?:\$\.|\" \")/)
575 unshift @endlist, "ul";
576 unshift @listtype, "";
577 print OUT "<ul>\n<li>";
581 $nptype = ($rest =~ /roman/)? "a" : "1";
582 unshift @endlist, "ol";
583 unshift @listtype, " TYPE=\"$nptype\"";
584 print OUT "<ol>\n<li$listtype[0]>";
591 print OUT "</li>\n<li$listtype[0]>";
597 print OUT "</li>\n</$endlist[0]>\n";
602 # .display asis can use <pre> which uses a typewriter font.
603 # Otherwise, we have to do our own line breaking. Turn tabbed lines
604 # into an HTML table. There will always be a .tabs line first.
606 elsif (/^\.display\b/)
613 # For non asis displays, start a paragraph, and set up to put an
614 # explicit break after every line.
620 $indent = "<tt> </tt>";
623 # For asis displays, use <pre> and no explicit breaks
629 $indent = " ";
632 # Now read through until we hit .endd (or EOF, but that shouldn't happen)
633 # and process the lines in the display.
639 # The presence of .tabs[et] starts a table
644 print OUT "<table cellspacing=0 cellpadding=0>\n";
647 # Some displays have an indent setting - ignore
649 elsif (/^\.indent\b/) {}
651 # Some displays have .blank inside them
658 # Some displays have emphasis inside them
662 print OUT "<font color=green>" if ! $inem;
668 print OUT "</font>" if $inem;
672 # There are occasional instances of .if [!]~~sys.fancy inside displays.
673 # In both cases we want the non-fancy alternative. (The only thing that
674 # matters in practice is noticing .tabs[et] actually.) Assume the syntax
677 elsif (/^\.if\s+~~sys.fancy/ || /^\.else\b/)
681 last if /^\.fi\b/ || /^\.else/;
685 elsif (/^\.if\s+!\s*~~sys.fancy/) {}
689 # Ignore .newline and .linelength
691 elsif (/^\.newline\b/ || /^\.linelength\b/) {}
695 elsif (/^\.(\s|$)/) {}
697 # There shouldn't be any other directives inside displays
701 print "*** Ignored directive inside .display: $_";
704 # Handle a data line within a display. If it's an asis display, the only
705 # conversion is to escape the HTML characters. Otherwise, process the
719 $_ = &handle_text($_, !$rm);
720 $_ = "<tt>$_</tt>" if !$rm && $_ ne "";
723 # In a table, break fields at $t. For non-rm we must break the
724 # <tt> group as well.
730 s/\s*\$t\s*/ <\/td><td>/g;
734 s/\s*\$t\s*/ <\/tt><\/td><td><tt>/g;
737 print OUT "<tr><td> $_</td></tr>\n";
740 # Otherwise, output straight, with <br> for non asis displays
745 print OUT "$indent$_$eol\n";
748 } # Loop for display contents
750 # Finish off the table and the <pre> - leave a paragraph open
752 print OUT "</table>\n" if $intable;
753 print OUT "</pre>\n" if $asis;
756 # Handle configuration option definitions
758 elsif (/^\.startconf\s+(.*)/)
760 $confuse = &handle_text($1);
765 my($option, $type, $default) =
766 /^\.conf\s+(\S+)\s+("(?:[^"]|"")+"|\S+)\s+("(?:[^"]|"")+"|.*)/;
768 $option =~ s/\@_/_/g; # Underscore will be quoted in option name
770 # If $type ends with $**$, add ",expanded" as there doesn't seem to be
771 # a dagger character generally available.
773 $type =~ s/^"([^"]+)"/$1/;
774 $type =~ s/\$\*\*\$/, expanded/;
776 # Default may be quoted, and it may also have quotes that are required,
779 $default =~ s/^"(.*)"$/$1/;
780 $default =~ s/""/"/g;
781 $default = &handle_text($default, 0);
785 &handle_index($option, 0);
786 print OUT "<h3>$option</h3>\n" .
787 "<i>Use:</i> $confuse<br>" .
788 "<i>Type:</i> $type<br><i>Default:</i> $default<br>\n";
791 elsif (/^\.endconf\b/)
793 print OUT "<hr><br>\n";
797 # Handle "items" - used for expansion items and the like. We force the
798 # item text into bold, and put a rule between items.
800 elsif (/^\.startitems\b/) {}
802 elsif (/^\.item\s+(.*)/)
806 $arg =~ s/^"(.*)"$/$1/;
807 $arg = &handle_text($arg, 0);
809 # If there are two .items in a row, we don't want to put in the
810 # separator line or start a new paragraph.
821 print OUT "<b>$arg</b>\n";
822 $new_lastwasitem = 1;
825 elsif (/^\.enditems\b/)
827 print OUT "<hr><br>\n";
831 # Handle command line option items
833 elsif (/^\.startoptions\b/) {}
835 elsif (/^\.option\s+(.*)/)
838 $arg =~ s/"([^"]*)"/$1/g;
843 # For indexing, we want to take up to the first # or < in the line,
846 my($name) = $arg =~ /^([^#<]+)/;
847 $name = &handle_text($name, 0);
848 &handle_index("-$name", 0);
850 # Output as heading, after the index
852 $arg = &handle_text($arg, 0);
853 print OUT "<h3>-$arg</h3>\n";
856 elsif (/^\.endoptions\b/)
858 print OUT "<hr><br>\n";
861 # Found an SGCAL directive that isn't dealt with. Oh dear.
865 print "*** Unexpected SGCAL directive: line $. ignored:\n";
869 # Remember if last was a .item, and read the next line
871 $lastwasitem = $new_lastwasitem;
877 ##################################################
878 # First Pass - collect references #
879 ##################################################
884 open (IN, $source_file) || die "Can't open $source_file (first pass)\n";
887 # At the start of the specification text, there are some textual replacement
888 # definitions. They set values, but not cross-references. They may be preceded
891 $_ = <IN> while (/^\.(\s|$)/);
893 while (/^\.r?set\s+(\S+)\s+"?([^"]+)\"?\s*$/)
899 # Now skip on till we hit the start of the first chapter. It will be numbered
900 # 0 if we hit ".set chapter -1". There is only ever one unnumbered chapter.
902 while (!/^\.chapter/)
904 $thischapter = -1 if /^\.set\s+chapter\s+-1/;
908 # Loop for handling chapters
915 # Scan through chapter, setting up cross-references to the chapter
916 # and to the sections within it.
920 last if /^\.chapter/;
929 # Handle .(r)set directives.
931 if (/^\.r?set\s+(\S+)\s+"?([^"]+)\"?\s*$/ && $1 ne "runningfoot")
933 my($key,$value) = ($1,$2);
934 $value =~ s/~~chapter/$thischapter/e;
935 $value =~ s/~~section/$thissection/e;
937 # Only one of $chapsplit or $sectsplit can be set.
942 "<a href=\"${file_base}_$thischapter.html\">$value</a>"
944 "<a href=\"#CHAP$thischapter\">$value</a>";
947 elsif ($key =~ /^SECT/)
950 "<a href=\"${file_base}_$thischapter.html" .
951 "#SECT$thischapter.$thissection\">$value</a>"
953 $sectsplit? "<a href=\"${file_base}_$thissection.html\">$value</a>"
955 "<a href=\"#SECT$thischapter.$thissection\">$value</a>";
958 $var_value{$key} = $value;
970 ##################################################
971 # Second Pass - generate HTML #
972 ##################################################
983 # Open the source file and get the first line
985 open (IN, $source_file) || die "Can't open $source_file (2nd pass)\n";
988 # Skip on till we hit the start of the first chapter, but note if we
989 # pass ".set chapter -1", which is used to indicate no chapter numbering for
990 # the first chapter (we number is 0). Keep track of whether we are in macro
991 # definitions or not, and when not, notice occurrences of .index, because this
992 # are the "x see y" type entries.
994 while (!/^\.chapter/)
996 $thischapter = -1 if /^\.set\s+chapter\s+-1/;
997 $inmacro = 1 if /^\.macro/;
998 $inmacro = 0 if /^\.endm/;
999 if (!$inmacro && /^\.index\s+(.*)/)
1003 $s = &handle_text($s, 0);
1004 $s =~ s/ / /g; # All spaces unsplittable
1006 $key =~ s/<[^>]+>//g;
1007 $key =~ s/&#(\d+);/chr($1)/eg;
1015 open (TOC, ">$html/${file_base}_toc.html") ||
1016 die "Can't open $html/${file_base}_toc.html\n";
1018 print TOC "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
1019 print TOC "<html>\n<head>\n<title>$doctitle Contents</title>\n</head>\n" .
1020 "<body bgcolor=\"#F8F8F8\" text=\"#00005A\" " .
1021 "link=\"#FF6600\" alink=\"#FF9933\" vlink=\"#990000\">\n";
1022 print TOC "<h1>$doctitle</h1><hr>\n<ul>\n";
1024 # Open the data file if we are not splitting at chapters
1026 &openout("$html/${file_base}.html") if !$chapsplit;
1028 # Loop for handling chapters. At the start of this loop, $_ is either EOF,
1029 # or contains a .chapter line.
1031 $firstchapter = $thischapter + 1;
1035 print TOC "</ul>\n" if $insection;
1042 # Start a new file if required
1046 &closeout("CHAP") if $thischapter != $firstchapter;
1047 &openout("$html/${file_base}_$thischapter.html");
1050 # Set up the chapter title. Save it for the TOC. Set up the anchor and
1051 # link back to the TOC and show the title.
1053 $_ =~ /^\.chapter\s+(.*)/;
1055 my($title) = (($thischapter > 0)? "$thischapter. " : "") . &handle_text($1, 0);
1058 print TOC "<li><a " .
1059 "name=\"TOC$tocn\" " .
1060 "href=\"$current_file#CHAP$thischapter\">$title</a></li>\n";
1063 print OUT "<a name=\"CHAP$thischapter\" href=\"${file_base}_toc.html#TOC$tocn\">\n";
1064 print OUT "$title\n</a></h1>\n";
1066 # Scan the contents of the chapter
1071 last if /^\.chapter/;
1073 # Handle the start of a new section, starting a new file if required
1075 if (/^\.section\s+(.*)/)
1079 print TOC "<ul>\n" if !$insection;
1082 my($title) = (($thischapter > 0)? "$thischapter.$thissection " :
1083 "$thissection. ") . &handle_text($1, 0);
1088 &openout("$html/${file_base}_$thissection.html");
1092 printf TOC ("<li><a " .
1093 "name=\"TOC$tocn\" " .
1094 "href=\"$current_file#SECT%s$thissection\">%s</a></li>\n",
1095 ($thischapter > 0)? "$thischapter." : "", $title);
1099 printf OUT ("<a name=\"SECT%s$thissection\" ",
1100 ($thischapter > 0)? "$thischapter." : "");
1101 print OUT "href=\"${file_base}_toc.html#TOC$tocn\">\n";
1102 print OUT "$title\n</a></h2>\n";
1107 # Blank lines at this level are ignored
1114 # Directive and non-directive lines are handled independently, though
1115 # in each case further lines may be read. Afterwards, the next line is
1116 # in $_. If .em is at the start of a paragraph, treat it with the
1117 # paragraph, because the matching .nem will be too. Messy!
1126 print OUT "<font color=green>" if ! $inem;
1128 # Used to handle it here - but that fails if it is .section.
1129 # Just let the next iteration of the loop handle it.
1130 # &handle_directive();
1136 &handle_paragraph();
1146 &handle_directive();
1154 &handle_paragraph();
1159 } # Loop for each line in a chapter
1160 } # Loop for each chapter
1162 # Close the last file, end off the TOC, and we are done.
1166 print TOC "</ul>\n" if $insection;
1168 if (defined %cindex)
1170 $cindex_tocn = ++$tocn;
1171 print TOC "<li><a name=\"TOC$tocn\" ".
1172 "href=\"${file_base}_cindex.html\">Concept Index</a></li>\n";
1175 if (defined %oindex)
1177 $oindex_tocn = ++$tocn;
1178 print TOC "<li><a name=\"TOC$tocn\" ".
1179 "href=\"${file_base}_oindex.html\">Option Index</a></li>\n";
1182 print TOC "</ul>\n</body>\n</html>\n";
1190 ##################################################
1191 # Adjust index points #
1192 ##################################################
1194 # Because of the way the source is written, there are often index entries
1195 # that immediately follow the start of chapters and sections and the definition
1196 # of "items" like "helo = verify". This gets the correct page numbers for the
1197 # PostScript and PDF formats. However, for HTML we want the index anchor to be
1198 # before the section heading, because browsers tend to put the index point at
1199 # the top of the screen. So we re-read all the files we've just created, and
1200 # move some of the index points about. This is necessary only if indexes exist.
1201 # The files are small enough to be handled entirely in memory.
1203 sub adjust_index_points {
1204 print "Adjusting index points to precede headings\n";
1208 opendir(DIR, "$html") || die "Failed to opendir $html\n";
1209 while ($file = readdir(DIR))
1212 next unless $file =~ /^${file_base}_\d+\.html$/;
1214 open(IN, "<$html/$file") ||
1215 die "Failed to open $html/$file (read)\n";
1219 for ($i = 0; $i < @lines; $i++)
1221 if ($lines[$i] =~ /^<a name="IX\d+"><\/a>$/)
1223 # Handle an index line that follows a heading definition. Move it back
1224 # to just before the <h1> or whatever. This preserves the order of
1225 # multiple index lines, not that that matters.
1227 if ($lines[$i-1] =~ /^<\/a><\/h(\d)>/)
1231 for ($j = $i-2; $j > 0 && $j > $i - 10; $j--)
1233 if ($lines[$j] =~ /<h$1>/)
1241 splice(@lines, $j, 0, splice(@lines, $i, 1));
1245 # Handle an index line that follows an "item". Move it back one line.
1247 elsif ($lines[$i-1] =~ /^<b>.*<\/b>\s*$/)
1249 splice(@lines, $i-1, 0, splice(@lines, $i, 1));
1252 # Handle an index line that follows a "conf" definition
1254 elsif ($lines[$i-1] =~ /^<i>Type:<\/i>/ && $lines[$i-2] =~ /^<h3>/)
1256 splice(@lines, $i-2, 0, splice(@lines, $i, 1));
1259 # Handle an index line that follows an "option" definition
1261 elsif ($lines[$i-1] =~ /^<h3>/)
1263 splice(@lines, $i-1, 0, splice(@lines, $i, 1));
1268 open(OUT, ">$html/$file") ||
1269 die "Failed to open $html/$file (write)\n";
1280 ##################################################
1282 ##################################################
1286 my($ifname) = $_[1];
1287 my($ititle) = $_[2];
1290 open(INDEX, ">$html/${file_base}_$_[1].html") ||
1291 die "Failed to open $html/${file_base}_$ifname\n";
1293 print INDEX "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
1294 print INDEX "<html>\n<head>\n<title>$doctitle $ititle</title>\n";
1295 print INDEX "<base target=\"body\">\n</head>\n";
1297 print INDEX "<body bgcolor=\"#FFFFDF\" text=\"#00005A\" " .
1298 "link=\"#FF6600\" alink=\"#FF9933\" vlink=\"#990000\">\n";
1300 print INDEX "<h3>$ititle</h3>\n";
1302 # We have to scan the keys in the hash twice; first to build the list
1303 # of initial letters, and then to do the business. The first time we
1304 # do not need to sort them.
1306 foreach $key (keys %$hash)
1308 my($initial) = substr($key,0,1);
1309 $initial = "\U$initial";
1310 $indexindex{$initial} = 1 if $initial ge "A" && $initial le "Z";
1313 print INDEX "<p>\n";
1314 foreach $key (sort keys %indexindex)
1316 print INDEX " <a href=\"#$key\" target=\"index\">$key</a>\n";
1318 print INDEX "<hr></p>\n";
1321 print INDEX "<p>\n";
1328 $aa =~ s/^\x93//; # Seems like the actual char values are
1329 $bb =~ s/^\x93//; # set by this time, not "“"
1331 return ("\L$aa" eq "\L$bb")? ("$aa" cmp "$bb") : ("\L$aa" cmp "\L$bb");
1335 my($initial) = substr($key,0,1);
1336 $initial = "\U$initial";
1337 if ($initial ne $letter && $initial ge "A" && $initial le "Z")
1339 print INDEX "<br>\n";
1340 print INDEX "<a name=\"$initial\"></a>\n";
1341 print INDEX "<font size=\"+1\">\U$initial\E</font><br>\n";
1344 print INDEX "$$hash{$key}<br>\n";
1347 print INDEX "</p>\n";
1349 print INDEX "</body>\n</html>\n";
1356 ##################################################
1357 # Show usage and die #
1358 ##################################################
1361 die "Usage: g2h [-split no|section|chapter] <source> <title>\n";
1366 ##################################################
1367 # Entry point and main program #
1368 ##################################################
1371 # Directory in which to put the new HTML files
1400 while (scalar @ARGV > 0 && $ARGV[0] =~ /^-/)
1402 if ($ARGV[0] eq "-split" && !$splitset)
1406 my($type) = shift @ARGV;
1407 if ($type eq "section") { $sectsplit = 1; }
1408 elsif ($type eq "chapter") { $chapsplit = 1; }
1409 elsif ($type eq "no" ) { $sectsplit = $chapsplit = 0; }
1415 # Get the source file and its base
1417 &usage() if scalar @ARGV <= 0;
1418 $source_file = shift @ARGV;
1419 ($file_base) = $source_file =~ /^(.*)\.src$/;
1421 &usage() if scalar @ARGV <= 0;
1422 $doctitle = shift @ARGV;
1424 print "\nCreate HTML for $doctitle from $source_file\n";
1426 # Remove the old HTML files
1428 print "Removing old HTML files\n";
1429 system("/bin/rm -rf $html/${file_base}_*.html");
1431 # First pass identifies all the chapters and sections, and collects the
1432 # values of the cross-referencing variables.
1434 print "Scanning for cross-references\n";
1437 $maxchapter = $thischapter; # Used if chapter splitting
1438 $maxsection = $thissection; # Used if section splitting
1440 # Second pass actually creates the HTML files.
1442 print "Creating the HTML files\n";
1445 # Reprocess for moving some of the index points, if indexes were created
1447 &adjust_index_points() if scalar(keys %cindex) > 0 || scalar(keys %oindex) > 0;
1449 # Finally, we must create the option and concept indexes if any data
1450 # has been collected for them.
1452 if (scalar(keys %cindex) > 0)
1454 print "Creating concept index\n";
1455 &create_index(\%cindex, "cindex", "Concepts");
1458 if (scalar(keys %oindex) > 0)
1460 print "Creating option index\n";
1461 &create_index(\%oindex, "oindex", "Options");