File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.21: download - view: text, annotated - select for diffs
Wed Apr 10 16:21:33 2002 UTC (22 years, 7 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
this is a very very first and bad attempt to work with dynamically produced eps

    1: # The LearningOnline Network
    2: # Printout
    3: #
    4: # $Id: lonprintout.pm,v 1.21 2002/04/10 16:21:33 sakharuk Exp $
    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: # (Internal Server Error Handler
   29: #
   30: # (Login Screen
   31: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
   32: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
   33: #
   34: # 3/1/1 Gerd Kortemeyer)
   35: #
   36: # 3/1 Gerd Kortemeyer
   37: #
   38: # 9/17 Alex Sakharuk
   39: #
   40: package Apache::lonprintout;
   41: 
   42: use strict;
   43: use Apache::Constants qw(:common :http);
   44: use Apache::lonxml;
   45: use Apache::lonnet;
   46: use Apache::inputtags;
   47: use Apache::edit;
   48: use Apache::File();
   49: 
   50: 
   51: 
   52: sub headerform {
   53:     my $r = shift;
   54:     $r->print(<<ENDHEADER);
   55: <html>
   56: <head>
   57: <title>LON-CAPA output for printing</title>
   58: </head>
   59: <body bgcolor="FFFFFF">
   60: <form method="post" enctype="multipart/form-data" action="/adm/printout" name="printform">
   61: <h1>What do you want to print? Make a choice.</h1><br />
   62: ENDHEADER
   63: }
   64: 
   65: 
   66: sub menu_for_output {
   67:     my $r = shift;
   68:     $r->print(<<ENDMENUOUT);
   69: <input type="hidden" name="phase" value="two">
   70: <input type="hidden" name="url" value="$ENV{'form.postdata'}">
   71: <input type="radio" name="choice" value="Standard LaTeX output for current document">  Current document
   72: (you will print what you see on the screen)<br />
   73: <input type="radio" name="choice" value="Standard LaTeX output for the primary sequence">  All problems from the primary sequence<br />
   74: <input type="radio" name="choice" value="Standard LaTeX output for whole primary sequence">  The whole primary sequence (problems plus all html and xml files)<br />
   75: <input type="radio" name="choice" value="Standard LaTeX output for the top level sequence">  All problems from the top level sequence<br />
   76: <br /><hr /><br />
   77: <h1>And what page format do you prefer?</h1>
   78: <input type="radio" name="layout" value="CBI" checked>  CBI <br />
   79: <input type="radio" name="layout" value="CAPA"> CAPA <br />
   80: <input type="submit" value="Submit your choice">
   81: </form>
   82: </body>
   83: </html>
   84: ENDMENUOUT
   85: }
   86: 
   87: 
   88: 
   89: 
   90: sub output_data {
   91:     my $r = shift;
   92:     $r->print(<<ENDPART);
   93: <html>
   94: <head>
   95: <title>LON-CAPA output for printing</title>
   96: </head>
   97: <body bgcolor="FFFFFF">
   98: <hr>
   99: ENDPART
  100: 
  101:     my $choice = $ENV{'form.choice'};
  102:     my $layout = $ENV{'form.layout'};
  103:     my $laystyle = 'book';
  104:     my $result = '';
  105:     my %mystyle;
  106:     my $filename;
  107: 
  108:     if ($choice eq 'Standard LaTeX output for current document') {
  109:       my %moreenv;
  110:       my $currequest=$ENV{'request.filename'};
  111:       $moreenv{'form.grade_target'}='tex';
  112:       $moreenv{'request.filename'}=$ENV{'form.url'};
  113:       &Apache::lonnet::appenv(%moreenv);
  114:       my $texversion=&Apache::lonnet::ssi($ENV{'form.url'});
  115:       &Apache::lonnet::delenv('form.grade_target');
  116:       %moreenv = ();
  117:       $moreenv{'request.filename'}=$currequest;
  118:       &Apache::lonnet::appenv(%moreenv);
  119:       $result .= $texversion;
  120: 
  121:     } elsif ($choice eq 'Standard LaTeX output for the primary sequence' or $choice eq 'Standard LaTeX output for whole primary sequence') {
  122: #-- where is the primary sequence containing file?
  123: 	my %moreenv;
  124: 	my $symbolic = &Apache::lonnet::symbread($ENV{'form.url'});
  125: 	$_ = $symbolic;
  126: 	m/([^_]+)_/;
  127: 	my $primary_sequence = '/res/'.$1;
  128: #-- open and analyses the primary sequence
  129: 	my $sequence_file=&Apache::lonnet::filelocation("",$primary_sequence);
  130: 	my $sequencefilecontents=&Apache::lonnet::getfile($sequence_file);
  131: 	my @master_seq = &content_map($sequencefilecontents);
  132: #-- produce an output string
  133: 	for (my $i=0;$i<=$#master_seq;$i++) {
  134: 	    $_ = $master_seq[$i];
  135: 	    m/\"(.*)\"/;
  136:             $_ = $1;
  137:             my $urlp = $1;
  138: 	    if ($choice eq 'Standard LaTeX output for the primary sequence') {
  139: 		if (/\.(problem|exam|quiz|assess|survey|form|library)/) {
  140: 		    my %moreenv;
  141: 		    $moreenv{'form.grade_target'}='tex';
  142: 		    &Apache::lonnet::appenv(%moreenv);
  143: 		    my $texversion=&Apache::lonnet::ssi($urlp);
  144: 		    &Apache::lonnet::delenv('form.grade_target');
  145: 		    $result .= $texversion;        
  146: 		}
  147: 	    } else {
  148: 		$moreenv{'form.grade_target'}='tex';
  149: 		&Apache::lonnet::appenv(%moreenv);
  150: 		my $texversion=&Apache::lonnet::ssi($urlp);
  151: 		&Apache::lonnet::delenv('form.grade_target');
  152: 		$result .= $texversion;    
  153: 	    }
  154: 	}
  155: #-- additional cleanup for output	
  156: 	my $first_app = index($result,'\documentclass',0);
  157: 	$first_app = index($result,'\documentclass',$first_app+5);
  158: 	while ($first_app != -1) {
  159: 	    my $second_app = index($result,'begin{document}',$first_app);
  160: 	    $first_app = rindex($result,'\end{document}',$first_app);
  161: 	    substr($result,$first_app,$second_app-$first_app+15) = '\vskip 3 mm';
  162: 	    $first_app = index($result,'\documentclass',$first_app+5);
  163: 	}
  164:     }  elsif ($choice eq 'Standard LaTeX output for the top level sequence') {
  165: 	my @master_seq = ();
  166: 	my @add_file_seq = ();
  167: #-- where is the main sequence of the course?
  168: 	my $main_seq = '/res/'.$ENV{'request.course.uri'};
  169: 	my $file=&Apache::lonnet::filelocation("",$main_seq);
  170: 	my $filecontents=&Apache::lonnet::getfile($file);
  171: 	my @file_seq = &content_map($filecontents);
  172: #-- do we have any other sequence inside?
  173: 	my $i=0;
  174: 	while ($i<=$#file_seq) {
  175: 	    $_ = $file_seq[$i];
  176: 	    if (/\.sequence$/) {
  177: 		$file = &Apache::lonnet::filelocation("",$file_seq[$i]);
  178: 		$filecontents=&Apache::lonnet::getfile($file);
  179: 		@add_file_seq = &content_map($filecontents);
  180: 		splice(@file_seq,$i,1,@add_file_seq);
  181: 		@add_file_seq = ();
  182: 		$i = -1;
  183: 	    }
  184: 	    $i++;
  185: 	}
  186: 	@master_seq = @file_seq;	
  187: #-- produce an output string
  188: 	for (my $i=0;$i<=$#master_seq;$i++) {
  189: 	    $_ = $master_seq[$i];
  190: 	    m/\"(.*)\"/;
  191:             $_ = $1;
  192:             my $urlp = $1;
  193:             if (/\.(problem|exam|quiz|assess|survey|form|library)/) {
  194: 		my %moreenv;
  195: 		$moreenv{'form.grade_target'}='tex';
  196: 		&Apache::lonnet::appenv(%moreenv);
  197: 		my $texversion=&Apache::lonnet::ssi($urlp);
  198: 		&Apache::lonnet::delenv('form.grade_target');
  199: 		$result .= $texversion;        
  200: 	    }
  201: 	}
  202: #-- additional cleanup for output	
  203: 	my $first_app = index($result,'\documentclass',0);
  204: 	$first_app = index($result,'\documentclass',$first_app+5);
  205: 	while ($first_app != -1) {
  206: 	    my $second_app = index($result,'begin{document}',$first_app);
  207: 	    $first_app = rindex($result,'\end{document}',$first_app);
  208: 	    substr($result,$first_app,$second_app-$first_app+15) = '\vskip 3 mm';
  209: 	    $first_app = index($result,'\documentclass',$first_app+5);
  210: 	}
  211:     }
  212: #-- corrections for the different page formats
  213:     if ($layout eq 'CBI') {
  214: 	$result =~ s/\\begin{document}/\\setlength{\\oddsidemargin}{-40pt}\\setlength{\\evensidemargin}{-60pt}\\setlength{\\topmargin}{200pt}\\setlength{\\textwidth}{4\.4in}\\setlength{\\textheight}{6\.8in}\\setlength{\\parindent}{20pt}\\setlength{\\marginparwidth}{90pt}\\setlength{\\textfloatsep}{8pt plus 2\.0pt minus 4\.0pt} \\begin{document}/;
  215:         $laystyle = 'album';
  216:     } elsif ($layout eq 'CAPA') {
  217:         my $courseidinfo = $ENV{'request.role'};
  218:         $_ = $courseidinfo;
  219:         m/.*\/(.*)/;
  220:         $courseidinfo = $ENV{'course.physnet_'.$1.'.description'};
  221: 	$result =~ s/\\documentclass\[letterpaper\]{article}/\\documentclass\[twocolumn\]{article}/;
  222: 	$result =~ s/\\begin{document}/\\textheight 25\.9cm\\oddsidemargin = -0\.57in\\evensidemargin = -0\.57in\\textwidth= 7\.7in\\begin{document}\\voffset=-1\.8cm\\setcounter{page}{1}\\noindent\\fbox{\\textbf{$ENV{'environment.firstname'} $ENV{'environment.lastname'}}}\\hskip 1\.4in $courseidinfo \\vskip 5 mm /;
  223: 	$result =~ s/\\includegraphics/\\includegraphics\[width=9\.0 cm\]/g;
  224: 	$result =~ s/(\\end{document})/\\newline\\noindent\\makebox\[9.0cm\]\[b\]{\\hrulefill}\\newline\\noindent\\tiny Dept\. of Physics and Astronomy, MSU\\makebox\[1.5cm\]\[b\]{\\hfill}LON-CAPA\\copyright MSU GNU\/GPS $1/;
  225:     }
  226: #-- LaTeX corrections 
  227:     $result =~ s/^\s+$//gm; #remove empty lines
  228:     $result =~ s/%/\\%/g;   #corrects %
  229:     $result =~ s/(\s)+/$1/g; #removes more than one empty space
  230:     $result =~ s/\\\\\s*(\\vskip)/ $1/gm;
  231: #-- Do all of eps figures exist?
  232:     my $notepslist = '';
  233:     my $end_eps = index($result,'.eps}',0);
  234:     while ($end_eps != -1) {
  235: 	my $start_eps = rindex($result,'{',$end_eps);
  236:         my $start_path = rindex($result,'\graphicspath{',$start_eps);
  237:         my $end_path = index($result,'}',$start_path);
  238: 	my $chunk_eps = substr($result,$start_eps+1,$end_eps-$start_eps+3);
  239: 	my $chunk_path = substr($result,$start_path+14,$end_path-$start_path-14);
  240: 	$chunk_path =~ s/^{*//g;
  241: 	unless (-s $chunk_path.$chunk_eps) {
  242: 	    $notepslist .= $chunk_path.$chunk_eps;
  243: 	}
  244: 	 $end_eps = index($result,'.eps}',$end_eps+5);
  245:     }
  246: #-- writing .tex file in prtspool 
  247:     my $temp_file;
  248:     $filename = "/home/httpd/prtspool/$ENV{'user.name'}$ENV{'user.domain'}temp$ENV{'user.login.time'}.tex";
  249:     unless ($temp_file = Apache::File->new('>'.$filename)) {
  250: 	$r->log_error("Couldn't open $filename for output $!");
  251: 	return SERVER_ERROR; 
  252:     } 
  253:     print $temp_file $result;
  254: $r->print(<<FINALEND);
  255: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename&$laystyle&$notepslist">
  256: </body>
  257: </html>
  258: FINALEND
  259: }
  260: 
  261: 
  262: 
  263: 
  264: sub content_map {
  265: #-- find a list of files to print
  266:     my $map_string = shift;
  267:     my @number_seq = ();
  268:     my @file_seq = ();
  269:     my $startlink = index($map_string,'<link',0);
  270:     my $endlink = index($map_string,'</link>',$startlink);
  271:     my $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
  272:     $_ = $chunk;
  273:     m/from=\"(\d+)\"/;
  274:     push @number_seq,$1;
  275:     while ($startlink != -1) {
  276: 	$endlink = index($map_string,'</link>',$startlink);
  277: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+7);
  278: 	substr($map_string,$startlink,$endlink-$startlink+7) = '';
  279: 	$_ = $chunk;
  280:         m/to=\"(\d+)\"/;
  281: 	push @number_seq,$1;
  282: 	$startlink = index($map_string,'from="'.$1.'"',$startlink);
  283: 	$startlink = rindex($map_string,'<link ',$startlink);
  284:     }
  285:     my $stalink = index($map_string,' to="'.$number_seq[0].'"',$startlink);
  286:     while ($stalink != -1) {
  287: 	$startlink = rindex($map_string,'<link ',$stalink);
  288: 	$endlink = index($map_string,'</link>',$startlink);
  289: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+7);
  290: 	substr($map_string,$startlink,$endlink-$startlink+7) = '';
  291: 	$_ = $chunk;
  292:         m/from=\"(\d+)\"/;
  293: 	unshift @number_seq,$1;
  294: 	$stalink = index($map_string,' to="'.$number_seq[0].'"',0);
  295:     }
  296:     for (my $i=0;$i<=$#number_seq;$i++) {
  297: 	$stalink = index($map_string,' id="'.$number_seq[$i].'"',0);
  298:         {    
  299: 	    my $ahed1 = index($map_string,'src="',$stalink);
  300: 	    my $ahed2 = index($map_string,'</resource>',$stalink);
  301: 	    if ($ahed1 != -1) {
  302: 		if ($ahed1 < $ahed2) {
  303: 		    $startlink = $ahed1;
  304: 		} else {
  305: 		    $startlink = rindex($map_string,'src="',$stalink);
  306: 		}
  307: 	    } else {
  308: 		$startlink = rindex($map_string,'src="',$stalink);
  309: 	    }
  310: 
  311: 	}
  312: 	$startlink = index($map_string,'"',$startlink);
  313: 	$endlink = index($map_string,'"',$startlink+1);
  314: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+1);
  315: 	push @file_seq,$chunk;
  316:     }
  317:     return @file_seq;
  318: }
  319: 
  320: 
  321: 
  322: sub handler {
  323: 
  324:     my $r = shift;
  325:     $r->content_type('text/html');
  326:     $r->send_http_header;
  327: 
  328: #-- start form
  329:     &headerform($r);
  330: #-- menu for output
  331:     unless  ($ENV{'form.phase'}) {
  332: 	&menu_for_output($r);
  333:     }
  334: 
  335: 
  336: #-- core part 
  337:     if ($ENV{'form.phase'} eq 'two') {
  338: 	&output_data($r);
  339: 
  340:     }
  341:     return OK;
  342: 
  343: } 
  344: 
  345: 1;
  346: __END__
  347: 
  348: 
  349: 
  350: 
  351: #### Test block
  352: #    my $ere;
  353: #    foreach $ere (%ENV) {
  354: #	$result .= ' SS '.$ere.' => '.$ENV{$ere}.' FF '."\n\n";
  355: #    }
  356: ####

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