home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / lyx21041.zip / XFree86 / lib / X11 / lyx / reLyX / Verbatim.pm (.txt) < prev   
LaTeX Document  |  1999-01-25  |  5KB  |  105 lines

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