File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.6: download - view: text, annotated - select for diffs
Tue Jul 15 18:50:43 2003 UTC (20 years, 10 months ago) by bowersj2
Branches: MAIN
CVS tags: HEAD
Allow the joining together of various help files by seperating the names
with commas in the file name.

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

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