File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.4: download - view: text, annotated - select for diffs
Fri Aug 9 14:48:31 2002 UTC (21 years, 10 months ago) by bowersj2
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, version_0_5_1, version_0_5, HEAD
This should be all the help information necessary for the next stable
release.

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

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