mkcdoc.pl 8.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
#use strict;

my %SECTION;
my %COMMAND;
my %TITLE; # the title line of the command
my %SUBTITLE; # the subtitle of the command
my %DISPLAY; # the display of the command in the TOC
my @files;
my $SourceFile;

opendir(D,"commands") or die "Can not open directory commands.";
@files = readdir(D);
closedir D;

for $SourceFile ( @files ){
  &ProcessFile( $SourceFile );
  }

my %SAVE_SECTION = %SECTION;
my %SAVE_COMMAND = %COMMAND;

&CreateOutput;

%SECTION = %SAVE_SECTION;
%COMMAND = %SAVE_COMMAND;

&CreateTexiOutput;
exit;

sub ProcessFile {
  my $file = shift;
  my $line;
  my $ActualCommand = undef;
  my @sections;
  my $section;

  return unless open(F,"commands/$file");
  while( defined($line = <F>) ){

    # /**SectionName starts a new command

    if( $line =~ /^\/\*\*(.*)\s*$/ ){
      if( defined($ActualCommand) ){
        warn "The command $ActualCommand was not closed. Closing implicitly.";
        $ActualCommand = undef;
        }
      $ActualCommand = $1;
      next;
      }

    next if ! defined $ActualCommand;

    if( $line =~ /^\*\// ){
      $ActualCommand = undef;
      next;
      }

    # =section list of sections the command has to be listed in

    if( $line =~ /^=section\s+(.*)/ ){
      my @sections = split /\s+/ , $1;
      for $section ( @sections ){
        push @{$SECTION{$section}},$ActualCommand;
        }
      next;
      }

    # =title title line of the actual command

    if( $line =~ /^=title\s+(.*)/ ){
      if( defined($TITLE{$ActualCommand}) ){
        my $err;
        $err = "Title is double defined for $ActualCommand\n" .
               "   " . $TITLE{$ActualCommand} . "\n" .
               "   " . $1 . "\n" ;
        warn $err;
        }
      $TITLE{$ActualCommand} = $1;
      next;
      }

    # =subtitle title line of the actual command

    if( $line =~ /^=subtitle\s+(.*)/ ){
      if( defined($SUBTITLE{$ActualCommand}) ){
        warn "Subtitle is double defined for $ActualCommand";
        }
      $SUBTITLE{$ActualCommand} = $1;
      next;
      }

    # =display title line of the actual command

    if( $line =~ /^=display\s+(.*)/ ){
      if( defined($DISPLAY{$ActualCommand}) ){
        warn "Title is double defined for $ActualCommand";
        }
      $DISPLAY{$ActualCommand} = $1;
      next;
      }

    push @{$COMMAND{$ActualCommand}},$line;
    next;
    }
  close F;
  }

sub CreateOutput {
  my $command;
  my $lines;
  my @commands;
  my $section;
  my @sections;

  @commands = sort keys %COMMAND;
  @sections = sort keys %SECTION;

  mkdir "../html";
  open(F,">../html/commands.html") or die "Can not open output file.";
  print F <<END;
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<H1>ScriptBasic commands and functions reference</H1>

<H2>List of Sections</H2>
END

  for $section ( @sections ){
    print F "<a href=\"#section_$section\">$section</A>\n";
    }

print F <<END;
<H2>List of Commands</H2>
<FONT SIZE="1">
END

  for $command ( @commands ){
    my $display;
    $display = $command;
    $display = $DISPLAY{$command} if defined $DISPLAY{$command};
    print F "<a href=\"#command_$command\">$display</A>\n";
    }

print F <<END;
</FONT>
<H2>List of Commands by Sections</H2>
END

  for $section ( @sections ){
    print F "<H3><a name=\"section_$section\">$section</A></H3>\n";
	print F "<FONT SIZE=\"1\">\n";
    my @scommands = sort @{$SECTION{$section}};
    my $scommand;
    for $scommand (@scommands){
      my $display;
      $display = $scommand;
      $display = $DISPLAY{$scommand} if defined $DISPLAY{$scommand};
      print F "<a href=\"#command_$scommand\">$display</A>\n";
      }
    print F "</FONT\>\n";
    }

print F <<END;
<H2>Commands</H2>
END

  for $command ( @commands ){
    my $title = $command;
    my $line;
    my $verbatim = 0;
    my $subtitle;
    my $FH;

    $title = $TITLE{$command} if defined $TITLE{$command};
    $subtitle = undef;
    $subtitle = $SUBTITLE{$command} if defined $SUBTITLE{$command};

    #
    # print lines that start a command
    #

    print F "<H3><a name=\"command_$command\">$title</A></H3>\n";
    print F "<H4>$subtitle</H4>\n" if defined $subtitle;
    print F "<BLOCKQUOTE>\n";
    $FH = 0;
    for $line ( @{$COMMAND{$command}} ){
      if( $line =~ /^\s*=details\s*$/ ){
        print F <<END;
<a href="commands/$command.html">details</A>
END
        mkdir "../html/commands",0777;
        open(DF,">../html/commands/$command.html") or die "Can not open file ../html/commands/$command.html";
        $FH = 1;
        print DF <<END;
<HTML>
<HEAD>
<TITLE>$command</TITLE>
</HEAD>
<BODY>
<H1>$title</H1>
<a href="../commands.html#command_$command">BACK</a><P>
END
        next;
        }
      if( $line =~ /^\s*=verbatim\s*$/ ){
        $verbatim++;
  	if( $FH ){
          print DF "<PRE>\n";
  	  }else{
          print F "<PRE>\n";
  	  }
        next;
        }
      if( $line =~ /^\s*=noverbatim\s*$/ ){
        $verbatim--;
  	if( $FH ){
          print DF "</PRE>\n";
  	  }else{
          print F "</PRE>\n";
  	  }
        next;
        }
      if( $verbatim ){
  	if( $FH ){
          print DF $line;
  	  }else{
          print F $line;
  	  }
        next;
        }

      if( $line =~ /^\s*=itemize\s*$/ ){
  	if( $FH ){
          print DF "<UL>\n";
  	  }else{
          print F "<UL>\n";
  	  }
        next;
        }
      if( $line =~ /^\s*=noitemize\s*$/ ){
  	if( $FH ){
          print DF "</UL>\n";
  	  }else{
          print F "</UL>\n";
  	  }
        next;
        }

      if( $line =~ /^\s*$/ ){
  	if( $FH ){
          print DF "<P>\n";
  	  }else{
          print F "<P>\n";
  	  }
        next;
        }

      my $l_line = $line;
      while( $l_line =~ /R\<(\w+?)\>/ ){
        my $d = $1;
        $d = $DISPLAY{$d} if defined $DISPLAY{$d};
        $l_line =~ s/R\<(\w+?)\>/\001a href=\"#command_$1\">$d\001\/a>/;
        }
      $l_line =~ s/T\<(.+?)\>/\001tt>$1\001\/tt>/g;
      $l_line =~ s/B\<(.+?)\>/\001B>$1\001\/B>/g;
      $l_line =~ s/I\<(.+?)\>/\001I>$1\001\/I>/g;

      $l_line =~ s/^\s*=item\s+/\001LI>/g;

      $l_line =~ tr{\001}{<};
      if( $FH ){
        print DF $l_line;
      }else{
        print F $l_line;
        }
      }
    #
    # print lines that close a command
    #
    print F "</BLOCKQUOTE>\n";
    if( $FH ){
	  print DF <<END;
<P><a href="../commands.html#command_$command">BACK</a>
</BODY>
</HTML>
END
	  close DF; # if it was opened
	  }
    }

  #
  # print lines that close the entire HTML file
  #
  print F <<END;
</BODY>
</HTML>
END
  close F;
  }

