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

1.1       bowersj2    1: # The LearningOnline Network with CAPA
1.27      www         2: #
1.38    ! www         3: # $Id: lonhelp.pm,v 1.37 2008/02/26 09:28:07 bisitz Exp $
1.27      www         4: #
1.1       bowersj2    5: # .tex help system web server handler
                      6: #
                      7: # Copyright Michigan State University Board of Trustees
                      8: #
                      9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     10: #
                     11: # LON-CAPA is free software; you can redistribute it and/or modify
                     12: # it under the terms of the GNU General Public License as published by
                     13: # the Free Software Foundation; either version 2 of the License, or
                     14: # (at your option) any later version.
                     15: #
                     16: # LON-CAPA is distributed in the hope that it will be useful,
                     17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     19: # GNU General Public License for more details.
                     20: #
                     21: # You should have received a copy of the GNU General Public License
                     22: # along with LON-CAPA; if not, write to the Free Software
                     23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     24: #
                     25: # /home/httpd/html/adm/gpl.txt
                     26: #
                     27: # http://www.lon-capa.org/
                     28: #
                     29: # .tex file help handler
1.27      www        30: 
1.1       bowersj2   31: 
                     32: package Apache::lonhelp;
                     33: 
                     34: use strict;
                     35: use Apache::Constants qw(:common :http);
1.5       albertel   36: use Apache::File();
                     37: use Apache::loncommon();
                     38: use Apache::lonacc();
                     39: use Apache::lontexconvert();
1.9       bowersj2   40: use Apache::lonnavmaps; # for advancedUser
1.11      albertel   41: use Apache::lonlocal;
1.18      albertel   42: use Apache::lonnet;
1.5       albertel   43: use tth();
                     44: use GDBM_File();
1.27      www        45: use lib '/home/httpd/lib/perl/';
                     46: use LONCAPA;
                     47:  
1.1       bowersj2   48: 
                     49: # This sub takes the name of a label in, and converts it to something
                     50: # that is a valid anchor name.
1.20      www        51: 
                     52: sub processLabelName {
1.1       bowersj2   53:     my ($name) = @_;
                     54:     $name =~ tr/a-zA-Z0-9/_/cs;
                     55:     return $name;
                     56: }
                     57: 
1.20      www        58: # Serve out the text
                     59: sub servetext {
                     60:     my ($r,$text) = @_;
1.37      bisitz     61:     my $bugs=&Apache::loncommon::help_open_bug('Documentation',&mt('Report a documentation bug'));
1.32      albertel   62:     my $morehelp;#=&Apache::loncommon::help_open_menu(undef,undef,1,undef,undef,undef,undef,'Help and Assistance Menu');
1.14      www        63:     my $header=&mt('LON-CAPA Help');
1.16      albertel   64:     $r->print(<<HEADER);
1.26      albertel   65:     <h3 style="font: sans-serif"><img align="right" alt="help logo"
1.30      www        66:     src="/adm/help/gif/lonhelpheader.gif"/>$header</h3><hr />
1.2       bowersj2   67:     <!-- BEGIN -->
1.1       bowersj2   68: HEADER
                     69: 
1.20      www        70:     $r->print($text);
1.1       bowersj2   71: 
1.19      albertel   72:     if (&Apache::lonnavmaps::advancedUser()) {
1.20      www        73: 	my $search=&mt('Search LON-CAPA help');
1.25      www        74:         my $query=&mt('Search');
1.26      albertel   75: 	my $uri = $r->uri;
1.20      www        76: 	$r->print(<<FOOTER);
1.26      albertel   77:     <hr /><form action="$uri" method="post">
1.35      www        78: <label>$search: <input type="text" name="searchterm" size="40" /></label><input type="submit" value="$query" />
1.26      albertel   79: </form>
                     80: <br />
1.30      www        81: $bugs
1.31      www        82: $morehelp
1.1       bowersj2   83: FOOTER
1.24      albertel   84:  
                     85:     }
                     86: 
                     87:     $r->print(<<ENDBODY);
1.9       bowersj2   88:     <!-- END -->
1.20      www        89: ENDBODY
1.24      albertel   90: 
1.1       bowersj2   91: }
                     92: 
                     93: # Render takes a tex fragment, transforms it for TtH, and returns the
                     94: # HTML equivalent
