Annotation of loncom/interface/lonprintout.pm, revision 1.33
1.1 www 1: # The LearningOnline Network
2: # Printout
3: #
1.33 ! sakharuk 4: # $Id: lonprintout.pm,v 1.32 2002/06/20 17:50:24 sakharuk Exp $
1.11 albertel 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: #
1.1 www 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: #
1.3 sakharuk 38: # 9/17 Alex Sakharuk
39: #
1.1 www 40: package Apache::lonprintout;
41:
42: use strict;
1.10 albertel 43: use Apache::Constants qw(:common :http);
1.2 sakharuk 44: use Apache::lonxml;
45: use Apache::lonnet;
1.13 sakharuk 46: use Apache::inputtags;
47: use Apache::edit;
1.5 sakharuk 48: use Apache::File();
1.1 www 49:
1.3 sakharuk 50:
51:
52: sub headerform {
1.1 www 53: my $r = shift;
1.3 sakharuk 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">
1.16 sakharuk 61: <h1>What do you want to print? Make a choice.</h1><br />
1.3 sakharuk 62: ENDHEADER
63: }
64:
1.1 www 65:
1.3 sakharuk 66: sub menu_for_output {
67: my $r = shift;
1.28 sakharuk 68: $r->print(<<ENDMENUOUT1);
1.3 sakharuk 69: <input type="hidden" name="phase" value="two">
1.10 albertel 70: <input type="hidden" name="url" value="$ENV{'form.postdata'}">
1.25 sakharuk 71: <input type="radio" name="choice" value="Standard LaTeX output for current document" checked> Current document
1.16 sakharuk 72: (you will print what you see on the screen)<br />
1.19 sakharuk 73: <input type="radio" name="choice" value="Standard LaTeX output for the primary sequence"> All problems from the primary sequence<br />
1.20 sakharuk 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 />
1.19 sakharuk 75: <input type="radio" name="choice" value="Standard LaTeX output for the top level sequence"> All problems from the top level sequence<br />
1.28 sakharuk 76: <br />
77: ENDMENUOUT1
1.31 sakharuk 78: my $subdirtoprint = &Apache::lonnet::filelocation("",$ENV{'form.url'});
79: $subdirtoprint =~ s/\/[^\/]+$//;
80: if (&Apache::lonnet::allowed('bre',$subdirtoprint) eq 'F') {
1.28 sakharuk 81: $r->print(<<ENDMENUOUT2);
1.31 sakharuk 82: <input type="radio" name="choice" value="Subdirectory print"> All problems from current subdirectory (where this particular problem is)<br />
1.28 sakharuk 83: ENDMENUOUT2
1.31 sakharuk 84: }
85: $r->print(<<ENDMENUOUT3);
1.16 sakharuk 86: <br /><hr /><br />
87: <h1>And what page format do you prefer?</h1>
1.28 sakharuk 88: <input type="radio" name="layout" value="CBI"> Two columns landscape <br />
89: <input type="radio" name="layout" value="CAPA" checked> Two columns portrait <br />
1.16 sakharuk 90: <input type="submit" value="Submit your choice">
1.3 sakharuk 91: </form>
92: </body>
93: </html>
1.28 sakharuk 94: ENDMENUOUT3
1.3 sakharuk 95: }
1.2 sakharuk 96:
97:
1.16 sakharuk 98:
99:
1.3 sakharuk 100: sub output_data {
101: my $r = shift;
102: $r->print(<<ENDPART);
103: <html>
104: <head>
105: <title>LON-CAPA output for printing</title>
106: </head>
107: <body bgcolor="FFFFFF">
108: <hr>
109: ENDPART
1.2 sakharuk 110:
1.3 sakharuk 111: my $choice = $ENV{'form.choice'};
1.16 sakharuk 112: my $layout = $ENV{'form.layout'};
113: my $laystyle = 'book';
1.2 sakharuk 114: my $result = '';
1.5 sakharuk 115:
1.3 sakharuk 116: if ($choice eq 'Standard LaTeX output for current document') {
1.14 albertel 117: my %moreenv;
118: $moreenv{'form.grade_target'}='tex';
119: $moreenv{'request.filename'}=$ENV{'form.url'};
120: &Apache::lonnet::appenv(%moreenv);
121: my $texversion=&Apache::lonnet::ssi($ENV{'form.url'});
122: &Apache::lonnet::delenv('form.grade_target');
123: $result .= $texversion;
1.29 sakharuk 124: $result = &additional_cleanup($result);
1.33 ! sakharuk 125: if ($ENV{'form.url'}=~m/\.page\s*$/) {$result = &page_cleanup($result);}
1.20 sakharuk 126: } elsif ($choice eq 'Standard LaTeX output for the primary sequence' or $choice eq 'Standard LaTeX output for whole primary sequence') {
1.18 sakharuk 127: #-- where is the primary sequence containing file?
128: my %moreenv;
129: my $symbolic = &Apache::lonnet::symbread($ENV{'form.url'});
130: $_ = $symbolic;
131: m/([^_]+)_/;
132: my $primary_sequence = '/res/'.$1;
133: #-- open and analyses the primary sequence
134: my $sequence_file=&Apache::lonnet::filelocation("",$primary_sequence);
135: my $sequencefilecontents=&Apache::lonnet::getfile($sequence_file);
136: my @master_seq = &content_map($sequencefilecontents);
137: #-- produce an output string
138: for (my $i=0;$i<=$#master_seq;$i++) {
139: $_ = $master_seq[$i];
140: m/\"(.*)\"/;
141: $_ = $1;
142: my $urlp = $1;
1.20 sakharuk 143: if ($choice eq 'Standard LaTeX output for the primary sequence') {
1.21 sakharuk 144: if (/\.(problem|exam|quiz|assess|survey|form|library)/) {
1.20 sakharuk 145: my %moreenv;
146: $moreenv{'form.grade_target'}='tex';
147: &Apache::lonnet::appenv(%moreenv);
148: my $texversion=&Apache::lonnet::ssi($urlp);
149: &Apache::lonnet::delenv('form.grade_target');
150: $result .= $texversion;
151: }
152: } else {
1.18 sakharuk 153: $moreenv{'form.grade_target'}='tex';
154: &Apache::lonnet::appenv(%moreenv);
155: my $texversion=&Apache::lonnet::ssi($urlp);
156: &Apache::lonnet::delenv('form.grade_target');
1.20 sakharuk 157: $result .= $texversion;
1.16 sakharuk 158: }
1.28 sakharuk 159: }
160: $result = &additional_cleanup($result);
1.13 sakharuk 161: } elsif ($choice eq 'Standard LaTeX output for the top level sequence') {
162: my @master_seq = ();
1.19 sakharuk 163: my @add_file_seq = ();
1.16 sakharuk 164: #-- where is the main sequence of the course?
1.13 sakharuk 165: my $main_seq = '/res/'.$ENV{'request.course.uri'};
166: my $file=&Apache::lonnet::filelocation("",$main_seq);
167: my $filecontents=&Apache::lonnet::getfile($file);
168: my @file_seq = &content_map($filecontents);
1.19 sakharuk 169: #-- do we have any other sequence inside?
170: my $i=0;
171: while ($i<=$#file_seq) {
172: $_ = $file_seq[$i];
173: if (/\.sequence$/) {
174: $file = &Apache::lonnet::filelocation("",$file_seq[$i]);
175: $filecontents=&Apache::lonnet::getfile($file);
176: @add_file_seq = &content_map($filecontents);
177: splice(@file_seq,$i,1,@add_file_seq);
178: @add_file_seq = ();
179: $i = -1;
180: }
181: $i++;
182: }
183: @master_seq = @file_seq;
1.13 sakharuk 184: #-- produce an output string
185: for (my $i=0;$i<=$#master_seq;$i++) {
186: $_ = $master_seq[$i];
187: m/\"(.*)\"/;
188: $_ = $1;
189: my $urlp = $1;
1.21 sakharuk 190: if (/\.(problem|exam|quiz|assess|survey|form|library)/) {
1.13 sakharuk 191: my %moreenv;
192: $moreenv{'form.grade_target'}='tex';
193: &Apache::lonnet::appenv(%moreenv);
194: my $texversion=&Apache::lonnet::ssi($urlp);
195: &Apache::lonnet::delenv('form.grade_target');
196: $result .= $texversion;
197: }
1.28 sakharuk 198: }
199: $result = &additional_cleanup($result);
1.31 sakharuk 200: } elsif ($choice eq 'Subdirectory print') {
201: my $subdirtoprint = &Apache::lonnet::filelocation("",$ENV{'form.url'});
202: $subdirtoprint =~ s/\/[^\/]+$//;
203: my @list_of_files = ();
204: my $localdirectory = $subdirtoprint;
205: $localdirectory =~ s/.*(\/res\/)/$1/;
206: my @content_directory = &Apache::lonnet::dirlist($localdirectory);
207: for (my $iy=0;$iy<=$#content_directory;$iy++) {
208: my @tempo_array = split(/&/,$content_directory[$iy]);
209: $content_directory[$iy] = $tempo_array[0];
210: if ($content_directory[$iy] =~ m/^[^\.]+\.problem$/) {
211: push @list_of_files,$content_directory[$iy];
1.28 sakharuk 212: }
1.13 sakharuk 213: }
1.31 sakharuk 214: $localdirectory =~ s/\/$//;
215: for (my $i=0;$i<=$#list_of_files;$i++) {
216: my $urlp = $localdirectory.'/'.$list_of_files[$i];
217: my %moreenv;
218: $moreenv{'form.grade_target'}='tex';
219: &Apache::lonnet::appenv(%moreenv);
220: my $texversion=&Apache::lonnet::ssi($urlp);
221: &Apache::lonnet::delenv('form.grade_target');
222: $texversion =~ s/(\\begin{document})/$1 {\\tiny\\begin{verbatim}$urlp\\end{verbatim}}/;
223: $result .= $texversion;
224: }
225: $result = &additional_cleanup($result);
226:
227:
1.28 sakharuk 228:
229:
230:
1.31 sakharuk 231:
1.7 sakharuk 232: }
1.16 sakharuk 233: #-- corrections for the different page formats
234: if ($layout eq 'CBI') {
235: $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}/;
236: $laystyle = 'album';
237: } elsif ($layout eq 'CAPA') {
238: my $courseidinfo = $ENV{'request.role'};
239: $_ = $courseidinfo;
240: m/.*\/(.*)/;
241: $courseidinfo = $ENV{'course.physnet_'.$1.'.description'};
242: $result =~ s/\\documentclass\[letterpaper\]{article}/\\documentclass\[twocolumn\]{article}/;
1.32 sakharuk 243: $result =~ s/\\begin{document}/\\textheight 25\.9cm\\oddsidemargin = -0\.57in\\evensidemargin = -0\.57in\\textwidth= 7\.7in\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/2-0\.2in}\\renewcommand{\\ref}{\\keephidden\}\\begin{document}\\voffset=-1\.8cm\\setcounter{page}{1}\\noindent\\fbox{\\textbf{$ENV{'environment.firstname'} $ENV{'environment.lastname'}}}\\hskip 1\.4in $courseidinfo \\vskip 5 mm /;
1.27 sakharuk 244: $result =~ s/\\includegraphics{/\\includegraphics\[width=9\.0 cm\]{/g;
1.25 sakharuk 245: $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\/GPL $1/;
1.16 sakharuk 246: }
1.23 sakharuk 247: #-- LaTeX corrections
248: my $first_comment = index($result,'<!--',0);
249: while ($first_comment != -1) {
250: my $end_comment = index($result,'-->',$first_comment);
251: substr($result,$first_comment,$end_comment-$first_comment+3) = '';
252: $first_comment = index($result,'<!--',$first_comment);
253: }
1.17 sakharuk 254: $result =~ s/^\s+$//gm; #remove empty lines
255: $result =~ s/%/\\%/g; #corrects %
1.20 sakharuk 256: $result =~ s/(\s)+/$1/g; #removes more than one empty space
1.23 sakharuk 257: $result =~ s/\\\\\s*\\vskip/\\vskip/gm;
1.22 sakharuk 258: $result =~ s/ (<|>|) / \$$1\$ /g; #corrects < or >
1.24 sakharuk 259: $result =~ s/\\\\\s*\\noindent\s*(\\\\)+/\\\\\\noindent /g;
1.23 sakharuk 260: $result =~ s/{\\par }\s*\\\\/\\\\/gm;
1.24 sakharuk 261: $result =~ s/\\\\\s+\[/ \[/g;
262: $result =~ s/θ/\$\\theta\$/g; #converts theta from html into tex
1.29 sakharuk 263: $result =~ s/\b_+\b/\\makebox\[1 cm\]\[b\]{\\hrulefill}/g;
1.7 sakharuk 264: #-- writing .tex file in prtspool
1.16 sakharuk 265: my $temp_file;
1.33 ! sakharuk 266: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout_".time."_".rand(10000000).".tex";
1.16 sakharuk 267: unless ($temp_file = Apache::File->new('>'.$filename)) {
268: $r->log_error("Couldn't open $filename for output $!");
269: return SERVER_ERROR;
270: }
271: print $temp_file $result;
1.3 sakharuk 272: $r->print(<<FINALEND);
1.26 sakharuk 273: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename&$laystyle">
1.3 sakharuk 274: </body>
275: </html>
276: FINALEND
277: }
1.2 sakharuk 278:
1.28 sakharuk 279: sub additional_cleanup {
280: my $result = shift;
281: my $first_app = index($result,'\documentclass',0);
282: $first_app = index($result,'\documentclass',$first_app+5);
283: while ($first_app != -1) {
284: my $second_app = index($result,'begin{document}',$first_app);
285: $first_app = rindex($result,'\end{document}',$first_app);
286: substr($result,$first_app,$second_app-$first_app+15) = '\vskip 3 mm';
287: $first_app = index($result,'\documentclass',$first_app+5);
288: }
289: return $result;
290: }
1.33 ! sakharuk 291: sub page_cleanup {
! 292: my $result = shift;
! 293: $_ = $result;
! 294: m/\\end{document}(\d*)$/;
! 295: my $insert = '{';
! 296: for (my $id=1;$id<=$1;$id++) { $insert .='l'; }
! 297: $insert .= '}';
! 298: $result =~ s/(\\begin{tabular})INSERT/$1$insert/g;
! 299: $result =~ s/&\s*REMOVE\\\\/\\end{tabular}\\\\\\begin{tabular}$insert/g;
! 300: return $result;
! 301: }
1.6 sakharuk 302:
1.3 sakharuk 303: sub content_map {
1.7 sakharuk 304: #-- find a list of files to print
1.3 sakharuk 305: my $map_string = shift;
1.5 sakharuk 306: my @number_seq = ();
1.7 sakharuk 307: my @file_seq = ();
1.5 sakharuk 308: my $startlink = index($map_string,'<link',0);
1.7 sakharuk 309: my $endlink = index($map_string,'</link>',$startlink);
310: my $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
311: $_ = $chunk;
312: m/from=\"(\d+)\"/;
313: push @number_seq,$1;
1.5 sakharuk 314: while ($startlink != -1) {
1.7 sakharuk 315: $endlink = index($map_string,'</link>',$startlink);
316: $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
1.5 sakharuk 317: substr($map_string,$startlink,$endlink-$startlink+7) = '';
318: $_ = $chunk;
319: m/to=\"(\d+)\"/;
320: push @number_seq,$1;
1.13 sakharuk 321: $startlink = index($map_string,'from="'.$1.'"',$startlink);
1.18 sakharuk 322: $startlink = rindex($map_string,'<link ',$startlink);
1.5 sakharuk 323: }
1.7 sakharuk 324: my $stalink = index($map_string,' to="'.$number_seq[0].'"',$startlink);
325: while ($stalink != -1) {
326: $startlink = rindex($map_string,'<link ',$stalink);
327: $endlink = index($map_string,'</link>',$startlink);
328: $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
329: substr($map_string,$startlink,$endlink-$startlink+7) = '';
330: $_ = $chunk;
331: m/from=\"(\d+)\"/;
332: unshift @number_seq,$1;
333: $stalink = index($map_string,' to="'.$number_seq[0].'"',0);
334: }
335: for (my $i=0;$i<=$#number_seq;$i++) {
336: $stalink = index($map_string,' id="'.$number_seq[$i].'"',0);
1.13 sakharuk 337: {
338: my $ahed1 = index($map_string,'src="',$stalink);
339: my $ahed2 = index($map_string,'</resource>',$stalink);
340: if ($ahed1 != -1) {
341: if ($ahed1 < $ahed2) {
342: $startlink = $ahed1;
343: } else {
344: $startlink = rindex($map_string,'src="',$stalink);
345: }
346: } else {
347: $startlink = rindex($map_string,'src="',$stalink);
348: }
349:
350: }
1.7 sakharuk 351: $startlink = index($map_string,'"',$startlink);
352: $endlink = index($map_string,'"',$startlink+1);
353: $chunk = substr($map_string,$startlink,$endlink-$startlink+1);
354: push @file_seq,$chunk;
355: }
356: return @file_seq;
357: }
1.5 sakharuk 358:
1.3 sakharuk 359:
360:
361: sub handler {
362:
363: my $r = shift;
364: $r->content_type('text/html');
365: $r->send_http_header;
366:
367: #-- start form
368: &headerform($r);
369: #-- menu for output
1.16 sakharuk 370: unless ($ENV{'form.phase'}) {
1.3 sakharuk 371: &menu_for_output($r);
372: }
373: #-- core part
374: if ($ENV{'form.phase'} eq 'two') {
375: &output_data($r);
376: }
1.2 sakharuk 377: return OK;
378:
1.1 www 379: }
380:
381: 1;
382: __END__
1.6 sakharuk 383:
384:
1.13 sakharuk 385:
386:
387: #### Test block
1.6 sakharuk 388: # my $ere;
389: # foreach $ere (%ENV) {
1.13 sakharuk 390: # $result .= ' SS '.$ere.' => '.$ENV{$ere}.' FF '."\n\n";
1.6 sakharuk 391: # }
1.13 sakharuk 392: ####
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>