File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.22: download - view: text, annotated - select for diffs
Thu Mar 16 17:18:34 2006 UTC (18 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #4697: Online help is searchable

    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 Apache::lonnavmaps; # for advancedUser
   39: use Apache::lonlocal;
   40: use Apache::lonnet;
   41: use tth();
   42: use GDBM_File();
   43: 
   44: # This sub takes the name of a label in, and converts it to something
   45: # that is a valid anchor name.
   46: 
   47: sub processLabelName {
   48:     my ($name) = @_;
   49:     $name =~ tr/a-zA-Z0-9/_/cs;
   50:     return $name;
   51: }
   52: 
   53: # Serve out the text
   54: sub servetext {
   55:     my ($r,$text) = @_;
   56:     my $html=&Apache::lonxml::xmlbegin();
   57:     my $htmlend=&Apache::lonxml::xmlend();
   58:     my $bugs=&Apache::loncommon::help_open_bug('Documentation');
   59:     my $header=&mt('LON-CAPA Help');
   60:     $r->print(<<HEADER);
   61: $html
   62:     <head>
   63:         <title>LON-CAPA Help</title>
   64:     </head>
   65:     <body bgcolor="#FFFFFF">
   66:     <h3 style="font: sans-serif"><img align="right"
   67:     src="/adm/help/gif/lonhelpheader.gif"/>$header<hr />$bugs</h3>
   68:     <!-- BEGIN -->
   69: HEADER
   70: 
   71:     $r->print($text);
   72: 
   73:     if (&Apache::lonnavmaps::advancedUser()) {
   74: 	my $search=&mt('Search LON-CAPA help');
   75: 	my $about=&mt('About LON-CAPA help and More Help');
   76: 	$r->print(<<FOOTER);
   77:     <!-- END -->
   78:     <hr /><form method="post">
   79: $search: <input type="text" name="searchterm" size="40" /><br />
   80: <a href="/adm/help/abouthelp.html">$about</a>
   81:     </body>
   82: $htmlend
   83: FOOTER
   84:    } else {
   85:        $r->print(<<ENDBODY);
   86:     <!-- END -->
   87:     </body>
   88: $htmlend
   89: ENDBODY
   90:    }
   91: }
   92: 
   93: # Render takes a tex fragment, transforms it for TtH, and returns the
   94: # HTML equivalent
   95: sub render {
   96:     my ($tex, $docroot, $serverroot) = @_;
   97:     tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
   98: 
   99:     # This tells TtH what to do with captions, labels, and other
  100:     # things
  101:     $tex = "\\documentclass{article}\n" . $tex;
  102: 
  103:     # We process these ourselves because TtH can't handle then without
  104:     # LaTeX .aux files
  105:     # absolute paths for use with help.loncapa.org
  106:     $tex =~ s|  \\ref\{([^}]*)\}
  107:              |
  108:               if (not(exists($fragmentLabels{$1}))) {
  109: 	          &Apache::lonnet::logthis("ERROR: $1 not a valid help label");
  110:               };
  111:              '\\begin{html}<a href="http://' . $serverroot ."/adm/help/".
  112:               substr($fragmentLabels{$1}, 0, -4) .
  113:               '.hlp#' . &processLabelName($1) . 
  114:              '"><img src="http://' . $serverroot . '/adm/help/gif/smallHelp.gif" border="0" /></a>' .
  115:              '\\end{html}'
  116:              |gxe;
  117: 
  118:     # Backslashes
  119:     $tex =~ s|\\textbackslash|###BACKSLASH###|g;
  120: 
  121:     # Figures leftover without captions
  122:     $tex =~ s|  \\includegraphics(\[[^]]*\])*\{([^}]*)\}
  123:              |  '\\begin{html}<img src="http://' . $serverroot . '/adm/help/gif/' . $2 . '.gif" border="2"'.
  124:                 ' bordercolor="#000000"/>\\end{html}'
  125:              |gxe;
  126: 
  127: 
  128:     $tex=&Apache::lontexconvert::tth_converted(\$tex);
  129:     
  130:     # Finish backslashes
  131:     $tex =~ s/###BACKSLASH###/'\\'/ge;
  132:  
  133:     # Fix the pretty quotes
  134:     $tex =~ s/('')|(``)/&quot;/g; #" to get emacs syntax highlighter happy
  135: 
  136:     # For some reason all captions come out as "Figure 0:", so
  137:     # just duck the issue...
  138: 
  139:     $tex =~ s/Figure 0://g;
  140: 	     $tex.=$Apache::lontexconvert::errorstring;
  141:     untie %fragmentLabels;
  142: 
  143:     return $tex;
  144: }
  145: 
  146: sub listmatches {
  147:     my ($docroot,$term) =@_;
  148:     my $output='';
  149:     opendir(DIR,$docroot.'/adm/help/tex/');
  150:     foreach my $filename (sort readdir(DIR)) {
  151: 	if ($filename=~/\.tex$/) {
  152: 	    open(FH,$docroot.'/adm/help/tex/'.$filename);
  153: 	    my $quote='';
  154: 	    while (my $line=<FH>) {
  155: 		if ($line=~/\Q$term\E/i) {
  156: 		    $line=~s/\\\w+//gs;
  157: 		    $line=~s/\{//gs;
  158: 		    $line=~s/\}//gs;
  159: 		    $line=~s/\\/ /gs;
  160: 		    $line=~s/\Q$term\E/\<b\>$term\<\/b\>/gsi;
  161: 		    $quote.='<br />...'.$line.'...';
  162: 		}
  163: 	    }
  164: 	    close(FH);
  165: 	    if ($quote) {
  166: 		my $title=$filename;
  167:                 $title=~s/\_/ /gs;
  168:                 $title=~s/\.tex$//;
  169:                 $filename=~s/\.tex$/\.hlp/;
  170: 		$output.='<li><a href="/adm/help/tex/'.$filename.'">'.$title.'</a>'.$quote.'</li>';
  171: 	    }
  172: 	}
  173:     }
  174:     closedir(DIR);
  175:     return ($output?'<ul>'.$output.'</ul>':&mt('"[_1]" not found',$term));
  176: }
  177: 
  178: sub handler {
  179:      my $r = shift;
  180: 
  181:      my $docroot = $r->dir_config('lonDocRoot');
  182:      my $serverroot = $ENV{'HTTP_HOST'};
  183: 
  184:      &Apache::lonlocal::get_language_handle($r);
  185:      my $text='';
  186:      if ($env{'form.searchterm'}=~/\w/) {
  187: 	 &Apache::loncommon::content_type($r,"text/html");
  188: 	 $text=&listmatches($docroot,$env{'form.searchterm'});
  189:      } else {
  190: 	 my $filenames = &Apache::lonnet::unescape(substr ($ENV{'REQUEST_URI'} , 
  191: 							   rindex($ENV{'REQUEST_URI'}, '/') + 1, -4));
  192: 	 
  193: 	 # Security check on the file; the whole filename must consist
  194: 	 # of nothing but alphanums, ' ,, or ., or the file
  195: 	 # will be "not found", no matter what.
  196: 	 
  197: 	 return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
  198: 	 
  199: 	 # Join together the tex files, return HTTP_NOT_FOUND if any of
  200: 	 # them are not found
  201: 	 my $tex = '';
  202: 	 # Since in insertlist.tab I want to specify multiple files,
  203: 	 # and insertlist.tab also uses commas, I need something else
  204: 	 # so replace : with ,
  205: 	 $filenames =~ s/:/,/g;
  206: 	 my @files = split(/,/, $filenames);
  207: 	 
  208: 	 for my $filename (@files) {
  209: 	     if (-e $docroot.'/adm/help/tex/'.
  210: 		 &Apache::lonlocal::current_language().'/'.
  211: 		 $filename.'.tex') {
  212: 		 $filename=&Apache::lonlocal::current_language().'/'.$filename;
  213: 	     }
  214: 	     (my $file = Apache::File->new($docroot
  215: 					   . '/adm/help/tex/'.$filename.'.tex'))
  216: 	         or return HTTP_NOT_FOUND;
  217: 	     $tex .= join('', <$file>);
  218: 	 }
  219: 	 
  220: 	 if ($env{'browser.mathml'}) {
  221: 	     &Apache::loncommon::content_type($r,'text/xml');
  222: 	     &tth::ttminit();
  223: 	     if ($env{'browser.unicode'}) {
  224: 		 &tth::ttmoptions('-L -u1');
  225: 	     } else {
  226: 		 &tth::ttmoptions('-L -u0');
  227: 	     }
  228: 	 } else {
  229: 	     &Apache::loncommon::content_type($r,"text/html");
  230: 	     &tth::tthinit();
  231: 	     if ($env{'browser.unicode'}) {
  232: 		 &tth::tthoptions('-L -u1');
  233: 	     } else {
  234: 		 &tth::tthoptions('-L -u0');
  235: 	     }
  236: 	 }
  237: 	 $text = &render($tex, $docroot, $serverroot);
  238:      }
  239: 
  240:      $r->send_http_header;
  241:      &servetext($r,$text);
  242:      return OK;
  243: }
  244: 
  245: 1;

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