home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / lyx21041.zip / XFree86 / lib / X11 / lyx / reLyX / noweb2lyx.in (.txt) < prev    next >
LaTeX Document  |  1999-05-12  |  4KB  |  112 lines

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