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

1.1       bowersj2    1: # The LearningOnline Network with CAPA
1.27      www         2: #
1.48    ! raeburn     3: # $Id: lonhelp.pm,v 1.47 2022/05/27 05:09:21 raeburn 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 {
1.42      raeburn    60:     my ($r,$uri,$text,$is_mobile,$firstfile) = @_;
                     61:     my $bugs;
                     62:     my %helpconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],
                     63:                                               $env{'request.role.domain'});
                     64:     if (ref($helpconfig{'helpsettings'}) eq 'HASH') {
                     65:         if ($helpconfig{'helpsettings'}{'submitbugs'} eq '1') {
                     66:             $bugs = &Apache::loncommon::help_open_bug('Documentation',&mt('Report a documentation bug'));
                     67:         }
                     68:     }
                     69:     my %lt = &Apache::lonlocal::texthash(
                     70:                                            header  => 'LON-CAPA Help',
                     71:                                            search  => 'Search LON-CAPA help',
                     72:                                            query   => 'Search',
                     73:                                         );
1.47      raeburn    74:     my $goback;
                     75:     if ((($env{'request.lti.login'}) && ($env{'request.lti.target'} eq 'iframe')) ||
                     76:         (($env{'request.deeplink.login'}) && ($env{'request.deeplink.target'} eq '_self'))) {
1.48    ! raeburn    77:         my $backicon = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL').'/').'clickhere.gif';
1.47      raeburn    78:         $goback = '<a href="javascript:history.go(-1);" class="LC_menubuttons_link" title="Go Back">'.
1.48    ! raeburn    79:                   '<img src="'.$backicon.'" alt="'.&mt('Go Back').'" class="LC_icon" border="0" />'.
1.47      raeburn    80:                   '</a>&nbsp;';
                     81:     }
1.16      albertel   82:     $r->print(<<HEADER);
1.26      albertel   83:     <h3 style="font: sans-serif"><img align="right" alt="help logo"
1.47      raeburn    84:     src="/adm/help/gif/lonhelpheader.gif" />$goback$lt{'header'}</h3><hr />
1.2       bowersj2   85:     <!-- BEGIN -->
1.1       bowersj2   86: HEADER
1.41      raeburn    87:     if ($is_mobile) {
                     88:         my $width = 500;
                     89:         my $height = 400;
                     90:         my $machine = &Apache::lonnet::absolute_url();
                     91:         $r->print(&Apache::loncommon::nicescroll_javascript('helpwrapper',
                     92:                                                             {cursorcolor => '#00F',
                     93:                                                              railalign => 'right',
                     94:                                                              railoffset => '{top:5,left:40}'},
                     95:                                                              undef,1,$machine.$firstfile));
                     96:         $r->print('<div id="helpwrapper" style="height:'.$height.'px; width:'.$width.'px; overflow: auto;">'.
                     97:                   $text.
                     98:                   '</div>');
                     99:     } else {
                    100:         $r->print($text);
                    101:     }
1.1       bowersj2  102: 
1.19      albertel  103:     if (&Apache::lonnavmaps::advancedUser()) {
1.42      raeburn   104:         $r->print(<<FOOTER);
                    105:     <hr /><div class="LC_left_float">
                    106:     <form action="$uri" method="post">
                    107:     <fieldset><legend>$lt{'search'}</legend>
                    108:     <input type="text" name="searchterm" size="40" />
                    109:     <input type="submit" value="$lt{'query'}" />
                    110:     </fieldset>
                    111:     </form>
                    112:     </div>
1.44      bisitz    113:     <div class="LC_left_float">
1.42      raeburn   114:     </div>
                    115:     <div style="padding:0;clear:both;margin:0;border:0"></div>
1.30      www       116: $bugs
1.1       bowersj2  117: FOOTER
1.24      albertel  118:     }
                    119: 
                    120:     $r->print(<<ENDBODY);
1.9       bowersj2  121:     <!-- END -->
1.20      www       122: ENDBODY
1.24      albertel  123: 
1.1       bowersj2  124: }
                    125: 
                    126: # Render takes a tex fragment, transforms it for TtH, and returns the
                    127: # HTML equivalent
1.20      www       128: sub render {
1.36      albertel  129:     my ($tex, $docroot) = @_;
1.2       bowersj2  130:     tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
1.1       bowersj2  131: 
                    132:     # This tells TtH what to do with captions, labels, and other
                    133:     # things
                    134:     $tex = "\\documentclass{article}\n" . $tex;
                    135: 
                    136:     # We process these ourselves because TtH can't handle then without
                    137:     # LaTeX .aux files
1.2       bowersj2  138:     # absolute paths for use with help.loncapa.org
1.1       bowersj2  139:     $tex =~ s|  \\ref\{([^}]*)\}
1.15      albertel  140:              |
1.30      www       141:               my $label=$1;
1.40      raeburn   142:               my $icon='/adm/help/help.png';
                    143:               my $ext;
