File:  [LON-CAPA] / loncom / interface / lonhelp.pm
Revision 1.17: download - view: text, annotated - select for diffs
Tue Mar 22 16:49:02 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- always use tth/m for converting help documents

    1: # The LearningOnline Network with CAPA
    2: # .tex help system web server handler
    3: #
    4: # Copyright Michigan State University Board of Trustees
    5: #
    6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    7: #
    8: # LON-CAPA is free software; you can redistribute it and/or modify
    9: # it under the terms of the GNU General Public License as published by
   10: # the Free Software Foundation; either version 2 of the License, or
   11: # (at your option) any later version.
   12: #
   13: # LON-CAPA is distributed in the hope that it will be useful,
   14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: # GNU General Public License for more details.
   17: #
   18: # You should have received a copy of the GNU General Public License
   19: # along with LON-CAPA; if not, write to the Free Software
   20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21: #
   22: # /home/httpd/html/adm/gpl.txt
   23: #
   24: # http://www.lon-capa.org/
   25: #
   26: # .tex file help handler
   27: # YEAR=2002
   28: # 7/4 Jeremy Bowers
   29: 
   30: package Apache::lonhelp;
   31: 
   32: use strict;
   33: use Apache::Constants qw(:common :http);
   34: use Apache::File();
   35: use Apache::loncommon();
   36: use Apache::lonacc();
   37: use Apache::lontexconvert();
   38: use Apache::lonnavmaps; # for advancedUser
   39: use Apache::lonlocal;
   40: use tth();
   41: use GDBM_File();
   42: 
   43: # This sub takes the name of a label in, and converts it to something
   44: # that is a valid anchor name.
   45: sub processLabelName 
   46: {
   47:     my ($name) = @_;
   48:     $name =~ tr/a-zA-Z0-9/_/cs;
   49:     return $name;
   50: }
   51: 
   52: # Serve out the Tex
   53: sub serveTex
   54: {
   55:     my ($tex, $r) = @_;
   56:     my $html=&Apache::lonxml::xmlbegin();
   57:     my $bugs=&Apache::loncommon::help_open_bug('Documentation');
   58:     my $header=&mt('LON-CAPA Help');
   59:     my $footer=&mt('About LON-CAPA help and More Help');
   60:     $r->print(<<HEADER);
   61: $html
   62:     <head>
   63:         <title>LON-CAPA Help</title>
   64:     </head>
   65:     <body bgcolor="#FFFFFF">
   66:     <h3 style="font: sans-serif"><img align="right"
   67:     src="/adm/help/gif/lonhelpheader.gif"/>$header<hr />$bugs</h3>
   68:     <!-- BEGIN -->
   69: HEADER
   70: 
   71:     $r->print($tex);
   72: 
   73:     if (Apache::lonnavmaps::advancedUser()) {
   74: $r->print(<<FOOTER);
   75:     <!-- END -->
   76:     <hr />
   77:     <center><font size="-1"><a href="/adm/help/abouthelp.html">$footer</a></font></center>
   78:     </body>
   79: </html>
   80: FOOTER
   81: } else {
   82:     $r->print(<<FOOTER);
   83:     <!-- END -->
   84:     </body>
   85: </html>
   86: FOOTER
   87: }
   88: }
   89: 
   90: # Render takes a tex fragment, transforms it for TtH, and returns the
   91: # HTML equivalent
   92: sub render 
   93: {
   94:     my ($tex, $docroot, $serverroot) = @_;
   95:     tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
   96: 
   97:     # This tells TtH what to do with captions, labels, and other
   98:     # things
   99:     $tex = "\\documentclass{article}\n" . $tex;
  100: 
  101:     # We process these ourselves because TtH can't handle then without
  102:     # LaTeX .aux files
  103:     # absolute paths for use with help.loncapa.org
  104:     $tex =~ s|  \\ref\{([^}]*)\}
  105:              |
  106:               if (not(exists($fragmentLabels{$1}))) {
  107: 	          &Apache::lonnet::logthis("ERROR: $1 not a valid help label");
  108:               };
  109:              '\\begin{html}<a href="http://' . $serverroot ."/adm/help/".
  110:               substr($fragmentLabels{$1}, 0, -4) .
  111:               '.hlp#' . processLabelName($1) . 
  112:              '"><img src="http://' . $serverroot . '/adm/help/gif/smallHelp.gif" border="0" /></a>' .
  113:              '\\end{html}'
  114:              |gxe;
  115: 
  116:     # Backslashes
  117:     $tex =~ s|\\textbackslash|###BACKSLASH###|g;
  118: 
  119:     # Figures leftover without captions
  120:     $tex =~ s|  \\includegraphics(\[[^]]*\])*\{([^}]*)\}
  121:              |  '\\begin{html}<img src="http://' . $serverroot . '/adm/help/gif/' . $2 . '.gif" border="2"'.
  122:                 ' bordercolor="#000000"/>\\end{html}'
  123:              |gxe;
  124: 
  125: 
  126:     $tex=&Apache::lontexconvert::tth_converted(\$tex);
  127:     
  128:     # Finish backslashes
  129:     $tex =~ s/###BACKSLASH###/'\\'/ge;
  130:  
  131:     # Fix the pretty quotes
  132:     $tex =~ s/('')|(``)/&quot;/g; #" to get emacs syntax highlighter happy
  133: 
  134:     # For some reason all captions come out as "Figure 0:", so
  135:     # just duck the issue...
  136: 
  137:     $tex =~ s/Figure 0://g;
  138: 	     $tex.=$Apache::lontexconvert::errorstring;
  139:     untie %fragmentLabels;
  140: 
  141:     return $tex;
  142: }
  143: 
  144: sub handler
  145: {
  146:      my $r = shift;
  147: 
  148:      my $docroot = $r->dir_config('lonDocRoot');
  149:      my $serverroot = $ENV{'HTTP_HOST'};
  150: 
  151:      my $filenames = &Apache::lonnet::unescape(substr ($ENV{'REQUEST_URI'} , 
  152: 			    rindex($ENV{'REQUEST_URI'}, '/') + 1, -4));
  153:      
  154:      # Security check on the file; the whole filename must consist
  155:      # of nothing but alphanums, ' ,, or ., or the file
  156:      # will be "not found", no matter what.
  157:      
  158:      return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
  159: 
  160:      &Apache::lonlocal::get_language_handle($r);
  161: 
  162:      # Join together the tex files, return HTTP_NOT_FOUND if any of
  163:      # them are not found
  164:      my $tex = '';
  165:      # Since in insertlist.tab I want to specify multiple files,
  166:      # and insertlist.tab also uses commas, I need something else
  167:      # so replace : with ,
  168:      $filenames =~ s/:/,/g;
  169:      my @files = split(/,/, $filenames);
  170:      
  171:      for my $filename (@files) {
  172: 	 if (-e $docroot.'/adm/help/tex/'.
  173: 	     &Apache::lonlocal::current_language().'/'.
  174: 	     $filename.'.tex') {
  175: 	     $filename=&Apache::lonlocal::current_language().'/'.$filename;
  176: 	 }
  177: 	 (my $file = Apache::File->new($docroot
  178: 			   . '/adm/help/tex/'.$filename.'.tex'))
  179: 	     or return HTTP_NOT_FOUND;
  180: 	 $tex .= join('', <$file>);
  181:      }
  182: 
  183:      if ($ENV{'browser.mathml'}) {
  184: 	 &Apache::loncommon::content_type($r,'text/xml');
  185: 	 &tth::ttminit();
  186: 	 if ($ENV{'browser.unicode'}) {
  187: 	     &tth::ttmoptions('-L -u1');
  188: 	 } else {
  189: 	     &tth::ttmoptions('-L -u0');
  190: 	 }
  191:      } else {
  192: 	 &Apache::loncommon::content_type($r,"text/html");
  193: 	 &tth::tthinit();
  194: 	 if ($ENV{'browser.unicode'}) {
  195: 	     &tth::tthoptions('-L -u1');
  196: 	 } else {
  197: 	     &tth::tthoptions('-L -u0');
  198: 	 }
  199:      }
  200: 
  201:      $r->send_http_header;
  202:      $tex = render($tex, $docroot, $serverroot);
  203:      serveTex($tex, $r);
  204: 
  205:      return OK;
  206: }
  207: 
  208: 1;

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