#################################################

sub CreateTexiOutput {
  my $command;
  my $lines;
  my @commands;
  my $section;
  my @sections;

  @commands = sort keys %COMMAND;
  @sections = sort keys %SECTION;

  open(F,">commands.texi") or die "Can not open commands.texi output file.";

  print F "\@chapter Command reference\n";

  for $command ( @commands ){
    my $title = $command;
    my $line;
    my $verbatim = 0;
    my $subtitle;
    my $FH;

    $title = $TITLE{$command} if defined $TITLE{$command};
    $subtitle = undef;
    $subtitle = $SUBTITLE{$command} if defined $SUBTITLE{$command};

    #
    # print lines that start a command
    #

    print F "\n\@section $title\n\n";
    print F "\@b{$subtitle}\n" if defined $subtitle;
    for $line ( @{$COMMAND{$command}} ){
      if( $line =~ /^\s*=details\s*$/ ){
        print F "\@subsection $command Details\n";
        next;
        }
      if( $line =~ /^\s*=verbatim\s*$/ ){
        $verbatim++;
        print F "\@example\n";
        next;
        }
      if( $line =~ /^\s*=noverbatim\s*$/ ){
        $verbatim--;
        print F "\@end example\n";
        next;
        }
      if( $verbatim ){
        print F $line;
        next;
        }

      if( $line =~ /^\s*=itemize\s*$/ ){
        print F "\@itemize\n";
        next;
        }
      if( $line =~ /^\s*=noitemize\s*$/ ){
        print F "\@end itemize\n";
        next;
        }

      if( $line =~ /^\s*$/ ){
        print F "\n\n";
        next;
        }

      my $l_line = $line;
      $l_line =~ s[\<tt\>][\@code\{]gi;
      $l_line =~ s[\<\/tt\>][\}]gi;
      $l_line =~ s[<UL>][\@itemize ]gi;
      $l_line =~ s[</UL>][\@end itemize ]gi;
      $l_line =~ s[<LI>][\@item ]gi;

      $l_line =~ s/\@/\@\@/g;
      $l_line =~ s/\$/\@\$/g;
      $l_line =~ s/\{/\@\{/g;
      $l_line =~ s/\}/\@\}/g;
      $l_line =~ s[<a href=".*?">(.*?)</a>][\@xref{$1}]gi;
      $l_line =~ s/R\<(.+?)\>/\@xref\{$1\}/g;
      $l_line =~ s/T\<(.+?)\>/\@code\{$1\}/g;
      $l_line =~ s/B\<(.+?)\>/\@b\{$1\}>/g;
      $l_line =~ s/I\<(.+?)\>/\@emph\{$1\}>/g;

      $l_line =~ s/^\s*=item\s+/\@item /g;

      print F $l_line;
      }
    }

  close F;
  }