1.20      www        95: sub render {
1.36      albertel   96:     my ($tex, $docroot) = @_;
1.2       bowersj2   97:     tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
1.1       bowersj2   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
1.2       bowersj2  105:     # absolute paths for use with help.loncapa.org
1.1       bowersj2  106:     $tex =~ s|  \\ref\{([^}]*)\}
1.15      albertel  107:              |
1.30      www       108:               my $label=$1;
                    109:               if ($1!~/\.hlp$/) {
                    110:                   if ((!exists($fragmentLabels{$1})) && ($1!~/\.hlp$/)) {
                    111: 	             &Apache::lonnet::logthis("ERROR: $1 not a valid help label");
                    112:                      $label='Error';
                    113:                   } else {
                    114:                      $label=substr($fragmentLabels{$1}, 0, -4);
                    115: 		  }
                    116: 	      } else {
                    117: 		  $label=~s/\.hlp$//;
                    118: 	      }
1.33      albertel  119:              '\\begin{html}<a href="/adm/help/'.
1.30      www       120:               $label .
1.20      www       121:               '.hlp#' . &processLabelName($1) . 
1.38    ! www       122:              '"><img src="/res/adm/pages/help.png" border="0" /></a>' .
1.2       bowersj2  123:              '\\end{html}'
1.1       bowersj2  124:              |gxe;
                    125: 
1.3       bowersj2  126:     # Backslashes
                    127:     $tex =~ s|\\textbackslash|###BACKSLASH###|g;
                    128: 
1.1       bowersj2  129:     # Figures leftover without captions
                    130:     $tex =~ s|  \\includegraphics(\[[^]]*\])*\{([^}]*)\}
1.33      albertel  131:              |  '\\begin{html}<img src="/adm/help/gif/' . $2 . '.gif" border="2"'.
1.1       bowersj2  132:                 ' bordercolor="#000000"/>\\end{html}'
                    133:              |gxe;
                    134: 
1.3       bowersj2  135: 
1.17      albertel  136:     $tex=&Apache::lontexconvert::tth_converted(\$tex);
1.5       albertel  137:     
1.3       bowersj2  138:     # Finish backslashes
                    139:     $tex =~ s/###BACKSLASH###/'\\'/ge;
                    140:  
                    141:     # Fix the pretty quotes
                    142:     $tex =~ s/('')|(``)/&quot;/g; #" to get emacs syntax highlighter happy
1.1       bowersj2  143: 
                    144:     # For some reason all captions come out as "Figure 0:", so
                    145:     # just duck the issue...
                    146: 
                    147:     $tex =~ s/Figure 0://g;
1.34      albertel  148:     $tex.=$Apache::lontexconvert::errorstring;
1.1       bowersj2  149:     untie %fragmentLabels;
                    150: 
                    151:     return $tex;
                    152: }
                    153: 
1.22      www       154: sub listmatches {
1.25      www       155:     my ($docroot,$term,$subdir) =@_;
                    156:     unless ($subdir) { $subdir=''; }
1.22      www       157:     my $output='';
1.25      www       158:     opendir(DIR,$docroot.'/adm/help/tex/'.$subdir);
1.22      www       159:     foreach my $filename (sort readdir(DIR)) {
                    160: 	if ($filename=~/\.tex$/) {
1.25      www       161: 	    open(FH,$docroot.'/adm/help/tex/'.$subdir.$filename);
1.22      www       162: 	    my $quote='';
                    163: 	    while (my $line=<FH>) {
                    164: 		if ($line=~/\Q$term\E/i) {
                    165: 		    $line=~s/\\\w+//gs;
                    166: 		    $line=~s/\{//gs;
                    167: 		    $line=~s/\}//gs;
                    168: 		    $line=~s/\\/ /gs;
1.25      www       169:                     $line=~s/\</\&lt\;/gs;
                    170:                     $line=~s/\>/\&gt\;/gs;
1.29      albertel  171: 		    $line=~s/(\Q$term\E)/\<b\>$1\<\/b\>/gsi;
1.22      www       172: 		    $quote.='<br />...'.$line.'...';
                    173: 		}
                    174: 	    }
                    175: 	    close(FH);
                    176: 	    if ($quote) {
                    177: 		my $title=$filename;
                    178:                 $title=~s/\_/ /gs;
                    179:                 $title=~s/\.tex$//;
                    180:                 $filename=~s/\.tex$/\.hlp/;
1.25      www       181: 		$output.='<li><a href="/adm/help/tex/'.$subdir.$filename.'">'.$title.'</a>'.$quote.'</li>';
1.22      www       182: 	    }
                    183: 	}
                    184:     }
                    185:     closedir(DIR);
1.25      www       186:     return (($output?'<ul>'.$output.'</ul>':&mt('"[_1]" not found',$term)),$output);
1.22      www       187: }
                    188: 
