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

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);
1.5       albertel   34: use Apache::File();
                     35: use Apache::loncommon();
                     36: use Apache::lonacc();
                     37: use Apache::lontexconvert();
1.9       bowersj2   38: use Apache::lonnavmaps; # for advancedUser
1.11      albertel   39: use Apache::lonlocal;
1.18      albertel   40: use Apache::lonnet;
1.5       albertel   41: use tth();
                     42: use GDBM_File();
1.1       bowersj2   43: 
                     44: # This sub takes the name of a label in, and converts it to something
                     45: # that is a valid anchor name.
1.20    ! www        46: 
        !            47: sub processLabelName {
1.1       bowersj2   48:     my ($name) = @_;
                     49:     $name =~ tr/a-zA-Z0-9/_/cs;
                     50:     return $name;
                     51: }
                     52: 
1.20    ! www        53: # Serve out the text
        !            54: sub servetext {
        !            55:     my ($r,$text) = @_;
1.16      albertel   56:     my $html=&Apache::lonxml::xmlbegin();
1.20    ! www        57:     my $htmlend=&Apache::lonxml::xmlend();
1.14      www        58:     my $bugs=&Apache::loncommon::help_open_bug('Documentation');
                     59:     my $header=&mt('LON-CAPA Help');
1.16      albertel   60:     $r->print(<<HEADER);
                     61: $html
1.1       bowersj2   62:     <head>
                     63:         <title>LON-CAPA Help</title>
                     64:     </head>
                     65:     <body bgcolor="#FFFFFF">
1.4       bowersj2   66:     <h3 style="font: sans-serif"><img align="right"
1.14      www        67:     src="/adm/help/gif/lonhelpheader.gif"/>$header<hr />$bugs</h3>
1.2       bowersj2   68:     <!-- BEGIN -->
1.1       bowersj2   69: HEADER
                     70: 
1.20    ! www        71:     $r->print($text);
1.1       bowersj2   72: 
1.19      albertel   73:     if (&Apache::lonnavmaps::advancedUser()) {
1.20    ! www        74: 	my $search=&mt('Search LON-CAPA help');
        !            75: 	my $about=&mt('About LON-CAPA help and More Help');
        !            76: 	$r->print(<<FOOTER);
1.2       bowersj2   77:     <!-- END -->
1.20    ! www        78:     <hr /><form method="post">
        !            79: $search: <input type="text" name="searchterm" size="40" /><br />
        !            80: <a href="/adm/help/abouthelp.html">$about</a>
1.1       bowersj2   81:     </body>
1.20    ! www        82: $htmlend
1.1       bowersj2   83: FOOTER
1.20    ! www        84:     }
        !            85:     $r->print(<<ENDBODY);
1.9       bowersj2   86:     <!-- END -->
                     87:     </body>
1.20    ! www        88: $htmlend
        !            89: ENDBODY
1.1       bowersj2   90: }
                     91: 
                     92: # Render takes a tex fragment, transforms it for TtH, and returns the
                     93: # HTML equivalent
1.20    ! www        94: sub render {
1.2       bowersj2   95:     my ($tex, $docroot, $serverroot) = @_;
                     96:     tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
1.1       bowersj2   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
1.2       bowersj2  104:     # absolute paths for use with help.loncapa.org
1.1       bowersj2  105:     $tex =~ s|  \\ref\{([^}]*)\}
1.15      albertel  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/".
1.1       bowersj2  111:               substr($fragmentLabels{$1}, 0, -4) .
1.20    ! www       112:               '.hlp#' . &processLabelName($1) . 
1.2       bowersj2  113:              '"><img src="http://' . $serverroot . '/adm/help/gif/smallHelp.gif" border="0" /></a>' .
                    114:              '\\end{html}'
1.1       bowersj2  115:              |gxe;
                    116: 
1.3       bowersj2  117:     # Backslashes
                    118:     $tex =~ s|\\textbackslash|###BACKSLASH###|g;
                    119: 
1.1       bowersj2  120:     # Figures leftover without captions
                    121:     $tex =~ s|  \\includegraphics(\[[^]]*\])*\{([^}]*)\}
