File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.19: download - view: text, annotated - select for diffs
Tue Jul 12 15:53:48 2005 UTC (18 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_X, version_2_1_3, version_2_1_2, version_2_1_1, version_2_1_0, version_2_0_X, version_2_0_99_1, version_2_0_2, version_2_0_1, version_2_0_0, version_1_99_3, version_1_99_2, HEAD
- style enforcements

    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);
   34: use Apache::File();
   35: use Apache::loncommon();
   36: use Apache::lonacc();
   37: use Apache::lontexconvert();
   38: use Apache::lonnavmaps; # for advancedUser
   39: use Apache::lonlocal;
   40: use Apache::lonnet;
   41: use tth();
   42: use GDBM_File();
   43: 
   44: # This sub takes the name of a label in, and converts it to something
   45: # that is a valid anchor name.
   46: sub processLabelName 
   47: {
   48:     my ($name) = @_;
   49:     $name =~ tr/a-zA-Z0-9/_/cs;
   50:     return $name;
   51: }
   52: 
   53: # Serve out the Tex
   54: sub serveTex
   55: {
   56:     my ($tex, $r) = @_;
   57:     my $html=&Apache::lonxml::xmlbegin();
   58:     my $bugs=&Apache::loncommon::help_open_bug('Documentation');
   59:     my $header=&mt('LON-CAPA Help');
   60:     my $footer=&mt('About LON-CAPA help and More Help');
   61:     $r->print(<<HEADER);
   62: $html
   63:     <head>
   64:         <title>LON-CAPA Help</title>
   65:     </head>
   66:     <body bgcolor="#FFFFFF">
   67:     <h3 style="font: sans-serif"><img align="right"
   68:     src="/adm/help/gif/lonhelpheader.gif"/>$header<hr />$bugs</h3>
   69:     <!-- BEGIN -->
   70: HEADER
   71: 
   72:     $r->print($tex);
   73: 
   74:     if (&Apache::lonnavmaps::advancedUser()) {
   75: $r->print(<<FOOTER);
   76:     <!-- END -->
   77:     <hr />
   78:     <center><font size="-1"><a href="/adm/help/abouthelp.html">$footer</a></font></center>
   79:     </body>
   80: </html>
   81: FOOTER
   82:     } else {
   83:     $r->print(<<FOOTER);
   84:     <!-- END -->
   85:     </body>
   86: </html>
   87: FOOTER
   88:     }
   89: }
   90: 
   91: # Render takes a tex fragment, transforms it for TtH, and returns the
   92: # HTML equivalent
   93: sub render 
   94: {
   95:     my ($tex, $docroot, $serverroot) = @_;
   96:     tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
   97: 
   98:     # This tells TtH what to do with captions, labels, and other
   99:     # things
  100:     $tex = "\\documentclass{article}\n" . $tex;
  101: 
  102:     # We process these ourselves because TtH can't handle then without
  103:     # LaTeX .aux files
  104:     # absolute paths for use with help.loncapa.org
  105:     $tex =~ s|  \\ref\{([^}]*)\}
  106:              |
  107:               if (not(exists($fragmentLabels{$1}))) {
  108: 	          &Apache::lonnet::logthis("ERROR: $1 not a valid help label");
  109:               };
  110:              '\\begin{html}<a href="http://' . $serverroot ."/adm/help/".
  111:               substr($fragmentLabels{$1}, 0, -4) .
  112:               '.hlp#' . processLabelName($1) . 
  113:              '"><img src="http://' . $serverroot . '/adm/help/gif/smallHelp.gif" border="0" /></a>' .
  114:              '\\end{html}'
  115:              |gxe;
  116: 
  117:     # Backslashes
  118:     $tex =~ s|\\textbackslash|###BACKSLASH###|g;
  119: 
  120:     # Figures leftover without captions
  121:     $tex =~ s|  \\includegraphics(\[[^]]*\])*\{([^}]*)\}
  122:              |  '\\begin{html}<img src="http://' . $serverroot . '/adm/help/gif/' . $2 . '.gif" border="2"'.
  123:                 ' bordercolor="#000000"/>\\end{html}'
  124:              |gxe;
  125: 
  126: 
  127:     $tex=&Apache::lontexconvert::tth_converted(\$tex);
  128:     
  129:     # Finish backslashes
  130:     $tex =~ s/###BACKSLASH###/'\\'/ge;
  131:  
  132:     # Fix the pretty quotes
  133:     $tex =~ s/('')|(``)/&quot;/g; #" to get emacs syntax highlighter happy
  134: 
  135:     # For some reason all captions come out as "Figure 0:", so
  136:     # just duck the issue...
  137: 
  138:     $tex =~ s/Figure 0://g;
  139: 	     $tex.=$Apache::lontexconvert::errorstring;
  140:     untie %fragmentLabels;
  141: 
  142:     return $tex;
  143: }
  144: 
  145: sub handler
  146: {
  147:      my $r = shift;
  148: 
  149:      my $docroot = $r->dir_config('lonDocRoot');
  150:      my $serverroot = $ENV{'HTTP_HOST'};
  151: 
  152:      my $filenames = &Apache::lonnet::unescape(substr ($ENV{'REQUEST_URI'} , 
  153: 			    rindex($ENV{'REQUEST_URI'}, '/') + 1, -4));
  154: 
  155:      # Security check on the file; the whole filename must consist
  156:      # of nothing but alphanums, ' ,, or ., or the file
  157:      # will be "not found", no matter what.
  158:      
  159:      return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
  160: 
  161:      &Apache::lonlocal::get_language_handle($r);
  162: 
  163:      # Join together the tex files, return HTTP_NOT_FOUND if any of
  164:      # them are not found
  165:      my $tex = '';
  166:      # Since in insertlist.tab I want to specify multiple files,
  167:      # and insertlist.tab also uses commas, I need something else
  168:      # so replace : with ,
  169:      $filenames =~ s/:/,/g;
  170:      my @files = split(/,/, $filenames);
  171:      
  172:      for my $filename (@files) {
  173: 	 if (-e $docroot.'/adm/help/tex/'.
  174: 	     &Apache::lonlocal::current_language().'/'.
  175: 	     $filename.'.tex') {
  176: 	     $filename=&Apache::lonlocal::current_language().'/'.$filename;
  177: 	 }
  178: 	 (my $file = Apache::File->new($docroot
  179: 			   . '/adm/help/tex/'.$filename.'.tex'))
  180: 	     or return HTTP_NOT_FOUND;
  181: 	 $tex .= join('', <$file>);
  182:      }
  183: 
  184:      if ($env{'browser.mathml'}) {
  185: 	 &Apache::loncommon::content_type($r,'text/xml');
  186: 	 &tth::ttminit();
  187: 	 if ($env{'browser.unicode'}) {
  188: 	     &tth::ttmoptions('-L -u1');
  189: 	 } else {
  190: 	     &tth::ttmoptions('-L -u0');
  191: 	 }
  192:      } else {
  193: 	 &Apache::loncommon::content_type($r,"text/html");
  194: 	 &tth::tthinit();
  195: 	 if ($env{'browser.unicode'}) {
  196: 	     &tth::tthoptions('-L -u1');
  197: 	 } else {
  198: 	     &tth::tthoptions('-L -u0');
  199: 	 }
  200:      }
  201: 
  202:      $r->send_http_header;
  203:      $tex = render($tex, $docroot, $serverroot);
  204:      serveTex($tex, $r);
  205: 
  206:      return OK;
  207: }
  208: 
  209: 1;

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