[LON-CAPA-cvs] cvs: loncom /interface lonhelp.pm

www lon-capa-cvs@mail.lon-capa.org
Thu, 16 Mar 2006 17:18:34 -0000


www		Thu Mar 16 12:18:34 2006 EDT

  Modified files:              
    /loncom/interface	lonhelp.pm 
  Log:
  Bug #4697: Online help is searchable
  
  
Index: loncom/interface/lonhelp.pm
diff -u loncom/interface/lonhelp.pm:1.21 loncom/interface/lonhelp.pm:1.22
--- loncom/interface/lonhelp.pm:1.21	Wed Mar 15 18:41:49 2006
+++ loncom/interface/lonhelp.pm	Thu Mar 16 12:18:34 2006
@@ -81,12 +81,13 @@
     </body>
 $htmlend
 FOOTER
-    }
-    $r->print(<<ENDBODY);
+   } else {
+       $r->print(<<ENDBODY);
     <!-- END -->
     </body>
 $htmlend
 ENDBODY
+   }
 }
 
 # Render takes a tex fragment, transforms it for TtH, and returns the
@@ -142,6 +143,38 @@
     return $tex;
 }
 
+sub listmatches {
+    my ($docroot,$term) =@_;
+    my $output='';
+    opendir(DIR,$docroot.'/adm/help/tex/');
+    foreach my $filename (sort readdir(DIR)) {
+	if ($filename=~/\.tex$/) {
+	    open(FH,$docroot.'/adm/help/tex/'.$filename);
+	    my $quote='';
+	    while (my $line=<FH>) {
+		if ($line=~/\Q$term\E/i) {
+		    $line=~s/\\\w+//gs;
+		    $line=~s/\{//gs;
+		    $line=~s/\}//gs;
+		    $line=~s/\\/ /gs;
+		    $line=~s/\Q$term\E/\<b\>$term\<\/b\>/gsi;
+		    $quote.='<br />...'.$line.'...';
+		}
+	    }
+	    close(FH);
+	    if ($quote) {
+		my $title=$filename;
+                $title=~s/\_/ /gs;
+                $title=~s/\.tex$//;
+                $filename=~s/\.tex$/\.hlp/;
+		$output.='<li><a href="/adm/help/tex/'.$filename.'">'.$title.'</a>'.$quote.'</li>';
+	    }
+	}
+    }
+    closedir(DIR);
+    return ($output?'<ul>'.$output.'</ul>':&mt('"[_1]" not found',$term));
+}
+
 sub handler {
      my $r = shift;
 
@@ -151,55 +184,57 @@
      &Apache::lonlocal::get_language_handle($r);
      my $text='';
      if ($env{'form.searchterm'}=~/\w/) {
+	 &Apache::loncommon::content_type($r,"text/html");
+	 $text=&listmatches($docroot,$env{'form.searchterm'});
      } else {
-        my $filenames = &Apache::lonnet::unescape(substr ($ENV{'REQUEST_URI'} , 
-			    rindex($ENV{'REQUEST_URI'}, '/') + 1, -4));
-
-        # Security check on the file; the whole filename must consist
-        # of nothing but alphanums, ' ,, or ., or the file
-        # will be "not found", no matter what.
-     
-        return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
-
-        # Join together the tex files, return HTTP_NOT_FOUND if any of
-        # them are not found
-        my $tex = '';
-        # Since in insertlist.tab I want to specify multiple files,
-        # and insertlist.tab also uses commas, I need something else
-        # so replace : with ,
-        $filenames =~ s/:/,/g;
-        my @files = split(/,/, $filenames);
-     
-        for my $filename (@files) {
-	    if (-e $docroot.'/adm/help/tex/'.
-	        &Apache::lonlocal::current_language().'/'.
-	        $filename.'.tex') {
-	        $filename=&Apache::lonlocal::current_language().'/'.$filename;
-	    }
-	    (my $file = Apache::File->new($docroot
-	   		   . '/adm/help/tex/'.$filename.'.tex'))
+	 my $filenames = &Apache::lonnet::unescape(substr ($ENV{'REQUEST_URI'} , 
+							   rindex($ENV{'REQUEST_URI'}, '/') + 1, -4));
+	 
+	 # Security check on the file; the whole filename must consist
+	 # of nothing but alphanums, ' ,, or ., or the file
+	 # will be "not found", no matter what.
+	 
+	 return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
+	 
+	 # Join together the tex files, return HTTP_NOT_FOUND if any of
+	 # them are not found
+	 my $tex = '';
+	 # Since in insertlist.tab I want to specify multiple files,
+	 # and insertlist.tab also uses commas, I need something else
+	 # so replace : with ,
+	 $filenames =~ s/:/,/g;
+	 my @files = split(/,/, $filenames);
+	 
+	 for my $filename (@files) {
+	     if (-e $docroot.'/adm/help/tex/'.
+		 &Apache::lonlocal::current_language().'/'.
+		 $filename.'.tex') {
+		 $filename=&Apache::lonlocal::current_language().'/'.$filename;
+	     }
+	     (my $file = Apache::File->new($docroot
+					   . '/adm/help/tex/'.$filename.'.tex'))
 	         or return HTTP_NOT_FOUND;
 	     $tex .= join('', <$file>);
-        }
-
-        if ($env{'browser.mathml'}) {
-	   &Apache::loncommon::content_type($r,'text/xml');
-	   &tth::ttminit();
-	   if ($env{'browser.unicode'}) {
-	       &tth::ttmoptions('-L -u1');
-	   } else {
-	       &tth::ttmoptions('-L -u0');
-	   }
-        } else {
-	   &Apache::loncommon::content_type($r,"text/html");
-	   &tth::tthinit();
-	   if ($env{'browser.unicode'}) {
-	       &tth::tthoptions('-L -u1');
-	   } else {
-	       &tth::tthoptions('-L -u0');
-	   }
-        }
-        $text = &render($tex, $docroot, $serverroot);
+	 }
+	 
+	 if ($env{'browser.mathml'}) {
+	     &Apache::loncommon::content_type($r,'text/xml');
+	     &tth::ttminit();
+	     if ($env{'browser.unicode'}) {
+		 &tth::ttmoptions('-L -u1');
+	     } else {
+		 &tth::ttmoptions('-L -u0');
+	     }
+	 } else {
+	     &Apache::loncommon::content_type($r,"text/html");
+	     &tth::tthinit();
+	     if ($env{'browser.unicode'}) {
+		 &tth::tthoptions('-L -u1');
+	     } else {
+		 &tth::tthoptions('-L -u0');
+	     }
+	 }
+	 $text = &render($tex, $docroot, $serverroot);
      }
 
      $r->send_http_header;