home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / lyx21041.zip / XFree86 / lib / X11 / lyx / reLyX / noweb2lyx.cmd < prev    next >
OS/2 REXX Batch file  |  1999-10-04  |  4KB  |  132 lines

  1. extproc perl.exe -S
  2. #!perl
  3. # Copyright (C) 1999 Kayvan A. Sylvan <kayvan@sylvan.com>
  4. # You are free to use and modify this code under the terms of
  5. # the GNU General Public Licence version 2 or later.
  6. #
  7. # Written with assistance from:
  8. #   Edmar Wienskoski Jr. <edmar-w-jr@technologist.com>
  9. #   Amir Karger <karger@post.harvard.edu>
  10. #
  11. # $Id: noweb2lyx.in,v 1.3 1999/05/12 15:11:08 karger Exp $
  12. #
  13. # NOTE: This file was automatically generated from noweb2lyx.lyx using noweb.
  14. #
  15. &usage() if ($#ARGV < 1); # zero or one argument 
  16. if ($ARGV[0] eq "-pre") {
  17.   &usage unless ($#ARGV == 2);
  18.   $input_file = $ARGV[1]; $output_file = $ARGV[2]; $pre_only = 1;
  19. } elsif ($ARGV[0] eq "-post") {
  20.   &usage unless ($#ARGV == 2);
  21.   $input_file = $ARGV[1]; $output_file = $ARGV[2]; $post_only = 1;
  22. } else {
  23.   &usage unless ($#ARGV == 1);
  24.   $input_file = $ARGV[0]; $output_file = $ARGV[1];
  25.   $pre_only = 0; $post_only = 0;
  26. }
  27. sub setup_files {
  28.   my($in, $out) = @_;
  29.   open(INPUT, "<$in") || die "Can not read $in: $!\n";
  30.   open(OUTPUT, ">$out") || die "Can not write $out: $!\n";
  31. }
  32. sub usage() {
  33.   print "Usage: noweb2lyx [-pre | -post] input-file output-file
  34.  
  35. If -pre is specified, only pre-processes the input-file for reLyX.
  36. Similarly, in the case of -post, post-processes reLyX output.
  37. In case of bugs, Email Kayvan Sylvan <kayvan\@sylvan.com>.\n";
  38.   exit;
  39. }
  40. if (!$post_only) {
  41.   if ($pre_only) {
  42.     &setup_files($input_file, $output_file);
  43.   } else {
  44.     $relyx_file = "temp$$";
  45.     &setup_files($input_file, $relyx_file);
  46.   }
  47.   inputline: while(<INPUT>)
  48.   {
  49.     if (/^\s*\<\<.*\>\>=/) { # Beginning of a noweb scrap
  50.       $savedScrap = $_;
  51.       $endLine = "";
  52.       scrapline: while (<INPUT>) {
  53.         last scrapline if /^@\s+/;
  54.         $savedScrap .= $_;
  55.       };
  56.       switch: {
  57.         if (/^@\s+$/) {$savedScrap .= $_; last switch; }
  58.         if (/^@\s+%def.*$/) {$savedScrap .= $_; last switch; }
  59.         if (/^@\s+(.*)$/) {$savedScrap .= "@\n"; $endLine = "$1\n"; }
  60.       }
  61.       print OUTPUT "\\begin{reLyXskip}\n";
  62.       print OUTPUT $savedScrap;
  63.       print OUTPUT "\\end{reLyXskip}\n\n";
  64.       print OUTPUT "$endLine";
  65.     } elsif (/^@\s+(.*)/) { # Beginning of a documentation chunk
  66.       print OUTPUT $1; # We do not need the ``@'' part
  67.     } elsif (/\[\[.+\]\]/) { # noweb quoted code
  68.       s/\[\[.+?\]{2,}/{$&}/g;
  69.       print OUTPUT;
  70.     } else {
  71.       print OUTPUT; # Just let the line pass through
  72.     }
  73.   }
  74.   close(INPUT);
  75.   close(OUTPUT);
  76. }
  77. if ((!$pre_only) && (!$post_only)) {
  78.   open(INPUT, "<$relyx_file") ||
  79.     die "Can not read $relyx_file: $!\n";
  80.   $class = "article"; # default if none found
  81.   parse: while(<INPUT>) {
  82.     if (/\\docu[m]entclass{(.*)}/) {
  83.       $class = $1;
  84.       last parse;
  85.     }
  86.   }
  87.   close(INPUT);
  88.   $doc_class = "literate-" . $class;
  89.   die "reLyX.cmd returned non-zero: $!\n"
  90.     if (system("reLyX.cmd -c $doc_class $relyx_file"));
  91. }
  92. if (!$pre_only) {
  93.   if ($post_only) {
  94.     &setup_files("$input_file", "$output_file");
  95.   } else {
  96.     &setup_files("$relyx_file.lyx", "$output_file");
  97.   }
  98.   line: while(<INPUT>)
  99.   {
  100.     if (/\\latex latex/) { # Beginning of some latex code
  101.       if (($line = <INPUT>) =~ /^\s*<</) { # code scrap
  102.         $savedScrap = "\\layout Scrap\n\n$line";
  103.         codeline: while (<INPUT>) {
  104.           $savedScrap .= $_;
  105.           last codeline if /^@\s+/;
  106.         };
  107.         print OUTPUT $savedScrap;
  108.         slurp: while (<INPUT>) {
  109.           last slurp if /\\latex /;
  110.           next slurp if /\\newline/;
  111.           next slurp if /^\s*$/;
  112.           warn "confused by line: $_";
  113.         }
  114.       } else {
  115.         # print the \latex latex line + next line
  116.         print OUTPUT "$_$line";
  117.       }
  118.       next line;
  119.     }
  120.     if (/\[\[.+\]\]/) { # special code for [[var]]
  121.       s/\[\[.+?\]{2,}/\n\\latex latex\n$&\n\\latex default\n/g;
  122.       print OUTPUT;
  123.       next line;
  124.     }
  125.     print OUTPUT; # default
  126.   } 
  127.   close(INPUT);
  128.   close(OUTPUT);
  129. }
  130. system("rm -f $relyx_file*") unless ($post_only || $pre_only);
  131.