Debian Codegraph @

lib/mlrisc-lib/MLRISC/Doc/html/mltex2html

raw
1 #!/usr/bin/perl
2 #
3 # This tool generates HTML pages in my own format given a stylized Latex file.
4 #
5 # The output is in HTML 4.01 Transitional form.
6 #
7 # Allen Leung ([email protected])
8
9 ############################################################################
10 #
11 # Formatting parameters
12 #
13 ############################################################################
14 $BACKGROUND_COLOR="#ffffff"; #white
15 $TEXT_COLOR="#000020";
16 $TITLE_COLOR="#aa0000"; #dark red
17 $ABSTRACT_TITLE_COLOR="#486591"; #black
18 $SECTION_COLOR="#486591"; #dark red
19 $SUBSECTION_COLOR="#486591"; #dark red
20 $SUBSUBSECTION_COLOR="#486591"; #dark red
21 $PARAGRAPH_COLOR="#486591"; #dark red
22 $NEWDEF_COLOR="#ff0000"; #red
23 $NEWTYPE_COLOR="#ff0000"; #red
24
25 $LINK_COLOR="navy";
26 $VLINK_COLOR="gray";
27 $ALINK_COLOR="maroon";
28
29 $SML_TYVAR_COLOR="#00aaaa"; #green
30 $SML_KEYWORD_COLOR="#6060a0"; #blue
31 $SML_IDENT_COLOR="#9c4040";
32
33 $EMPH_COLOR="#ff0000"; #red
34 $DESC_COLOR="#000070"; #blue
35 $CAPTION_COLOR="#007777"; #green/blue
36 $CODE_COLOR="#000000"; # black
37 $TOC_BACKGROUND_COLOR="#e6e6e6";
38 $SECTION_TOC_TEXT_COLOR="#486591";
39 $MAJORSECTION_TEXT_COLOR="ffffff";
40 $MAJORSECTION_BACKGROUND_COLOR="#486591";
41 $SECTION_TOC_BACKGROUND_COLOR="#e6e6e6";
42 $TOC_FACE="hevetica";
43 $MAX_LOCAL_TOC_ENTRY_LENGTH=40;
44 $MAX_GLOBAL_TOC_ENTRY_LENGTH=40;
45 $GLOBAL_TOC_WIDTH=170;
46 $SCREEN_WIDTH=700;
47 $TEXT_WIDTH=400;
48 $TOC_SIZE=-1;
49
50 $X_PIXELS = 1024;
51 $Y_PIXELS = 768;
52 $IMAGE_SCALING=0.8;
53 $PAPER_HEIGHT = "11in";
54 $PAPER_WIDTH = "8.5in";
55
56 @AUTHORS=("Lal George", "Allen Leung");
57 @EMAILS=('[email protected]','[email protected]');
58 $PWD=`pwd`;
59 $DATE=`date`;
60 $HOSTNAME=`hostname`;
61 chop $HOSTNAME;
62 chop $DATE;
63 chop $PWD;
64 $USER=$ENV{"USER"};
65 $WWWHOST="www.cs.nyu.edu";
66 $URLPREFIX="leunga/MLRISC/Doc/html/";
67
68 $BEGINPRE="<font color=\"$CODE_COLOR\"><small><pre>";
69 $ENDPRE="</pre></small></font>";
70 $BEGINALLTT="<font color=\"$CODE_COLOR\"><pre>";
71 $ENDALLTT="</pre></font>";
72
73 ############################################################################
74 #
75 # Global variables and tables
76 #
77 ############################################################################
78 $PARAM='[^\{\}]*';
79 sub globalInit()
80 {
81 @GLOBALTOC=(); # table of contents for entire document
82 @LOCALTOC=(); # table of contents for each section
83 %LINKLABEL={}; # name to link for referenceing
84 %LINKNAME={};
85 $PATHNAME=""; # current pathname
86 $FILENAME=""; # current file
87 $HTML_FILENAME="";
88 $LINE_NO=0; # current line
89 $GENERATE_TOC=0; # generate table of contents?
90 $PAGE_TITLE="";
91 $DOCUMENT_TITLE="";
92 $DOCUMENT_ABSTRACT="";
93 $DOCUMENT_AUTHOR="";
94 %SECTION_TEXT = ();
95 @TEXT=();
96 @TEXT_STACK=();
97 }
98
99 sub IGNORE { return ""; }
100
101 ############################################################################
102 #
103 # Error
104 #
105 ############################################################################
106 sub msg
107 { my($text) = @_;
108 print "$FILENAME:$LINE_NO: ", $text, "\n";
109 }
110 sub warning { msg("WARNING: " . $_[0]); }
111 sub error { msg($_[0]); die($_[0]); }
112 ############################################################################
113 #
114 # Filename processing
115 #
116 ############################################################################
117 sub basename
118 { my($f) = @_;
119 $f =~ s|^.*/||;
120 return $f;
121 }
122
123 sub dirname
124 { my($f) = @_;
125 $f =~ s|^(.*/).*$|\1|;
126 return $f;
127 }
128
129 sub suffix
130 { my($f) = @_;
131 $f = basename($f);
132 $f =~ /^.*\.(.*)/;
133 return $1;
134 }
135
136 sub replaceSuffix
137 { my($f, $old, $new) = @_;
138 if ($f =~ /^(.*\.)$old/)
139 { return $1 . $new;
140 } else
141 { error("file $f does not have suffix $old");
142 }
143 }
144
145 ############################################################################
146 #
147 # Indexing and Cross Referencing.
148 #
149 ############################################################################
150 sub localtocentry # add local index entry
151 { my($name,$level) = @_;
152 my(@entry) = ($name, $level);
153 push @LOCALTOC, \@entry;
154 my($label) = $LABELCOUNTER++;
155 $LINKLABEL{$name} = '#'. $label;
156 $LINKNAME{$name} = $name;
157 $CURRENT_SECTION_NAME=$name;
158 return "<a name=\"$label\"></a>";
159 }
160
161 sub globaltocentry # add global index entry
162 { my($name, $level) = @_;
163 my(@entry) = ($name, $level);
164 push @GLOBALTOC, \@entry;
165 my($key) = "section:$name";
166 $LINKLABEL{$key} = $HTML_FILENAME;
167 $LINKNAME{$key} = $name;
168 $CURRENT_SECTION_NAME=$name;
169 }
170
171 sub tableofcontents
172 { $GENERATE_TOC = 1;
173 print "[Generating table of contents]\n";
174 return "";
175 }
176
177 sub label
178 { my($label) = @_;
179 $LINKLABEL{$label} = $HTML_FILENAME . '#' . $label;
180 $LINKNAME{$label} = $CURRENT_SECTION_NAME;
181 return "<a name=\"$label\"></a>";
182 }
183
184 sub ref
185 { my($label) = @_;
186 return "{$label}";
187 }
188
189 sub lookupRef
190 { my($label) = @_;
191 my($ref) = $LINKLABEL{$label};
192 my($name) = $LINKNAME{$label};
193 return " <a href=\"$ref\">$name</a>";
194 }
195
196 sub resolveReferences
197 { my($text) = @_;
198 $text =~ s/\{($PARAM)\}/&lookupRef($1)/oge;
199 return $text;
200 }
201
202 ############################################################################
203 #
204 # Execute a subprogram
205 #
206 ############################################################################
207 sub runprog
208 { my($command) = @_;
209 print STDERR "running: ", $command, "\n";
210 system("$command");
211 if ($?) { die("$? $command"); }
212 }
213
214 ############################################################################
215 #
216 # Environment/command processing
217 #
218 ############################################################################
219 sub enterMode
220 { my($m) = @_;
221 push @MODE_STACK, $m;
222 $MODE = $m;
223 }
224
225 sub leaveMode
226 { my($m) = @_;
227 if ($MODE ne $m)
228 { die("Trying to leave $m but actually in " . $MODE . " mode"); }
229 pop @MODE_STACK;
230 $MODE = $MODE_STACK[$#MODE_STACK];
231 $parbreak=1;
232 }
233
234 sub pushTextStack
235 { push @TEXT_STACK, \@TEXT;
236 @TEXT=();
237 }
238
239 sub popTextStack
240 { my(@text) = @TEXT;
241 my($old) = pop @TEXT_STACK;
242 @TEXT = @{$old};
243 return "@text";
244 }
245
246 %DEFINEDNAMES = {};
247 %BEGINENV = {}; # how to process \begin{env}
248 %ENDENV = {}; # how to process \end{env}
249 %HTMLENV = {}; # html tag translation
250 %COMMAND = {}; # how to process \command
251 %ARITY = {}; # how many parameters does the environment has
252 @ENV0 = ();
253 @ENV1 = ();
254 @ENV2 = ();
255 @COMMAND0 = ();
256 @COMMAND1 = ();
257 @COMMAND2 = ();
258 @COMMAND3 = ();
259
260 sub newenvironment
261 { my($name,$arity,$begin,$end) = @_;
262 if (defined $DEFINEDNAMES{$name})
263 { die("$name has already been defined");
264 }
265 $DEFINEDNAMES{$name} = "env";
266 $BEGINENV{$name} = $begin;
267 $ENDENV{$name} = $end;
268 $ARITY{$name} = $arity;
269 push @ENV0, $name if $arity == 0;
270 push @ENV1, $name if $arity == 1;
271 push @ENV2, $name if $arity == 2;
272 }
273
274 sub newhtmlenvironment
275 { my($name,$tag) = @_;
276 if (defined $DEFINEDNAMES{$name})
277 { die("$name has already been defined");
278 }
279 $DEFINEDNAMES{$name} = "htmltag";
280 $HTMLENV{$name} = $tag;
281 push @ENV0, $name;
282 }
283
284 # This environment does not cause trailing paragraph breaks
285 sub newhtmlparenvironment
286 { newhtmlenvironment(@_);
287 my($name) = @_;
288 $PARENV{$name} = 1;
289 }
290
291 sub newcommand
292 { my($name,$arity,$command) = @_;
293 if (defined $DEFINEDNAMES{$name})
294 { die("$name has already been defined");
295 }
296 $DEFINEDNAMES{$name} = "command";
297 $COMMAND{$name} = $command;
298 $ARITY{$name} = $arity;
299 push @COMMAND0, $name if $arity == 0;
300 push @COMMAND1, $name if $arity == 1;
301 push @COMMAND2, $name if $arity == 2;
302 push @COMMAND3, $name if $arity == 3;
303 }
304
305 sub beginenv
306 { my($env,@args) = @_;
307 my($type) = $DEFINEDNAMES{$env};
308 if (! defined $type)
309 { warning("environment $env is not defined");
310 return "\\begin{$env}";
311 }
312 if ($type eq "env")
313 { my($func) = $BEGINENV{$env};
314 return $func->(@args); # call function
315 }
316 return "<$HTMLENV{$env}>" if $type eq "htmltag";
317 die("don't know how to handle \\begin{$env} at line $LINE_NO");
318 }
319
320 sub endenv
321 { my($env) = @_;
322 my($type) = $DEFINEDNAMES{$env};
323 if (! defined $type)
324 { warning("environment $env is not defined");
325 return "\\end{$env}";
326 }
327 return "</$HTMLENV{$env}>" if $type eq "htmltag";
328 if ($type eq "env")
329 { my($func) = $ENDENV{$env};
330 my($text) = $func->(); # call function
331 $parbreak = 1;
332 return $text;
333 }
334 die("don't know how to handle \\end{$env} at line $LINE_NO");
335 }
336
337 sub command
338 { my($command,@args) = @_;
339 my($type) = $DEFINEDNAMES{$command};
340 if (! defined $type)
341 { warning("command $command is not defined");
342 return "\\$env";
343 }
344 #print "[$command]";
345 if ($type eq "command")
346 { my($func) = $COMMAND{$command};
347 if (! $func)
348 { die("don't know how to handle \\$command at line $LINE_NO"); }
349 return $func->(@args);
350 }
351 die("don't know how to handle \\$command at line $LINE_NO");
352 }
353 ############################################################################
354 #
355 # Color processing
356 #
357 ############################################################################
358 $COLORS{"red"} = "#ff0000";
359 $COLORS{"green"} = "#00ff00";
360 $COLORS{"blue"} = "#0000ff";
361 $COLORS{"darkred"} = "#aa0000";
362 $COLORS{"darkgreen"} = "#00aa00";
363 $COLORS{"darkblue"} = "#0000aa";
364 $COLORS{"black"} = "#000000";
365 $COLORS{"grey"} = "#777777";
366 $COLORS{"white"} = "#ffffff";
367
368 sub colorOf
369 { my($c) = @_;
370 return $COLORS{$c} if (defined $COLORS{$c});
371 return "\"$c\"";
372 }
373 sub begincolor { my($c) = @_; return "<font color=" . colorOf($c) . ">"; }
374 sub endcolor { return "</font>"; }
375
376 ############################################################################
377 #
378 # Generic LaTeX fonts and sizes handling.
379 #
380 ############################################################################
381 sub bf { return ""; }
382 sub tt { return ""; }
383 sub it { return ""; }
384 sub em { return ""; }
385 sub sf { return ""; }
386 sub rm { return ""; }
387 sub huge { return ""; }
388 sub LARGE { return ""; }
389 sub Large { return ""; }
390 sub large { return ""; }
391 sub normalsize { return ""; }
392 sub small { return ""; }
393 sub footnotesize { return ""; }
394 sub scriptsize { return ""; }
395 sub tiny { return ""; }
396
397 ############################################################################
398 #
399 # Headers and footers
400 #
401 ############################################################################
402 sub header
403 { $HEADER= <<"END";
404 <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
405 <!-- Generated by mltex2html -->
406 <!-- do not edit this file -->
407 END
408 return $HEADER;
409 }
410
411 sub footer
412 { my($filename) = @_;
413 my($URL)="http://$WWWHOST/$URLPREFIX$outfile";
414 my($contact) = "<table>\n";
415 for ($i=0; $i <= $#AUTHORS; $i++)
416 { $author = $AUTHORS[$i];
417 $email = $EMAILS[$i];
418 $contact .= "<tr><td><font size=\"-1\">\n";
419 $contact .= "<a href=\"mailto:$email\">$author</a>\n";
420 $contact .= "</font></td></tr>\n";
421 }
422 $contact .= "</table>\n";
423
424 $FOOTER= <<"END";
425 <hr>
426 <table cellpadding=0 cellspacing=0 width="100%">
427 <tr>
428 <td align=left>
429 $contact
430 </td>
431 <td align=right>
432 <a href="http://cm.bell-labs.com/cm/cs/what/smlnj/index.html">
433 <img src="graphics/smlnj.jpg" width=80 height=50
434 alt="SML/NJ" border=0>
435 </a>
436 <a href="http://validator.w3.org/check?url=$URL">
437 <img src="graphics/vh401.gif" width=88 height=31
438 alt="Validate this page" border=0>
439 </a>
440 </td>
441 </tr>
442 <tr> <td align=left>
443 <font size="-1">
444 <i> Generated by
445 <a href="mltex.html">
446 <font color="#007777">mltex2html</font>
447 </a>
448 </i>
449 </font>
450 </td>
451 </tr>
452 <tr> <td>
453 <font size="-2">
454 Last modified: $DATE by $USER\@$HOSTNAME
455 </font>
456 </td>
457 </tr>
458 </table>
459 END
460 return $FOOTER;
461 }
462
463 ############################################################################
464 #
465 # SML keyword highlighting
466 #
467 ############################################################################
468 @KEYWORDS=("structure", "datatype", "type", "of", "eqtype", "struct",
469 "end", "case", "if", "then", "else", "signature",
470 "functor", "withtype", "sharing", "include", "where",
471 "val", "fun", "handle", "raise", "exception", "let", "in",
472 "local", "abstype", "rec", "and", "andalso", "orelse", "open",
473 "infix", "infixr", "nonfix"
474 );
475
476 @DEFKEYWORDS=("functor", "structure", "signature", "exception", "withtype",
477 "datatype", "type", "eqtype", "fun", "val");
478
479 $KEYWORDS = "@KEYWORDS";
480 $KEYWORDS =~ s/ /|/g;
481 $DEFKEYWORDS = "@DEFKEYWORDS";
482 $DEFKEYWORDS =~ s/ /|/g;
483 $SMLIDENT = "[a-zA-Z_][a-zA-Z0-9_']*";
484 $SMLTYPEVAR = "('+$SMLIDENT)";
485
486 sub highlightKeyword { return "<font color=\"$SML_KEYWORD_COLOR\"><b>$_[0]</b></font>"; }
487 sub highlightTyvar { return "<font color=\"$SML_TYVAR_COLOR\">$_[0]</font>"; }
488 sub highlightIdent
489 { my($keyword, $typevar, $ident) = @_;
490 if ($typevar ne "") { $typevar .= " "; }
491 return "$keyword $typevar<font color=\"$SML_IDENT_COLOR\"><b>$ident</b></font>";
492 }
493
494 sub escape
495 { my($line) = @_;
496 $line =~ s|<|<|g;
497 $line =~ s|>|>|g;
498 $line =~ s|<|&lt;|g;
499 $line =~ s|>|&gt;|g;
500 return $line;
501 }
502
503
504 sub smldisplay
505 { my($line) = @_;
506 #$line =~ s/\b(($DEFKEYWORDS)\s+($SMLTYPEVAR|\(\s*$SMLTYPEVAR(\s*,\s*$SMLTYPEVAR)*\))?)\s*($SMLIDENT)(\s*=)/&highlightIdent($2,$3,$8) . $9/oge;
507 $line =~ s/\b($KEYWORDS)\b/&highlightKeyword($1)/oge;
508 $line =~ s/^(sig)\b/&highlightKeyword($1)/oge;
509 $line =~ s/([^\.\w])(sig)\b/$1 . &highlightKeyword($2)/oge;
510 $line =~ s/('\w+)/&highlightTyvar($1)/oge;
511 return $line;
512 }
513
514 sub sml
515 { return "<tt>" . smldisplay(@_) . "</tt>";
516 }
517 ############################################################################
518 #
519 # Sectioning commands
520 #
521 ############################################################################
522 sub include
523 { my($filename) = @_;
524 $FILENAME= $PATHNAME . $filename . ".tex";
525 $HTML_FILENAME=replaceSuffix(basename($FILENAME),"tex","html");
526 print STDERR "[Processing $FILENAME]\n";
527 my($title,@text) = processSection($FILENAME);
528 my($localtoc) = "";
529 if ($#LOCALTOC >= 0)
530 { my(@toctitle) = ($title, 0);
531 my(@LOCALTOC) = (\@toctitle, @LOCALTOC);
532 $localtoc =
533 makeTOC("local",
534 "border=0 align=right bgcolor=\"$SECTION_TOC_BACKGROUND_COLOR\"",
535 $MAX_LOCAL_TOC_ENTRY_LENGTH,"",@LOCALTOC);
536 }
537 my(@entry) = ($title, $localtoc, @text);
538 $SECTION_TEXT{$FILENAME} = \@entry;
539 return "";
540 }
541
542
543 sub section
544 { my($name) = @_;
545 #print "section $name\n";
546 $parbreak=1;
547 $PAGE_TITLE=$name;
548 globaltocentry($name,1);
549 return "";
550 }
551
552 sub majorsection
553 { my($name) = @_;
554 my(@entry) = ($name, 0);
555 push @GLOBALTOC, \@entry;
556 $parbreak=1;
557 return "";
558 }
559
560 sub title
561 { $DOCUMENT_TITLE = $_[0];
562 $parbreak=1;
563 return "";
564 }
565
566 sub subsection
567 { my($name) = @_;
568 #print "[subsection $name]\n";
569 $parbreak=1;
570 my($label) = localtocentry($name, 2);
571 return <<"END"
572 $label
573 <h2><font color="$SUBSECTION_COLOR">$name</font></h2>
574 END
575 }
576
577 sub subsubsection
578 { my($name) = @_;
579 #print "subsubsection $name\n";
580 $parbreak=1;
581 my($label) = localtocentry($name, 3);
582 return <<"END";
583 $label
584 <h3><font color="$SUBSUBSECTION_COLOR">$name</font></h3>
585 END
586 }
587
588 sub paragraph
589 { my($name) = @_;
590 #print "paragraph $name\n";
591 $parbreak=1;
592 my($label) = localtocentry($name, 4);
593 return <<"END";
594 $label
595 <h4><font color="$PARAGRAPH_COLOR">$name</font></h4>
596 END
597 }
598
599 sub beginabstract
600 { print "[begin abstract]\n";
601 pushTextStack();
602 return "";
603 }
604
605 sub endabstract
606 { print "[end abstract]\n";
607 $DOCUMENT_ABSTRACT = popTextStack();
608 return "";
609 }
610
611 ############################################################################
612 #
613 # Hypertext links
614 #
615 ############################################################################
616 sub href { return "<a href=\"$_[0]\">$_[1]</a>"; }
617 sub mlrischref { return "<a href=\"../../$_[0]\" target=code>$_[1]</a>"; }
618 sub externhref { return "<a href=\"$_[0]\">$_[1]</a>"; }
619
620 ############################################################################
621 #
622 # Images and figures
623 #
624 ###########################################################################
625
626 sub isGraphics
627 { my($filename) = @_;
628 return 1 if $filename =~ /\.png$/;
629 return 1 if $filename =~ /\.jpg$/;
630 return 1 if $filename =~ /\.gif$/;
631 return 1 if $filename =~ /\.tif$/;
632 return 1 if $filename =~ /\.tiff$/;
633 return 0;
634 }
635
636 sub image { return "<img alt=\"$_[0]\" src=$_[1] $_[2]>"; }
637
638 #
639 # Given a image file, try to find out if the original source exists
640 # Paths are hardwired for now.
641 sub figOf
642 { my($filename) = @_;
643 my($basename) = "";
644 $basename = $1 if $filename =~ m|^\.\./pictures/eps/(.*)\.eps|;
645 my($fig) = "../pictures/fig/$basename.fig";
646 return $fig if (-f $fig);
647 return "";
648 }
649
650 #
651 # Try to convert the eps file to png
652 #
653 sub imageOf
654 { my($filename) = @_;
655 $basename = $1 if ($filename =~ m|^\.\./pictures/eps/(.*)\.eps$|);
656 my($png) = "../pictures/png/$basename.png";
657 return $png if -f $png;
658 my($jpg) = "../pictures/jpeg/$basename.jpg";
659 return $jpg if -f $jpg;
660
661 my($fig) = figOf($filename);
662 if ($fig)
663 { runprog("fig2dev -L png $fig $png");
664 return $png;
665 }
666 return "";
667 }
668
669 #
670 # Try to translate LaTeX lengths into pixel sizes
671 #
672 sub lengthOf
673 { my($l) = @_;
674 return $1 * 0.4 if $l =~ /^(\d*(\.\d*)?)em$/; # making a guess
675 return $1 * 0.4 if $l =~ /^(\d*(\.\d*)?)cm$/;
676 return $1 * 1.0 if $l =~ /^(\d*(\.\d*)?)in$/;
677 if (! ($l =~ /^\s*$/)) { die("lengthOf($l)"); }
678 return 0;
679 }
680 sub heightOf { return lengthOf(@_) / lengthOf($PAPER_HEIGHT) * $Y_PIXELS * $IMAGE_SCALING; }
681 sub widthOf { return lengthOf(@_) / lengthOf($PAPER_WIDTH) * $X_PIXELS * $IMAGE_SCALING; }
682
683 #
684 # Try to translate sizes into pixel sizes
685 #
686 sub makeSize
687 { my($height, $width) = @_;
688 $height = heightOf($height);
689 $width = widthOf($width);
690 my($size) = "";
691 $size .= "height=$height" if $height;
692 $size .= " width=$width" if $width;
693 return $size;
694 }
695
696
697 sub psfig
698 { my($params) = @_;
699 my($filename) = ($params =~ /figure=([^ ,]*)/);
700 my($height) = ($params =~ /height=([^ ,]*)/);
701 my($width) = ($params =~ /width=([^ ,]*)/);
702 my($size) = makeSize($height, $width);
703
704 if (! defined $filename) { die("figure not found in $params"); }
705 if (! -f $filename) { warning("filename $filename not found"); }
706
707 my($img) = imageOf($filename);
708
709 if ($img)
710 { return <<"END";
711 <a href="$img" target=image>
712 <img src="$img" align=left alt="Click to enlarge" $size border=0>
713 </a>
714 END
715 } else
716 { return "<a href=\"$filename\"> $filename </a>";
717 }
718 }
719
720 sub cpsfig { return "<center>" . psfig(@_) . "</center>"; }
721
722
723 sub beginfig
724 { my($border,$align) = @_;
725 $CAPTION = "";
726 $align = "align=left" if $align eq "l";
727 $align = "align=right" if $align eq "r";
728 $align = "align=center" if $align eq "c";
729 return "<table border=$border $align><tr><td align=center>";
730 }
731
732 sub endfig
733 { my($caption) = @_;
734 msg("new figure, caption=$caption");
735 if ($caption ne "")
736 { $caption = <<"END";
737 <tr>
738 <td align=center>
739 <font color="$CAPTION_COLOR">
740 <i>$caption</i>
741 </font>
742 </td>
743 </tr>
744 END
745 }
746 return "</td></tr>$caption</table>";
747 }
748
749 sub beginFigure { return beginfig(0,""); }
750 sub endFigure { return endfig($CAPTION); }
751
752 sub caption
753 { my($text) = @_;
754 $CAPTION = $text;
755 return "";
756 }
757
758 sub beginwrapfigure
759 { my($align,$size) = @_;
760 return beginfig(0,$align);
761 }
762
763 sub endwrapfigure { return endfig($CAPTION); }
764
765 ############################################################################
766 #
767 # Generic tabular processing
768 #
769 ############################################################################
770 sub begintab
771 { my($taboptions, $html) = @_;
772 enterMode("tabular");
773
774 # guess the border
775 my($border) = 0;
776 $border = 1 if ($taboptions =~ /^\|/);
777 $border = 2 if ($taboptions =~ /^\|\|/);
778
779 if ($html eq "") { $html = "align=center"; }
780
781 push @tabular_stack, $taboptions;
782 push @current_taboptions, $taboptions;
783
784 return "<table border=$border $html><tr>" . newtabitem();
785 }
786
787 sub endtab
788 { leaveMode("tabular");
789 pop @tabular_stack;
790 pop @current_tab;
791 return "</td></tr></table>";
792 }
793
794 sub newtabitem
795 { my($t) = $current_taboptions[$#current_taboptions];
796 my($align) = "left";
797
798 $t = $1 if $t =~ /^\|+(.*)/;
799 ($align = "center", $t = $1) if $t =~ /^c(.*)/;
800 ($align = "left", $t = $1) if $t =~ /^l(.*)/;
801 ($align = "right", $t = $1) if $t =~ /^r(.*)/;
802
803 $current_taboptions[$#current_taboptions] = $t;
804
805 return "<td align=$align>";
806 }
807
808 sub addtab
809 { return "</td>" . newtabitem();
810 }
811
812 sub tabnewline
813 { $current_taboptions[$#current_taboptions] =
814 $tabular_stack[$#tabular_stack];
815 return "</tr><tr>" . newtabitem();
816 }
817
818 sub newline
819 { return "<br>";
820 }
821
822 ############################################################################
823 #
824 # Math mode processing
825 #
826 ############################################################################
827 $MATHPARAM='[^$]*';
828 $MATHPARAM2='([^$\\]|\\[^)]+)*';
829 sub math
830 { my($math) = @_;
831
832 $math =~ s|&||g;
833
834 $math =~ s|\\le\b|<=|g;
835 $math =~ s|\\ge\b|>=|g;
836
837 #quote all < and > first
838 $math =~ s/</</g;
839 $math =~ s/>/>/g;
840
841 # fonts changing
842 $math =~ s|{\\tt\s+($PARAM)}|<tt>\1</tt>|g;
843
844 # subscript/superscripts
845 $math =~ s|_([^{])|<sub>\1</sub>|g;
846 $math =~ s|\^([^{])|<sup>\1</sup>|g;
847 $math =~ s|_{($PARAM)}|<sub>\1</sub>|g;
848 $math =~ s|^{($PARAM)}|<sup>\1</sup>|g;
849 $math =~ s|\\sb{($PARAM)}|<sub>\1</sub>|g;
850 $math =~ s|\\sp{($PARAM)}|<sup>\1</sup>|g;
851
852 # other stuff
853 $math =~ s|\\edge\{($PARAM)\}|&rarr<sub>\1</sub>|g;
854 $math =~ s|\\defas\b|=|g;
855 $math =~ s|\\lim\b|lim|g;
856 $math =~ s|\\sin\b|sin|g;
857 $math =~ s|\\cos\b|cos|g;
858 $math =~ s|\\tan\b|tan|g;
859 $math =~ s|\\log\b|log|g;
860 $math =~ s|\\equiv\b|==|g;
861 $math =~ s|\\ldots\b|...|g;
862
863 $math =~ s|\\in\b|&isin;|g;
864 $math =~ s|\\alpha\b|&alpha;|g;
865 $math =~ s|\\beta\b|&beta;|g;
866 $math =~ s|\\gamma\b|&gamma;|g;
867 $math =~ s|\\lambda\b|&lambda;|g;
868 $math =~ s|\\phi\b|&phi;|g;
869 $math =~ s|\\sum\b|&sum;|g;
870 $math =~ s|\\cup\b|&cup;|g;
871 $math =~ s|\\cap\b|&cap;|g;
872 $math =~ s|\\rightarrow\b|&rarr;|g;
873 $math =~ s|\\leftarrow\b|&larr;|g;
874
875 $math =~ s/\{/{/g;
876 $math =~ s/\}/}/g;
877
878 #unquote all < and >
879 $math =~ s|<|&lt;|g;
880 $math =~ s|>|&gt;|g;
881
882 $math =~ s|\\ne\b|<>|g;
883
884 $math =~ s|\\\\|<dt><dd>|g;
885
886 return "<math class=\"inline\"><i>$math</i></math>";
887 }
888
889 sub displaymath
890 { my($math) = @_;
891 return "<dl><dt><dd>" . math($math) . "</dl>";
892 }
893
894 sub stupidBrowserMath
895 {
896 s#&rarr(;|\b)#-&gt;#g;
897 s#&larr(;|\b)#&lt-#g;
898 s#&isin(;|\b)#in #g;
899 }
900
901
902 ############################################################################
903 #
904 # Frames and boxes
905 #
906 ############################################################################
907 sub beginboxit { return "<table border=1><tr><td>"; }
908 sub endboxit { return "</td></tr></table>"; }
909
910 ############################################################################
911 #
912 # Setup the commands and environments
913 #
914 ############################################################################
915 newenvironment("color",1, \&begincolor, \&endcolor);
916
917 newhtmlenvironment("small","small");
918 newhtmlenvironment("Bold","b");
919 newhtmlenvironment("Italics","i");
920 newhtmlenvironment("Emph","em");
921 newhtmlenvironment("address","address");
922
923 newhtmlparenvironment("quotation","blockquote");
924 newhtmlparenvironment("center","center");
925 newhtmlparenvironment("enumerate","ol");
926 newhtmlparenvironment("itemize","ul");
927 newhtmlparenvironment("description","dl");
928
929 newenvironment("boxit", 0, \&beginboxit, \&endboxit);
930 newenvironment("Boxit", 0, \&beginboxit, \&endboxit);
931
932 newcommand("title", 1, \&title);
933 newcommand("section", 1, \&section);
934 newcommand("subsection", 1, \&subsection);
935 newcommand("subsubsection", 1, \&subsubsection);
936 newcommand("paragraph", 1, \&paragraph);
937 newcommand("include", 1, \&include);
938 newcommand("majorsection", 1, \&majorsection);
939 newenvironment("abstract",0,\&beginabstract,\&endabstract);
940
941 newcommand("bf",0,\&bf);
942 newcommand("tt",0,\&tt);
943 newcommand("it",0,\&it);
944 newcommand("em",0,\&em);
945 newcommand("sf",0,\&sf);
946 newcommand("rm",0,\&rm);
947 newcommand("huge",0,\&huge);
948 newcommand("LARGE",0,\&LARGE);
949 newcommand("Large",0,\&Large);
950 newcommand("large",0,\&large);
951 newcommand("normalsize",0,\&normalsize);
952 newcommand("footnotesize",0,\&footnotesize);
953 newcommand("scriptsize",0,\&scriptsize);
954 newcommand("tiny",0,\&tiny);
955
956 newcommand("Term", 1, sub { return "<i>$_[0]</i>"; } );
957
958 newcommand("italics", 1, sub { return "<i>$_[0]</i>"; });
959 newcommand("bold", 1, sub { return "<b>$_[0]</b>"; });
960 newcommand("emph", 1, sub { return "<em>$_[0]</em>"; });
961
962 newcommand("href", 2, \&href);
963 newcommand("mlrischref", 2, \&mlrischref);
964 newcommand("externhref", 2, \&externhref);
965
966 newcommand("image", 3, \&image);
967 newcommand("psfig", 1, \&psfig);
968 newcommand("cpsfig", 1, \&cpsfig);
969
970 newcommand("linebreak", 0, \&IGNORE);
971 newcommand("hr", 0, sub { return "<hr>"; });
972 newcommand("hline", 0, \&IGNORE);
973 newcommand("pagebreak", 0, \&IGNORE);
974
975 newcommand("br", 1, sub { return "<br $_[0]>"; });
976
977 newcommand("label",1, \&label);
978 newcommand("ref",1, \&ref);
979
980 newcommand("MLRISC", 0, sub { return "MLRISC"; });
981 newcommand("LaTeX", 0, sub { return "L<sup>A</sup>T<sub>E</sub>X"; });
982 newcommand("MLTeX", 0, sub { return "MLT<sub>E</sub>X"; });
983
984
985 newcommand("caption", 1, \&caption);
986 newenvironment("Figure", 0, \&beginFigure, \&endFigure);
987 newenvironment("wrapfigure", 2, \&beginwrapfigure, \&endwrapfigure);
988
989 sub newdef { return "<font color=\"$NEWDEF_COLOR\">$_[0]</font>"; }
990 sub newtype { return "<font color=\"$NEWTYPE_COLOR\"><tt>$_[0]</tt></font>"; }
991
992 newcommand("newdef",1,\&newdef);
993 newcommand("newtype",1,\&newtype);
994
995 newcommand("cite", 1, sub { return "<cite>[$_[0]]</cite>"; });
996 newcommand("tableofcontents", 0, \&tableofcontents);
997
998 ############################################################################
999 #
1000 # Import style file
1001 #
1002 ############################################################################
1003 require "mltex.thm";
1004
1005 ############################################################################
1006 #
1007 # Setup the patterns for command and environment parsing
1008 #
1009 ############################################################################
1010 $COMMAND0 = "@COMMAND0"; $COMMAND0 =~ s/ /|/g;
1011 $COMMAND1 = "@COMMAND1"; $COMMAND1 =~ s/ /|/g;
1012 $COMMAND2 = "@COMMAND2"; $COMMAND2 =~ s/ /|/g;
1013 $COMMAND3 = "@COMMAND3"; $COMMAND3 =~ s/ /|/g;
1014 $COMMAND0 = 'XXXXXXXXXXXXXXXXX' if ($COMMAND0 eq "");
1015 $COMMAND1 = 'XXXXXXXXXXXXXXXXX' if ($COMMAND1 eq "");
1016 $COMMAND2 = 'XXXXXXXXXXXXXXXXX' if ($COMMAND2 eq "");
1017 $COMMAND3 = 'XXXXXXXXXXXXXXXXX' if ($COMMAND3 eq "");
1018 $ENV0 = "@ENV0"; $ENV0 =~ s/ /|/g;
1019 $ENV1 = "@ENV1"; $ENV1 =~ s/ /|/g;
1020 $ENV2 = "@ENV2"; $ENV2 =~ s/ /|/g;
1021 $ENV0 = 'XXXXXXXXXXXXXXXXX' if ($ENV0 eq "");
1022 $ENV1 = 'XXXXXXXXXXXXXXXXX' if ($ENV1 eq "");
1023 $ENV2 = 'XXXXXXXXXXXXXXXXX' if ($ENV2 eq "");
1024
1025 #print "COMMAND0 = $COMMAND0\n";
1026 #print "COMMAND1 = $COMMAND1\n";
1027 #print "COMMAND2 = $COMMAND2\n";
1028 #print "COMMAND3 = $COMMAND3\n";
1029 #print "ENV0 = $ENV0\n";
1030 #print "ENV1 = $ENV1\n";
1031 #print "ENV2 = $ENV2\n";
1032
1033 ############################################################################
1034 #
1035 # Indexing and Table of Contents generation.
1036 #
1037 ############################################################################
1038 sub truncentry
1039 { my($entry,$max_len) = @_;
1040 if ($max_len > 0 && length($entry) >= $max_len)
1041 { $entry = substr($entry, 0, $max_len-1) . "...";
1042 }
1043 return $entry;
1044 }
1045
1046 ############################################################################
1047 #
1048 # Make a table of contents
1049 #
1050 ############################################################################
1051 sub makeTOC
1052 { my($type, # local or global
1053 $html, # extra html parameters to the table
1054 $max_len, # maximal length of each entry
1055 $highlight, # which entry to highlight
1056 @TOC # the entries
1057 ) = @_;
1058
1059 return "" if $#TOC < 0;
1060
1061 my($toc) = "";
1062 my($labelprefix) = "";
1063 if ($type eq "global") { $labelprefix = "section:"; }
1064
1065 $toc = <<"END";
1066 <!-- table of contents -->
1067 <table cellpadding=0 cellspacing=0 $html>
1068 <tr><td>
1069 END
1070
1071 for $entry (@TOC)
1072 { my($name) = $entry->[0];
1073 my($level) = $entry->[1];
1074 my($label) = $LINKLABEL{$labelprefix . $name};
1075 my($text) = truncentry($name,$max_len);
1076
1077 # Major section heading
1078 if ($level == 0)
1079 {
1080 $toc .= <<"END";
1081 </td></tr><tr><td>
1082 <table bgcolor="$MAJORSECTION_BACKGROUND_COLOR" width="100%" border=0
1083 cellpadding=0 cellspacing=0>
1084 <tr><td align=center><font color="$MAJORSECTION_TEXT_COLOR">
1085 $text
1086 </font>
1087 </td></tr></table><tr><td>
1088 END
1089 } else
1090 # Normal extries
1091 {
1092 my($indent) = "-" x ($level - 2);
1093
1094 if ($name eq $highlight)
1095 { $text = "<font color=\"$SECTION_TOC_TEXT_COLOR\"><b>$text</b></font>"; }
1096
1097 if ($type eq "local")
1098 { $text = "<font size=\"$TOC_SIZE\" color=\"$SECTION_TOC_TEXT_COLOR\">$text</font>";
1099 } else
1100 { $text = "<font size=\"$TOC_SIZE\">$text</font>";
1101 }
1102
1103 $toc .= <<"END";
1104 $indent<a href="$label">$text</a><br>
1105 END
1106 }
1107 }
1108
1109 $toc .= <<"END";
1110 </td></tr>
1111 </table>
1112 <!-- end of table of contents -->
1113 END
1114 return $toc;
1115 }
1116 ############################################################################
1117 #
1118 # Generate the output
1119 #
1120 ############################################################################
1121
1122 sub writeSection
1123 { my($filename,$TITLE,$localtoc,@TEXT) = @_;
1124 my($outfile) = replaceSuffix(basename($filename), "tex", "html");
1125 my($toc) = "";
1126
1127 print STDERR "[Generating $outfile]\n";
1128
1129 # Global table of contents
1130 if ($GENERATE_TOC)
1131 { $toc =
1132 makeTOC("global",
1133 "border=0 width=$GLOBAL_TOC_WIDTH bgcolor=\"$TOC_BACKGROUND_COLOR\"",
1134 $MAX_GLOBAL_TOC_ENTRY_LENGTH, $TITLE, @GLOBALTOC);
1135 $toc = <<"END";
1136 <td valign=top align=left width="$GLOBAL_TOC_WIDTH">
1137 $toc
1138 </td>
1139 END
1140 }
1141
1142 open(OUTFILE,">$outfile") || die("$!: $outfile");
1143
1144 $header = header($TITLE);
1145 $footer = footer($outfile);
1146 $maintext = resolveReferences("@TEXT");
1147 $TOTAL_WIDTH = $TEXT_WIDTH + $GLOBAL_TOC_WIDTH;
1148
1149 print OUTFILE <<"END";
1150 $header
1151 <html>
1152 <head>
1153 <title> $TITLE </title>
1154 </head>
1155 <body bgcolor="$BACKGROUND_COLOR" text="$TEXT_COLOR"
1156 link="$LINK_COLOR" vlink="$VLINK_COLOR" alink="$ALINK_COLOR">
1157 <table border=0>
1158 <tr>
1159 $toc
1160 <td width=2> </td>
1161 <td valign=top align=left>
1162 <center><h1><font color="$SECTION_COLOR"><b>$TITLE</b></font></h1></center>
1163 <hr>
1164 $localtoc
1165 $maintext
1166 $footer
1167 </td>
1168 </tr>
1169 </table>
1170 </body>
1171 </html>
1172 END
1173 close OUTFILE;
1174 }
1175
1176 ############################################################################
1177 #
1178 # Write out the main page
1179 #
1180 ############################################################################
1181 sub writeDocument
1182 { my($filename) = @_;
1183 my($outfile) = replaceSuffix(basename($filename), "tex", "html");
1184
1185 print STDERR "[Generating $outfile]\n";
1186
1187 my($toc) = makeTOC("global","border=0 width=$GLOBAL_TOC_WIDTH",
1188 $MAX_GLOBAL_TOC_ENTRY_LENGTH, "", @GLOBALTOC);
1189
1190 open(OUTFILE,">$outfile") || die("$!: $outfile");
1191 my($header) = header($DOCUMENT_TITLE);
1192 my($footer) = footer($outfile);
1193
1194 print OUTFILE <<"END";
1195 $header
1196 <html>
1197 <head>
1198 <title> $DOCUMENT_TITLE </title>
1199 </head>
1200 <body bgcolor="$BACKGROUND_COLOR" text="$TEXT_COLOR">
1201 <table border=0>
1202 <tr>
1203 <td valign=top align=left> $toc </td>
1204 <td width=2> </td>
1205 <td valign=top align=left textwidth="$TEXT_WIDTH">
1206 <center><h1><font color="$TITLE_COLOR">$DOCUMENT_TITLE</font></h1></center>
1207 <hr>
1208 <center>
1209 $DOCUMENT_AUTHOR
1210 </center>
1211 <center>
1212 <font color="$ABSTRACT_TITLE_COLOR"><b>Abstract</b></font>
1213 </center>
1214 <blockquote>
1215 $DOCUMENT_ABSTRACT
1216 </blockquote>
1217 $footer
1218 </td></tr>
1219 </table>
1220 </td>
1221 </tr>
1222 </table>
1223 </body>
1224 </html>
1225 END
1226 close OUTFILE;
1227 }
1228
1229 ############################################################################
1230 #
1231 # Initialization
1232 #
1233 ############################################################################
1234 sub init()
1235 {
1236 $MODE = "latex";
1237 @MODE_STACK=($MODE);
1238 @tabular_stack=();
1239
1240 $LINE_NO=0;
1241 @TEXT=();
1242 @LOCALTOC=();
1243 $LABELCOUNTER="link0000";
1244 }
1245
1246 ############################################################################
1247 #
1248 # Main loop for processing a section
1249 #
1250 ############################################################################
1251 sub processSection
1252 { my ($filename) = @_;
1253
1254 if (! ($filename =~/\.tex$/))
1255 { die("$filename must end in .tex");
1256 }
1257 open(INFILE,$filename) || die("$!: $filename");
1258
1259 init();
1260
1261 local($LINE_NO) = 0;
1262
1263 while (<INFILE>)
1264 {
1265 $LINE_NO++;
1266
1267 # Verbatim mode handling
1268 if ($MODE eq "verbatim")
1269 { if (s|\\end{verbatim}|$ENDPRE|) { leaveMode("verbatim"); }
1270 else
1271 { s/</&lt;/g;
1272 s/>/&gt;/g;
1273 }
1274 push @TEXT, $_;
1275 next;
1276 }
1277
1278 if ($MODE eq "alltt")
1279 { if (s|\\end{alltt}|$ENDALLTT|) { leaveMode("alltt"); };
1280 push @TEXT, $_;
1281 next;
1282 }
1283
1284 $_ = escape($_) if $MODE eq "sml";
1285
1286 # \verb
1287 # Must match the mimimum number of times
1288 s|\\verb(.)(.*?)\1|<tt>\2</tt>|g if ($MODE ne "verbatim");
1289
1290 # paragraph
1291 if ($MODE eq "latex")
1292 { if (/^\s+$/)
1293 { if (! $parbreak) { $_ = "<p>\n"; $parbreak=1; }
1294 } else
1295 { $parbreak = 0; }
1296 }
1297
1298 # special formatting environments
1299 if (s|\\begin{SML}|$BEGINPRE|) { enterMode("sml"); };
1300 if (s|\\end{SML}|$ENDPRE|) { leaveMode("sml"); };
1301 if (s|\\begin{code}|$BEGINPRE|) { enterMode("code"); };
1302 if (s|\\end{code}|$ENDPRE|) { leaveMode("code"); };
1303 if (s|\\begin{verbatim}|$BEGINPRE|) { enterMode("verbatim"); };
1304 if (s|\\begin{alltt}|$BEGINALLTT|) { enterMode("alltt"); };
1305
1306 # method
1307 if (s|\\begin{methods}|<dl><dt><tt>|) { enterMode("methods"); };
1308 if (s|\\end{methods}|</tt></dl>|) { leaveMode("methods"); };
1309 s|\&|</tt><dd>|g if $MODE eq "methods";
1310 s|\\\\|<dt><tt>|g if $MODE eq "methods";
1311
1312 # Itemize
1313 s|\\item\[([^\]]*)\]|<dt><font color="$DESC_COLOR">\1</font><dd>|;
1314 s|\\item|<li>|;
1315
1316 # Tables
1317 s|\\begin{tabular}{($PARAM)}|&begintab($1,"")|oge;
1318 s|\\end{tabular}|&endtab()|oge;
1319
1320 s|\\begin{Table}{($PARAM)}{($PARAM)}|&begintab($1,$2)|oge;
1321 s|\\end{Table}|&endtab()|oge;
1322
1323 s|\&|&addtab()|oge if $MODE eq "tabular";
1324 s|\\\\|&tabnewline()|oge if $MODE eq "tabular";
1325 s|\\\\|&newline()|oge if $MODE eq "latex";
1326
1327 # SML mode handling
1328 s|\\sml{($PARAM)}|&sml($1)|oge;
1329 s|\\code{($PARAM)}|<tt>\1</tt>|g;
1330 if ($MODE eq "sml") { $_ = smldisplay($_); }
1331
1332 # Math mode handling
1333 if ($MODE ne "sml") { s|\$($MATHPARAM)\$|&math($1)|eg; }
1334 s|\\\(($MATHPARAM2)\\\)|&math($1)|oeg;
1335 if (/\\\[\s*\S|\S\s*\\\[/)
1336 { error("please put \\[ on a separate line by itself"); }
1337 if (/^\s*\S+\s*\\\]\s*$/)
1338 { error("please put \\] on a separate line by itself"); }
1339 if ($MODE eq "displaymath") { $_ = math($_); }
1340 if ($MODE eq "eqnarray*") { $_ = math($_); }
1341 if ($MODE eq "eqnarray") { $_ = math($_); }
1342
1343 if (s|\\\[\s*$|<dl><dt><dd>|) { enterMode("displaymath"); }
1344 if (s|\\\]|</dl>|) { leaveMode("displaymath"); }
1345 if (s|\\begin{eqnarray\*}|<dl><dt><dd>|) { enterMode("eqnarray*"); }
1346 if (s|\\end{eqnarray\*}|</dl>|) { leaveMode("eqnarray*"); }
1347 if (s|\\begin{eqnarray}|<dl><dt><dd>|) { enterMode("eqnarray"); }
1348 if (s|\\end{eqnarray}|</dl>|) { leaveMode("eqnarray"); }
1349
1350 # How to handle environment
1351 s|~?\\begin{($ENV0)}|&beginenv($1)|oge;
1352 s|~?\\begin{($ENV1)}{($PARAM)}|&beginenv($1,$2)|oge;
1353 s|~?\\begin{($ENV2)}{($PARAM)}{($PARAM)}|&beginenv($1,$2,$3)|oge;
1354 s|~?\\end{($PARAM)}|&endenv($1)|oge;
1355
1356 # How to handle commands
1357 s/~?\\($COMMAND0)(\{\}|\b)/&command($1)/oge;
1358 s|~?\\($COMMAND1){($PARAM)}|&command($1,$2)|oge;
1359 s|~?\\($COMMAND2){($PARAM)}{($PARAM)}|&command($1,$2,$3)|oge;
1360 s|~?\\($COMMAND3){($PARAM)}{($PARAM)}{($PARAM)}|&command($1,$2,$3,$4)|oge;
1361
1362 # Math mode stuff
1363 s|\\_|_|g;
1364 s|\\{|{|g;
1365 s|\\}|}|g;
1366
1367 # Indentation
1368 s|\\noindent ||g;
1369
1370 #
1371 # Not all browers can handle the math stuff yet. Do this in the mean time.
1372 #
1373 stupidBrowserMath();
1374
1375 push @TEXT, $_;
1376 }
1377 close INFILE;
1378 return ($PAGE_TITLE, @TEXT);
1379
1380 }
1381
1382 ############################################################################
1383 #
1384 # Main loop for processing a document
1385 #
1386 ############################################################################
1387 sub processDocument
1388 { my($filename) = @_;
1389 my($line, $output);
1390
1391 globalInit();
1392
1393 $MODE = "latex";
1394 @MODE_STACK=($MODE);
1395
1396 $PATHNAME = "";
1397 if ($filename =~ /^(.*\/)[^\/]*$/) { $PATHNAME = $1; }
1398
1399 $LINE_NO=0;
1400 $FILENAME=$filename;
1401
1402 open (DOCUMENT, $filename) || die("$! $filename");
1403 while ($_ = <DOCUMENT>)
1404 { $LINE_NO++;
1405 s|%.*$||; #skip comments
1406
1407 s|\\\\|&newline()|oge if $MODE eq "latex";
1408
1409 # How to handle environment
1410 s|~?\\begin{($ENV0)}|&beginenv($1)|oge;
1411 s|~?\\begin{($ENV1)}{($PARAM)}|&beginenv($1,$2)|oge;
1412 s|~?\\begin{($ENV2)}{($PARAM)}{($PARAM)}|&beginenv($1,$2,$3)|oge;
1413 s|~?\\end{($PARAM)}|&endenv($1)|oge;
1414
1415 # How to handle commands
1416 s/~?\\($COMMAND0)(\{\}|\b)/&command($1)/oge;
1417 s|~?\\($COMMAND1){($PARAM)}|&command($1,$2)|oge;
1418 s|~?\\($COMMAND2){($PARAM)}{($PARAM)}|&command($1,$2,$3)|oge;
1419 s|~?\\($COMMAND3){($PARAM)}{($PARAM)}{($PARAM)}|&command($1,$2,$3,$4)|oge;
1420
1421 push @TEXT, $_;
1422 }
1423 close DOCUMENT;
1424
1425 # Write out the sections
1426 foreach $file (keys %SECTION_TEXT)
1427 { my($output) = $SECTION_TEXT{$file};
1428 my($title, $localtoc, @text) = @{$output};
1429 writeSection($file, $title, $localtoc, @text);
1430 }
1431
1432 # Write out the main page
1433 writeDocument($filename);
1434 }
1435
1436 #main
1437 foreach $file (@ARGV)
1438 { processDocument($file)
1439 }
1440
1441