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

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.14      www        56:     my $bugs=&Apache::loncommon::help_open_bug('Documentation');
1.24      albertel   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();
1.14      www        62:     my $header=&mt('LON-CAPA Help');
1.16      albertel   63:     $r->print(<<HEADER);
1.24      albertel   64:     $start_page
1.26    ! albertel   65:     <h3 style="font: sans-serif"><img align="right" alt="help logo"
        !            66:     src="/adm/help/gif/lonhelpheader.gif"/>$header</h3><hr />$bugs
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');
                     74: 	my $about=&mt('About LON-CAPA help and More Help');
1.25      www        75:         my $query=&mt('Search');
1.26    ! albertel   76: 	my $uri = $r->uri;
1.20      www        77: 	$r->print(<<FOOTER);
1.26    ! albertel   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 />
1.20      www        82: <a href="/adm/help/abouthelp.html">$about</a>
1.1       bowersj2   83: FOOTER
1.24      albertel   84:  
                     85:     }
                     86: 
                     87:     $r->print(<<ENDBODY);
1.9       bowersj2   88:     <!-- END -->
1.24      albertel   89:     $end_page
1.20      www        90: ENDBODY
1.24      albertel   91: 
1.1       bowersj2   92: }
                     93: 
                     94: # Render takes a tex fragment, transforms it for TtH, and returns the
                     95: # HTML equivalent
1.20      www        96: sub render {
1.2       bowersj2   97:     my ($tex, $docroot, $serverroot) = @_;
                     98:     tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
1.1       bowersj2   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
1.2       bowersj2  106:     # absolute paths for use with help.loncapa.org
1.1       bowersj2  107:     $tex =~ s|  \\ref\{([^}]*)\}
1.15      albertel  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/".
1.1       bowersj2  113:               substr($fragmentLabels{$1}, 0, -4) .
1.20      www       114:               '.hlp#' . &processLabelName($1) . 
1.2       bowersj2  115:              '"><img src="http://' . $serverroot . '/adm/help/gif/smallHelp.gif" border="0" /></a>' .
                    116:              '\\end{html}'
1.1       bowersj2  117:              |gxe;
                    118: 
1.3       bowersj2  119:     # Backslashes
                    120:     $tex =~ s|\\textbackslash|###BACKSLASH###|g;
                    121: 
1.1       bowersj2  122:     # Figures leftover without captions
                    123:     $tex =~ s|  \\includegraphics(\[[^]]*\])*\{([^}]*)\}
1.2       bowersj2  124:              |  '\\begin{html}<img src="http://' . $serverroot . '/adm/help/gif/' . $2 . '.gif" border="2"'.
1.1       bowersj2  125:                 ' bordercolor="#000000"/>\\end{html}'
                    126:              |gxe;
                    127: 
1.3       bowersj2  128: 
1.17      albertel  129:     $tex=&Apache::lontexconvert::tth_converted(\$tex);
1.5       albertel  130:     
1.3       bowersj2  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
1.1       bowersj2  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;
1.5       albertel  141: 	     $tex.=$Apache::lontexconvert::errorstring;
1.1       bowersj2  142:     untie %fragmentLabels;
                    143: 
                    144:     return $tex;
                    145: }
                    146: 
1.22      www       147: sub listmatches {
1.25      www       148:     my ($docroot,$term,$subdir) =@_;
                    149:     unless ($subdir) { $subdir=''; }
1.22      www       150:     my $output='';
1.25      www       151:     opendir(DIR,$docroot.'/adm/help/tex/'.$subdir);
1.22      www       152:     foreach my $filename (sort readdir(DIR)) {
                    153: 	if ($filename=~/\.tex$/) {
1.25      www       154: 	    open(FH,$docroot.'/adm/help/tex/'.$subdir.$filename);
1.22      www       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;
1.23      www       162: 		    $line=~s/(\Q$term\E)/\<b\>$1\<\/b\>/gsi;
1.25      www       163:                     $line=~s/\</\&lt\;/gs;
                    164:                     $line=~s/\>/\&gt\;/gs;
1.22      www       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/;
1.25      www       174: 		$output.='<li><a href="/adm/help/tex/'.$subdir.$filename.'">'.$title.'</a>'.$quote.'</li>';
1.22      www       175: 	    }
                    176: 	}
                    177:     }
                    178:     closedir(DIR);
1.25      www       179:     return (($output?'<ul>'.$output.'</ul>':&mt('"[_1]" not found',$term)),$output);
1.22      www       180: }
                    181: 
1.20      www       182: sub handler {
1.1       bowersj2  183:      my $r = shift;
                    184: 
1.2       bowersj2  185:      my $docroot = $r->dir_config('lonDocRoot');
                    186:      my $serverroot = $ENV{'HTTP_HOST'};
                    187: 
1.21      www       188:      &Apache::lonlocal::get_language_handle($r);
                    189:      my $text='';
                    190:      if ($env{'form.searchterm'}=~/\w/) {
1.22      www       191: 	 &Apache::loncommon::content_type($r,"text/html");
1.25      www       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:          }
1.21      www       201:      } else {
1.22      www       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'))
1.21      www       228: 	         or return HTTP_NOT_FOUND;
                    229: 	     $tex .= join('', <$file>);
1.22      www       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);
1.5       albertel  250:      }
                    251: 
1.13      www       252:      $r->send_http_header;
1.20      www       253:      &servetext($r,$text);
1.1       bowersj2  254:      return OK;
                    255: }
                    256: 
                    257: 1;

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