1.20      www       189: sub handler {
1.1       bowersj2  190:      my $r = shift;
                    191: 
1.2       bowersj2  192:      my $docroot = $r->dir_config('lonDocRoot');
                    193: 
1.21      www       194:      &Apache::lonlocal::get_language_handle($r);
1.28      albertel  195:      &Apache::loncommon::content_type($r,"text/html");
1.36      albertel  196:      my $start_page=
                    197: 	 &Apache::loncommon::start_page('LON-CAPA Help',undef,
                    198: 					{'only_body' => 1,});
1.21      www       199:      my $text='';
                    200:      if ($env{'form.searchterm'}=~/\w/) {
1.25      www       201: 	 ($text,my $matches)=&listmatches($docroot,$env{'form.searchterm'},&Apache::lonlocal::current_language().'/');
                    202:          if ($matches) {
                    203:              my ($englishresult,$englishmatches)=&listmatches($docroot,$env{'form.searchterm'});
                    204:              if ($englishmatches) {
                    205:                 $text.='<hr />'.$englishresult;
                    206:              }
                    207:          } else {
                    208:              $text=&listmatches($docroot,$env{'form.searchterm'}); 
                    209:          }
1.21      www       210:      } else {
1.32      albertel  211: 	 my $filenames = &unescape(substr($ENV{'REQUEST_URI'} , 
                    212: 					  rindex($ENV{'REQUEST_URI'}, '/') + 1, -4));
1.22      www       213: 	 
                    214: 	 # Security check on the file; the whole filename must consist
                    215: 	 # of nothing but alphanums, ' ,, or ., or the file
                    216: 	 # will be "not found", no matter what.
                    217: 	 
                    218: 	 return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
                    219: 	 
                    220: 	 # Join together the tex files, return HTTP_NOT_FOUND if any of
                    221: 	 # them are not found
                    222: 	 my $tex = '';
                    223: 	 # Since in insertlist.tab I want to specify multiple files,
                    224: 	 # and insertlist.tab also uses commas, I need something else
                    225: 	 # so replace : with ,
                    226: 	 $filenames =~ s/:/,/g;
                    227: 	 my @files = split(/,/, $filenames);
                    228: 	 
                    229: 	 for my $filename (@files) {
                    230: 	     if (-e $docroot.'/adm/help/tex/'.
                    231: 		 &Apache::lonlocal::current_language().'/'.
                    232: 		 $filename.'.tex') {
                    233: 		 $filename=&Apache::lonlocal::current_language().'/'.$filename;
                    234: 	     }
                    235: 	     (my $file = Apache::File->new($docroot
                    236: 					   . '/adm/help/tex/'.$filename.'.tex'))
1.21      www       237: 	         or return HTTP_NOT_FOUND;
                    238: 	     $tex .= join('', <$file>);
1.22      www       239: 	 }
1.28      albertel  240: 
1.36      albertel  241: 	 $text = &render($tex, $docroot);
1.5       albertel  242:      }
                    243: 
1.13      www       244:      $r->send_http_header;
1.36      albertel  245:      $r->print($start_page);
1.20      www       246:      &servetext($r,$text);
1.36      albertel  247:      $r->print(&Apache::loncommon::end_page());
                    248: 
1.1       bowersj2  249:      return OK;
                    250: }
                    251: 
                    252: 1;

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