File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.44: download - view: text, annotated - select for diffs
Wed Apr 23 17:57:57 2014 UTC (10 years ago) by bisitz
Branches: MAIN
CVS tags: version_2_11_1, version_2_11_0_RC3, version_2_11_0, HEAD
- XHML
- Fix Typo

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

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