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

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

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