File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.41: download - view: text, annotated - select for diffs
Mon Jul 15 17:42:11 2013 UTC (10 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC1, HEAD
- Scrolling in modal windows on mobile devices.
  - Wrapper (div using nicescroll) for contextual help shown in iframe.
  - div style width and height reset to 100% onload if not viewed in iframe
- Bubblesheet format file contents display in modal window in domain
  configuration screens
- loncommon::modal_link() takes two additional args:
  $transparency and $style -- can be used to set allowtransparency and style
  for iframe (default is allowtransparency='true'). $style can be used to
  set background-color, to make contents of modal window more readable (e.g.
  if is a plain text file).

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

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