1.2       bowersj2  122:              |  '\\begin{html}<img src="http://' . $serverroot . '/adm/help/gif/' . $2 . '.gif" border="2"'.
1.1       bowersj2  123:                 ' bordercolor="#000000"/>\\end{html}'
                    124:              |gxe;
                    125: 
1.3       bowersj2  126: 
1.17      albertel  127:     $tex=&Apache::lontexconvert::tth_converted(\$tex);
1.5       albertel  128:     
1.3       bowersj2  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
1.1       bowersj2  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;
1.5       albertel  139: 	     $tex.=$Apache::lontexconvert::errorstring;
1.1       bowersj2  140:     untie %fragmentLabels;
                    141: 
                    142:     return $tex;
                    143: }
                    144: 
1.20    ! www       145: sub handler {
1.1       bowersj2  146:      my $r = shift;
                    147: 
1.2       bowersj2  148:      my $docroot = $r->dir_config('lonDocRoot');
                    149:      my $serverroot = $ENV{'HTTP_HOST'};
                    150: 
1.12      www       151:      my $filenames = &Apache::lonnet::unescape(substr ($ENV{'REQUEST_URI'} , 
                    152: 			    rindex($ENV{'REQUEST_URI'}, '/') + 1, -4));
1.19      albertel  153: 
1.1       bowersj2  154:      # Security check on the file; the whole filename must consist
                    155:      # of nothing but alphanums, ' ,, or ., or the file
                    156:      # will be "not found", no matter what.
                    157:      
1.7       bowersj2  158:      return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
1.1       bowersj2  159: 
1.10      www       160:      &Apache::lonlocal::get_language_handle($r);
                    161: 
1.6       bowersj2  162:      # Join together the tex files, return HTTP_NOT_FOUND if any of
                    163:      # them are not found
                    164:      my $tex = '';
1.7       bowersj2  165:      # Since in insertlist.tab I want to specify multiple files,
                    166:      # and insertlist.tab also uses commas, I need something else
1.8       bowersj2  167:      # so replace : with ,
1.7       bowersj2  168:      $filenames =~ s/:/,/g;
1.6       bowersj2  169:      my @files = split(/,/, $filenames);
                    170:      
                    171:      for my $filename (@files) {
1.11      albertel  172: 	 if (-e $docroot.'/adm/help/tex/'.
                    173: 	     &Apache::lonlocal::current_language().'/'.
                    174: 	     $filename.'.tex') {
                    175: 	     $filename=&Apache::lonlocal::current_language().'/'.$filename;
1.10      www       176: 	 }
1.6       bowersj2  177: 	 (my $file = Apache::File->new($docroot
                    178: 			   . '/adm/help/tex/'.$filename.'.tex'))
                    179: 	     or return HTTP_NOT_FOUND;
                    180: 	 $tex .= join('', <$file>);
                    181:      }
1.5       albertel  182: 
1.18      albertel  183:      if ($env{'browser.mathml'}) {
1.10      www       184: 	 &Apache::loncommon::content_type($r,'text/xml');
1.5       albertel  185: 	 &tth::ttminit();
1.18      albertel  186: 	 if ($env{'browser.unicode'}) {
1.5       albertel  187: 	     &tth::ttmoptions('-L -u1');
                    188: 	 } else {
                    189: 	     &tth::ttmoptions('-L -u0');
                    190: 	 }
                    191:      } else {
1.10      www       192: 	 &Apache::loncommon::content_type($r,"text/html");
1.5       albertel  193: 	 &tth::tthinit();
1.18      albertel  194: 	 if ($env{'browser.unicode'}) {
1.5       albertel  195: 	     &tth::tthoptions('-L -u1');
                    196: 	 } else {
                    197: 	     &tth::tthoptions('-L -u0');
                    198: 	 }
                    199:      }
                    200: 
1.13      www       201:      $r->send_http_header;
1.20    ! www       202:      my $text = &render($tex, $docroot, $serverroot);
        !           203:      &servetext($r,$text);
1.1       bowersj2  204:      return OK;
                    205: }
                    206: 
                    207: 1;

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