1 ####################### VERBATIM COPYING SUBROUTINES ########################
2 # This file is part of reLyX.
3 # Copyright (c) 1998-9 Amir Karger karger@post.harvard.edu
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 # Subs for copying stuff verbatim from a TeX file instead of parsing it.
8 # These subs use the rather low-level TT:OpenFile::paragraph method, because
9 # the higher level methods assume you've parsed, and verbatim stuff might be
16 # This subroutine handles a \verb token. Text is guaranteed to be on one line.
17 # \verb must be followed by a non-letter, then copy anything until the next
18 # occurrence of that character.
19 my ($fileobject, $token) = @_;
20 my $verb = $token->exact_print; # "\verb" or "\verb*"
21 my $textref = $fileobject->paragraph;
22 # eat e.g., !text $\% text!
23 if ($$textref =~ s/^([^A-Za-z*]).*?\1//) {
25 } else { warn "unable to parse \\verb"; $verb .="||" }
30 # This subroutine eats text verbatim until a certain text is reached
31 # The end text itself is not eaten; this is necessary so that
32 # environments are properly nested (otherwise, TeX.pm complains)
33 # It returns a string containing the text
35 # Arg 0 is the Text::TeX::OpenFile file object, arg 1 is the beginning token
36 my $fileobject = shift;
37 my $begin_token = shift;
38 my %endtokentbl = ( '\(' => '\)' , '\[' => '\]' );
40 my $type = ref($begin_token);
41 $type =~ s/^Text::TeX:://o or die "unknown token type $type?!";
43 # Figure out beginning & end text of this token or environment
44 # Beginning text so we know if you have an environment nested within itself
45 # End text so we know when to finish copying OR when to 'pop' a level
46 # if an environment is nested within itself
47 # Because the searches will generally be matching expressions with backslashes
48 # and other meta-characters, we put \Q\E around (pieces of) the expressions
49 my ($begin_text, $end_text);
50 if ($type =~ /^Token$/) { # \( or \[
51 $begin_text = $begin_token->print; # e.g., '\('
52 die "unknown begin_text" unless exists $endtokentbl{$begin_text};
53 $end_text = "\Q$endtokentbl{$begin_text}\E";
54 # actually, begin_text shouldn't be nec. since you can't nest math
55 $begin_text = "\Q$begin_text\E"; # quote slashes, etc.
57 } elsif (/^Begin::Group::Args$/) { # \begin{foo}
58 # \s* to allow, e.g., '\begin {foo}'
59 $begin_text = $begin_token->print;
60 $begin_text = "\Q$begin_text\E";
61 $begin_text =~ s/begin/begin\\s*/;
62 ($end_text = $begin_text) =~ s/begin/end/;
65 die "copy_verbatim called with unknown token type $type!";
67 #print "\nsub copy_verbatim going to copy until $end_text\n" if $debug_on;
70 my $textref; # reference to stuff we read in to print
71 my $to_print = ""; #text to print
72 # we're automatically "nested" once since we had the original \begin
75 # (Eat and) Print out paragraphs until you find $end_text
76 # paragraph returns "" if it's time to get a new paragraph -- if that
77 # happens, we want to continue, but we can't dereference $textref
78 # Call paragraph with an argument so that it gets a new paragraph if
79 # it gets to the end of a paragraph
80 # Allow nesting of this environment!
81 while (defined ($textref = $fileobject->paragraph(1))) {
82 next unless $textref; # new paragraph; keep going
84 # If we match begin or end text, eat everything up to it
85 # Make sure to handle (nested) begin & end texts in order, so we can
86 # differentiate \begin \begin \end \end from \begin \end \begin \end
87 if ($$textref =~ /$end_text/ && $` !~ /$begin_text/) {
88 # Note that $` will be from the last *successful* match,
89 # namely the end_text match
91 $to_print .= $`; # print until the \end command
93 $to_print .= $&; # print the end text too
94 $$textref = $'; # leave the rest in the paragraph
96 # Leave end text (and anything after it) for TeX.pm
98 last; # done copying since there's no more nesting
101 # If we match beginning text, we have a nested environment
102 } elsif ($$textref =~ /$begin_text/ && $` !~ /$end_text/) {
103 $to_print .= $`; # print up to and
104 $to_print .= $&; # INCLUDING the begin text
105 $$textref = $'; # leave the rest in the paragraph
108 # If we didn't match begin OR end text, just eat the whole paragraph
110 $to_print .= $$textref;
114 die "eof without finding matching text $end_text" if (!defined $textref);
117 #print "Exiting sub copy_verbatim\n" if $debug_on;
119 } # end copy_verbatim
121 1; # return true to calling routine