1.30      www       144:               if ($1!~/\.hlp$/) {
1.40      raeburn   145:                   if (($1 =~ /^\w+\.manual\.pdf$/) && (-e $docroot.'/adm/help/'.$1)) {
                    146:                      $icon = '/adm/lonIcons/pdf.gif';
                    147:                   } elsif ((!exists($fragmentLabels{$1})) && ($1!~/\.hlp$/)) {
1.30      www       148: 	             &Apache::lonnet::logthis("ERROR: $1 not a valid help label");
                    149:                      $label='Error';
                    150:                   } else {
                    151:                      $label=substr($fragmentLabels{$1}, 0, -4);
1.40      raeburn   152:                      $ext = '.hlp#' . &processLabelName($1);
1.30      www       153: 		  }
                    154: 	      } else {
                    155: 		  $label=~s/\.hlp$//;
1.40      raeburn   156:                   $ext = '.hlp#' . &processLabelName($1);
1.30      www       157: 	      }
1.33      albertel  158:              '\\begin{html}<a href="/adm/help/'.
1.30      www       159:               $label .
1.40      raeburn   160:               $ext . 
1.44      bisitz    161:              '"><img src="'.$icon.'" border="0" alt="'.&mt('Help').'" /></a>' .
1.2       bowersj2  162:              '\\end{html}'
1.1       bowersj2  163:              |gxe;
                    164: 
1.3       bowersj2  165:     # Backslashes
                    166:     $tex =~ s|\\textbackslash|###BACKSLASH###|g;
                    167: 
1.1       bowersj2  168:     # Figures leftover without captions
                    169:     $tex =~ s|  \\includegraphics(\[[^]]*\])*\{([^}]*)\}
1.33      albertel  170:              |  '\\begin{html}<img src="/adm/help/gif/' . $2 . '.gif" border="2"'.
1.1       bowersj2  171:                 ' bordercolor="#000000"/>\\end{html}'
                    172:              |gxe;
                    173: 
1.3       bowersj2  174: 
1.17      albertel  175:     $tex=&Apache::lontexconvert::tth_converted(\$tex);
1.5       albertel  176:     
1.3       bowersj2  177:     # Finish backslashes
                    178:     $tex =~ s/###BACKSLASH###/'\\'/ge;
                    179:  
                    180:     # Fix the pretty quotes
                    181:     $tex =~ s/('')|(``)/&quot;/g; #" to get emacs syntax highlighter happy
