File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.40: download - view: text, annotated - select for diffs
Tue Jan 4 21:53:23 2011 UTC (13 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
- Link and Icon for PDF version of manuals on Help Intro pages.

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

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