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.
7 # Written with assistance from:
8 # Edmar Wienskoski Jr. <edmar-w-jr@technologist.com>
9 # Amir Karger <karger@post.harvard.edu>
11 # $Id: noweb2lyx.in,v 1.2 2000/03/29 23:02:36 karger Exp $
13 # NOTE: This file was automatically generated from noweb2lyx.lyx using noweb.
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;
23 &usage unless ($#ARGV == 1);
24 $input_file = $ARGV[0]; $output_file = $ARGV[1];
25 $pre_only = 0; $post_only = 0;
29 open(INPUT, "<$in") || die "Can not read $in: $!\n";
30 open(OUTPUT, ">$out") || die "Can not write $out: $!\n";
33 print "Usage: noweb2lyx [-pre | -post] input-file output-file
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";
42 &setup_files($input_file, $output_file);
44 $relyx_file = "temp$$";
45 &setup_files($input_file, $relyx_file);
47 inputline: while(<INPUT>)
49 if (/^\s*\<\<.*\>\>=/) { # Beginning of a noweb scrap
52 scrapline: while (<INPUT>) {
53 last scrapline if /^@\s+/;
57 if (/^@\s+$/) {$savedScrap .= $_; last switch; }
58 if (/^@\s+%def.*$/) {$savedScrap .= $_; last switch; }
59 if (/^@\s+(.*)$/) {$savedScrap .= "@\n"; $endLine = "$1\n"; }
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;
71 print OUTPUT; # Just let the line pass through
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{(.*)}/) {
88 $doc_class = "literate-" . $class;
89 die "reLyX returned non-zero: $!\n"
90 if (system("reLyX -c $doc_class $relyx_file"));
94 &setup_files("$input_file", "$output_file");
96 &setup_files("$relyx_file.lyx", "$output_file");
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>) {
105 last codeline if /^@\s+/;
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: $_";
115 # print the \latex latex line + next line
116 print OUTPUT "$_$line";
120 if (/\[\[.+\]\]/) { # special code for [[var]]
121 s/\[\[.+?\]{2,}/\n\\latex latex\n$&\n\\latex default\n/g;
125 print OUTPUT; # default
130 system("rm -f $relyx_file*") unless ($post_only || $pre_only);