File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.50: download - view: text, annotated - select for diffs
Fri Feb 21 21:28:48 2025 UTC (4 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- WCAG 2 compliance.

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

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