1.1       bowersj2  182: 
1.45      raeburn   183:     $tex =~ s/`/'/g;
                    184: 
1.1       bowersj2  185:     # For some reason all captions come out as "Figure 0:", so
                    186:     # just duck the issue...
                    187: 
                    188:     $tex =~ s/Figure 0://g;
1.34      albertel  189:     $tex.=$Apache::lontexconvert::errorstring;
1.1       bowersj2  190:     untie %fragmentLabels;
                    191: 
                    192:     return $tex;
                    193: }
                    194: 
1.22      www       195: sub listmatches {
1.25      www       196:     my ($docroot,$term,$subdir) =@_;
                    197:     unless ($subdir) { $subdir=''; }
1.22      www       198:     my $output='';
1.25      www       199:     opendir(DIR,$docroot.'/adm/help/tex/'.$subdir);
1.22      www       200:     foreach my $filename (sort readdir(DIR)) {
                    201: 	if ($filename=~/\.tex$/) {
1.25      www       202: 	    open(FH,$docroot.'/adm/help/tex/'.$subdir.$filename);
1.22      www       203: 	    my $quote='';
                    204: 	    while (my $line=<FH>) {
                    205: 		if ($line=~/\Q$term\E/i) {
                    206: 		    $line=~s/\\\w+//gs;
                    207: 		    $line=~s/\{//gs;
                    208: 		    $line=~s/\}//gs;
                    209: 		    $line=~s/\\/ /gs;
1.25      www       210:                     $line=~s/\</\&lt\;/gs;
                    211:                     $line=~s/\>/\&gt\;/gs;
1.29      albertel  212: 		    $line=~s/(\Q$term\E)/\<b\>$1\<\/b\>/gsi;
1.22      www       213: 		    $quote.='<br />...'.$line.'...';
                    214: 		}
                    215: 	    }
                    216: 	    close(FH);
                    217: 	    if ($quote) {
                    218: 		my $title=$filename;
                    219:                 $title=~s/\_/ /gs;
                    220:                 $title=~s/\.tex$//;
                    221:                 $filename=~s/\.tex$/\.hlp/;
1.25      www       222: 		$output.='<li><a href="/adm/help/tex/'.$subdir.$filename.'">'.$title.'</a>'.$quote.'</li>';
1.22      www       223: 	    }
                    224: 	}
                    225:     }
                    226:     closedir(DIR);
1.25      www       227:     return (($output?'<ul>'.$output.'</ul>':&mt('"[_1]" not found',$term)),$output);
1.22      www       228: }
                    229: 
1.20      www       230: sub handler {
1.1       bowersj2  231:      my $r = shift;
                    232: 
1.2       bowersj2  233:      my $docroot = $r->dir_config('lonDocRoot');
                    234: 
1.21      www       235:      &Apache::lonlocal::get_language_handle($r);
1.28      albertel  236:      &Apache::loncommon::content_type($r,"text/html");
1.41      raeburn   237:      my $caller;
                    238:      if ($env{'form.searchterm'}=~/\w/) {
                    239:         $caller = 'search';
                    240:      }
                    241: 
1.43      raeburn   242:      my $starthash;
                    243: 
                    244:      if ($env{'browser.mobile'}) {
                    245:          $starthash = {
                    246:                         only_body   => 1,
                    247:                         add_entries => {
                    248:                                          'onload' => "javascript:expand_div('$caller');",
                    249:                                        },
                    250:                       };
                    251:      } else {
                    252:          $starthash = {
                    253:                         only_body   => 1,
                    254:                       };
                    255:      }
1.41      raeburn   256:      my $firstfile;
1.36      albertel  257:      my $start_page=
1.41      raeburn   258: 	 &Apache::loncommon::start_page('LON-CAPA Help',undef,$starthash);
1.21      www       259:      my $text='';
1.42      raeburn   260:      my $uri = $r->uri;
1.21      www       261:      if ($env{'form.searchterm'}=~/\w/) {
1.25      www       262: 	 ($text,my $matches)=&listmatches($docroot,$env{'form.searchterm'},&Apache::lonlocal::current_language().'/');
                    263:          if ($matches) {
                    264:              my ($englishresult,$englishmatches)=&listmatches($docroot,$env{'form.searchterm'});
                    265:              if ($englishmatches) {
                    266:                 $text.='<hr />'.$englishresult;
                    267:              }
                    268:          } else {
                    269:              $text=&listmatches($docroot,$env{'form.searchterm'}); 
                    270:          }
1.21      www       271:      } else {
1.42      raeburn   272:          my $filenames = &unescape(substr($uri,rindex($uri,'/')+1,-4));
1.22      www       273: 	 
                    274: 	 # Security check on the file; the whole filename must consist
                    275: 	 # of nothing but alphanums, ' ,, or ., or the file
                    276: 	 # will be "not found", no matter what.
                    277: 	 
                    278: 	 return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
                    279: 	 
                    280: 	 # Join together the tex files, return HTTP_NOT_FOUND if any of
                    281: 	 # them are not found
                    282: 	 my $tex = '';
                    283: 	 # Since in insertlist.tab I want to specify multiple files,
                    284: 	 # and insertlist.tab also uses commas, I need something else
                    285: 	 # so replace : with ,
                    286: 	 $filenames =~ s/:/,/g;
                    287: 	 my @files = split(/,/, $filenames);
1.41      raeburn   288:          $firstfile = '/adm/help/'.$files[0].'.hlp';
1.46      raeburn   289:          my $count = 0;
1.41      raeburn   290: 
1.22      www       291: 	 for my $filename (@files) {
                    292: 	     if (-e $docroot.'/adm/help/tex/'.
                    293: 		 &Apache::lonlocal::current_language().'/'.
                    294: 		 $filename.'.tex') {
                    295: 		 $filename=&Apache::lonlocal::current_language().'/'.$filename;
                    296: 	     }
                    297: 	     (my $file = Apache::File->new($docroot
                    298: 					   . '/adm/help/tex/'.$filename.'.tex'))
1.21      www       299: 	         or return HTTP_NOT_FOUND;
                    300: 	     $tex .= join('', <$file>);
1.46      raeburn   301:              $count ++;
                    302:              if (scalar(@files) > $count) {
                    303:                  $tex .= '\hrulefill';
                    304:              }
1.22      www       305: 	 }
1.28      albertel  306: 
1.36      albertel  307: 	 $text = &render($tex, $docroot);
1.5       albertel  308:      }
                    309: 
1.13      www       310:      $r->send_http_header;
1.36      albertel  311:      $r->print($start_page);
1.42      raeburn   312:      &servetext($r,$uri,$text,$env{'browser.mobile'},$firstfile);
1.36      albertel  313:      $r->print(&Apache::loncommon::end_page());
                    314: 
1.1       bowersj2  315:      return OK;
                    316: }
                    317: 
                    318: 1;

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