Annotation of loncom/interface/lonhelp.pm, revision 1.10

1.1       bowersj2    1: # The LearningOnline Network with CAPA
                      2: # .tex help system web server handler
                      3: #
                      4: # Copyright Michigan State University Board of Trustees
                      5: #
                      6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      7: #
                      8: # LON-CAPA is free software; you can redistribute it and/or modify
                      9: # it under the terms of the GNU General Public License as published by
                     10: # the Free Software Foundation; either version 2 of the License, or
                     11: # (at your option) any later version.
                     12: #
                     13: # LON-CAPA is distributed in the hope that it will be useful,
                     14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16: # GNU General Public License for more details.
                     17: #
                     18: # You should have received a copy of the GNU General Public License
                     19: # along with LON-CAPA; if not, write to the Free Software
                     20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     21: #
                     22: # /home/httpd/html/adm/gpl.txt
                     23: #
                     24: # http://www.lon-capa.org/
                     25: #
                     26: # .tex file help handler
                     27: # YEAR=2002
                     28: # 7/4 Jeremy Bowers
                     29: 
                     30: package Apache::lonhelp;
                     31: 
                     32: use strict;
                     33: use Apache::Constants qw(:common :http);
1.5       albertel   34: use Apache::File();
                     35: use Apache::loncommon();
                     36: use Apache::lonacc();
                     37: use Apache::lontexconvert();
1.9       bowersj2   38: use Apache::lonnavmaps; # for advancedUser
1.5       albertel   39: use tth();
                     40: use GDBM_File();
1.1       bowersj2   41: 
                     42: # This sub takes the name of a label in, and converts it to something
                     43: # that is a valid anchor name.
                     44: sub processLabelName 
                     45: {
                     46:     my ($name) = @_;
                     47:     $name =~ tr/a-zA-Z0-9/_/cs;
                     48:     return $name;
                     49: }
                     50: 
                     51: # Serve out the Tex
                     52: sub serveTex
                     53: {
                     54:     my ($tex, $r) = @_;
                     55: 
                     56: $r->print(<<HEADER);
                     57: <html>
                     58:     <head>
                     59:         <title>LON-CAPA Help</title>
                     60:     </head>
                     61:     <body bgcolor="#FFFFFF">
1.4       bowersj2   62:     <h3 style="font: sans-serif"><img align="right"
                     63:     src="/adm/help/gif/lonhelpheader.gif"/>LON-CAPA Help<hr /></h3>
1.2       bowersj2   64:     <!-- BEGIN -->
1.1       bowersj2   65: HEADER
                     66: 
                     67:     $r->print($tex);
                     68: 
1.9       bowersj2   69:     if (Apache::lonnavmaps::advancedUser()) {
1.1       bowersj2   70: $r->print(<<FOOTER);
1.2       bowersj2   71:     <!-- END -->
1.4       bowersj2   72:     <hr />
                     73:     <center><font size="-1"><a href="/adm/help/abouthelp.html">About
                     74:     LON-CAPA help and More Help</a></font></center>
1.1       bowersj2   75:     </body>
                     76: </html>
                     77: FOOTER
1.9       bowersj2   78: } else {
                     79:     $r->print(<<FOOTER);
                     80:     <!-- END -->
                     81:     </body>
                     82: </html>
                     83: FOOTER
                     84: }
1.1       bowersj2   85: }
                     86: 
                     87: # Render takes a tex fragment, transforms it for TtH, and returns the
                     88: # HTML equivalent
                     89: sub render 
                     90: {
1.2       bowersj2   91:     my ($tex, $docroot, $serverroot) = @_;
                     92:     tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
1.1       bowersj2   93: 
                     94:     # This tells TtH what to do with captions, labels, and other
                     95:     # things
                     96:     $tex = "\\documentclass{article}\n" . $tex;
                     97: 
                     98:     # We process these ourselves because TtH can't handle then without
                     99:     # LaTeX .aux files
1.2       bowersj2  100:     # absolute paths for use with help.loncapa.org
1.1       bowersj2  101:     $tex =~ s|  \\ref\{([^}]*)\}
1.2       bowersj2  102:              |'\\begin{html}<a href="http://' . $serverroot ."/adm/help/".
1.1       bowersj2  103:               substr($fragmentLabels{$1}, 0, -4) .
1.2       bowersj2  104:               '.hlp#' . processLabelName($1) . 
                    105:              '"><img src="http://' . $serverroot . '/adm/help/gif/smallHelp.gif" border="0" /></a>' .
                    106:              '\\end{html}'
1.1       bowersj2  107:              |gxe;
                    108: 
1.3       bowersj2  109:     # Backslashes
                    110:     $tex =~ s|\\textbackslash|###BACKSLASH###|g;
                    111: 
