File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.26: download - view: text, annotated - select for diffs
Tue May 23 18:13:07 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- correctiing some bad html

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

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