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

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

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