Annotation of doc/help/texxml2latex.pl, revision 1.7

1.1       bowersj2    1: #!/usr/bin/perl
                      2: 
1.2       bowersj2    3: # The LearningOnline Network with CAPA
                      4: # Converts a texxml file into a single tex file
                      5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: # 7-16-2002 Jeremy Bowers
                     29: 
1.1       bowersj2   30: use strict;
                     31: use HTML::TokeParser;
                     32: use GDBM_File;
1.5       bowersj2   33: use File::Temp;
1.1       bowersj2   34: 
                     35: # accept texxml document on standard in
                     36: my $p = HTML::TokeParser->new( $ARGV[0] );
1.4       albertel   37: my $dirprefix = "../../loncom/html/adm/help/tex/";
1.1       bowersj2   38: 
1.5       bowersj2   39: # Make myself a temp dir for processing POD
                     40: my $tmpdir = File::Temp::tempdir('loncapahelpgenXXXXXXX', TMPDIR => 1);
                     41: 
1.1       bowersj2   42: # Print the header
                     43: open (LATEX_FILE, $dirprefix . "Latex_Header.tex");
                     44: print <LATEX_FILE>;
                     45: 
                     46: while (my $token = $p->get_token())
                     47: {
                     48:     my $type = $token->[0];
1.5       bowersj2   49:     if ($type eq 'S') {
1.1       bowersj2   50: 	my $tag = $token->[1];
                     51: 	my $attr = $token->[2];
1.5       bowersj2   52: 	if ($tag eq 'section') {
1.1       bowersj2   53: 	    my $title = $attr->{'name'};
                     54: 	    print "\\section{$title}\n\n";
                     55: 	}
                     56: 
1.5       bowersj2   57: 	if ($tag eq 'subsection') {
1.1       bowersj2   58: 	    my $title = $attr->{'name'};
                     59: 	    print "\\subsection{$title}\n\n";
                     60: 	}
                     61: 
1.5       bowersj2   62: 	if ($tag eq 'subsubsection') {
1.1       bowersj2   63: 	    my $title = $attr->{'name'};
                     64: 	    print "\\subsubsection{$title}\n\n";
                     65: 	}
                     66: 
1.5       bowersj2   67: 	if ($tag eq 'file') {
1.1       bowersj2   68: 	    my $file = $attr->{'name'};
                     69: 	    open (LATEX_FILE, $dirprefix . $file);
                     70: 	    print <LATEX_FILE>;
1.3       bowersj2   71: 	    print "\n\n";
1.1       bowersj2   72: 	}
                     73: 
1.5       bowersj2   74: 	if ($tag eq 'tex') {
1.3       bowersj2   75: 	    print "\n\n";
1.1       bowersj2   76: 	    print $attr->{'content'};
1.3       bowersj2   77: 	    print "\n\n";
1.1       bowersj2   78: 	}
1.5       bowersj2   79: 
                     80: 	if ($tag eq 'pod') {
                     81: 	    my $file = $attr->{'file'};
                     82: 	    my $section = $attr->{'section'};
                     83: 	    if (!defined($section)) { $section = ''; }
1.6       bowersj2   84: 	    else { 
                     85: 		$section = "-section $section";
                     86: 		# Escape the pipes so they are considered ORs in the
                     87: 		# RE for podselect's "section" option, and not 
                     88: 		# pipes by the shell:
                     89: 		$section =~ s/\|/\\\|/g;
                     90: 	    }
1.5       bowersj2   91: 	    $file = '../../loncom/' . $file;
                     92: 	    my $tempfile = 't' . substr($file, rindex($file, '/') + 1);
                     93: 	    system ("cp $file $tmpdir");
                     94: 	    # The "echo" command is necessary; pod2latex can't
                     95: 	    # handle a perl file that *starts* with pod.
1.6       bowersj2   96: 	    system ("echo > $tmpdir/$tempfile; cat $file | podselect $section >> $tmpdir/$tempfile; cd $tmpdir; pod2latex -h1level 2 $tempfile");
1.5       bowersj2   97: 	    my $latexFile = substr($tempfile, 0, rindex($tempfile, '.')) . '.tex';
                     98: 	    open LATEX_FILE, $tmpdir . '/' . $latexFile;
1.7     ! bowersj2   99: 	    # pod2latex inserts \labels and \indexs for every section,
        !           100: 	    # which is horrible because the section names tend to get
        !           101: 	    # reused a lot. This filters those out, so we need to do
        !           102: 	    # create our own indexes.
        !           103: 	    for (<LATEX_FILE>) {
        !           104: 		$_ =~ s/\\([^{]*)section(\*?)\{([^\\]+)\\label\{[^\\]+\}\\index\{([^\\]+)\}\}/\\\1section\2\{\3\}/g;
        !           105: 		print $_;
        !           106: 	    }
1.5       bowersj2  107: 	    print "\n\n";
                    108: 	}
1.1       bowersj2  109:     }
                    110: }
                    111: 
                    112: # Print out the footer.
                    113: open (LATEX_FILE, $dirprefix . "Latex_Footer.tex");
                    114: print <LATEX_FILE>;
1.5       bowersj2  115: 
                    116: # Remove the temp directory
                    117: system ("rm -rf $tmpdir");

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>