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

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

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