1.1       bowersj2  112:     # Figures leftover without captions
                    113:     $tex =~ s|  \\includegraphics(\[[^]]*\])*\{([^}]*)\}
1.2       bowersj2  114:              |  '\\begin{html}<img src="http://' . $serverroot . '/adm/help/gif/' . $2 . '.gif" border="2"'.
1.1       bowersj2  115:                 ' bordercolor="#000000"/>\\end{html}'
                    116:              |gxe;
                    117: 
1.3       bowersj2  118: 
1.5       albertel  119:     $tex=&Apache::lontexconvert::converted(\$tex);
                    120:     
1.3       bowersj2  121:     # Finish backslashes
                    122:     $tex =~ s/###BACKSLASH###/'\\'/ge;
                    123:  
                    124:     # Fix the pretty quotes
                    125:     $tex =~ s/('')|(``)/&quot;/g; #" to get emacs syntax highlighter happy
1.1       bowersj2  126: 
                    127:     # For some reason all captions come out as "Figure 0:", so
                    128:     # just duck the issue...
                    129: 
                    130:     $tex =~ s/Figure 0://g;
1.5       albertel  131: 	     $tex.=$Apache::lontexconvert::errorstring;
1.1       bowersj2  132:     untie %fragmentLabels;
                    133: 
                    134:     return $tex;
                    135: }
                    136: 
                    137: sub handler
                    138: {
                    139:      my $r = shift;
                    140: 
1.2       bowersj2  141:      my $docroot = $r->dir_config('lonDocRoot');
                    142:      my $serverroot = $ENV{'HTTP_HOST'};
                    143: 
1.6       bowersj2  144:      my $filenames = substr ($ENV{'REQUEST_URI'} , 
1.1       bowersj2  145: 			    rindex($ENV{'REQUEST_URI'}, '/') + 1, -4);
                    146:      
                    147:      # Security check on the file; the whole filename must consist
                    148:      # of nothing but alphanums, ' ,, or ., or the file
                    149:      # will be "not found", no matter what.
                    150:      
1.7       bowersj2  151:      return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
1.1       bowersj2  152: 
1.10    ! www       153:      &Apache::lonlocal::get_language_handle($r);
        !           154: 
1.6       bowersj2  155:      # Join together the tex files, return HTTP_NOT_FOUND if any of
                    156:      # them are not found
                    157:      my $tex = '';
1.7       bowersj2  158:      # Since in insertlist.tab I want to specify multiple files,
                    159:      # and insertlist.tab also uses commas, I need something else
1.8       bowersj2  160:      # so replace : with ,
1.7       bowersj2  161:      $filenames =~ s/:/,/g;
1.6       bowersj2  162:      my @files = split(/,/, $filenames);
                    163:      
                    164:      for my $filename (@files) {
1.10    ! www       165: 	 if (-e $docroot.'/adm/help/tex/'.$filename.'_'.
        !           166: 	     &Apache::lonlocal::current_language().'.tex') {
        !           167: 	     $filename.='_'.&Apache::lonlocal::current_language();
        !           168: 	 }
1.6       bowersj2  169: 	 (my $file = Apache::File->new($docroot
                    170: 			   . '/adm/help/tex/'.$filename.'.tex'))
                    171: 	     or return HTTP_NOT_FOUND;
                    172: 	 $tex .= join('', <$file>);
                    173:      }
1.5       albertel  174: 
                    175:      # get me my environment if it exists
                    176:      &Apache::lonacc::handler($r);
                    177: 
                    178:      if ($ENV{'browser.mathml'}) {
1.10    ! www       179: 	 &Apache::loncommon::content_type($r,'text/xml');
1.5       albertel  180: 	 &tth::ttminit();
                    181: 	 if ($ENV{'browser.unicode'}) {
                    182: 	     &tth::ttmoptions('-L -u1');
                    183: 	 } else {
                    184: 	     &tth::ttmoptions('-L -u0');
                    185: 	 }
                    186:      } else {
1.10    ! www       187: 	 &Apache::loncommon::content_type($r,"text/html");
1.5       albertel  188: 	 &tth::tthinit();
                    189: 	 if ($ENV{'browser.unicode'}) {
                    190: 	     &tth::tthoptions('-L -u1');
                    191: 	 } else {
                    192: 	     &tth::tthoptions('-L -u0');
                    193: 	 }
                    194:      }
                    195: 
1.2       bowersj2  196:      $tex = render($tex, $docroot, $serverroot);
1.1       bowersj2  197:      serveTex($tex, $r);
                    198: 
                    199:      return OK;
                    200: }
                    201: 
                    202: 1;

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