File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.2: download - view: text, annotated - select for diffs
Mon Jul 8 20:47:41 2002 UTC (22 years ago) by bowersj2
Branches: MAIN
CVS tags: HEAD
Changes for integration with FAQ-O-Matic. Actual graphic loaded for
links instead of the word "graphic". Path to html directory no longer
hardcoded. Links and image hrefs fully qualified now.

    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:     <!-- BEGIN -->
   60: HEADER
   61: 
   62:     $r->print($tex);
   63: 
   64: $r->print(<<FOOTER);
   65:     <!-- END -->
   66:     </body>
   67: </html>
   68: FOOTER
   69: }
   70: 
   71: # Render takes a tex fragment, transforms it for TtH, and returns the
   72: # HTML equivalent
   73: sub render 
   74: {
   75:     my ($tex, $docroot, $serverroot) = @_;
   76:     tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
   77: 
   78:     # This tells TtH what to do with captions, labels, and other
   79:     # things
   80:     $tex = "\\documentclass{article}\n" . $tex;
   81: 
   82:     # We process these ourselves because TtH can't handle then without
   83:     # LaTeX .aux files
   84:     # absolute paths for use with help.loncapa.org
   85:     $tex =~ s|  \\ref\{([^}]*)\}
   86:              |'\\begin{html}<a href="http://' . $serverroot ."/adm/help/".
   87:               substr($fragmentLabels{$1}, 0, -4) .
   88:               '.hlp#' . processLabelName($1) . 
   89:              '"><img src="http://' . $serverroot . '/adm/help/gif/smallHelp.gif" border="0" /></a>' .
   90:              '\\end{html}'
   91:              |gxe;
   92: 
   93:     # Figures leftover without captions
   94:     $tex =~ s|  \\includegraphics(\[[^]]*\])*\{([^}]*)\}
   95:              |  '\\begin{html}<img src="http://' . $serverroot . '/adm/help/gif/' . $2 . '.gif" border="2"'.
   96:                 ' bordercolor="#000000"/>\\end{html}'
   97:              |gxe;
   98: 
   99:     $tex = tth::tth($tex);
  100: 
  101:     # For some reason all captions come out as "Figure 0:", so
  102:     # just duck the issue...
  103: 
  104:     $tex =~ s/Figure 0://g;
  105: 
  106:     untie %fragmentLabels;
  107: 
  108:     return $tex;
  109: }
  110: 
  111: sub handler
  112: {
  113:      my $r = shift;
  114: 
  115:      my $docroot = $r->dir_config('lonDocRoot');
  116:      my $serverroot = $ENV{'HTTP_HOST'};
  117: 
  118:      my $filename = substr ($ENV{'REQUEST_URI'} , 
  119: 			    rindex($ENV{'REQUEST_URI'}, '/') + 1, -4);
  120:      
  121:      # Security check on the file; the whole filename must consist
  122:      # of nothing but alphanums, ' ,, or ., or the file
  123:      # will be "not found", no matter what.
  124:      
  125:      return 404 if ($filename !~ /\A[-0-9a-zA-z_'',.]+\Z/);
  126: 
  127:      (my $file = Apache::File->new($docroot . "/adm/help/tex/".$filename.'.tex'))
  128: 	 or return 404;
  129:      my $tex = join('', <$file>);
  130:      $tex = render($tex, $docroot, $serverroot);
  131:      $r->content_type("text/html");
  132:      serveTex($tex, $r);
  133: 
  134:      return OK;
  135: }
  136: 
  137: 1;

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