Annotation of loncom/interface/lonhelp.pm, revision 1.7
1.1 bowersj2 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);
1.5 albertel 34: use Apache::File();
35: use Apache::loncommon();
36: use Apache::lonacc();
37: use Apache::lontexconvert();
38: use tth();
39: use GDBM_File();
1.1 bowersj2 40:
41: # This sub takes the name of a label in, and converts it to something
42: # that is a valid anchor name.
43: sub processLabelName
44: {
45: my ($name) = @_;
46: $name =~ tr/a-zA-Z0-9/_/cs;
47: return $name;
48: }
49:
50: # Serve out the Tex
51: sub serveTex
52: {
53: my ($tex, $r) = @_;
54:
55: $r->print(<<HEADER);
56: <html>
57: <head>
58: <title>LON-CAPA Help</title>
59: </head>
60: <body bgcolor="#FFFFFF">
1.4 bowersj2 61: <h3 style="font: sans-serif"><img align="right"
62: src="/adm/help/gif/lonhelpheader.gif"/>LON-CAPA Help<hr /></h3>
1.2 bowersj2 63: <!-- BEGIN -->
1.1 bowersj2 64: HEADER
65:
66: $r->print($tex);
67:
68: $r->print(<<FOOTER);
1.2 bowersj2 69: <!-- END -->
1.4 bowersj2 70: <hr />
71: <center><font size="-1"><a href="/adm/help/abouthelp.html">About
72: LON-CAPA help and More Help</a></font></center>
1.1 bowersj2 73: </body>
74: </html>
75: FOOTER
76: }
77:
78: # Render takes a tex fragment, transforms it for TtH, and returns the
79: # HTML equivalent
80: sub render
81: {
1.2 bowersj2 82: my ($tex, $docroot, $serverroot) = @_;
83: tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
1.1 bowersj2 84:
85: # This tells TtH what to do with captions, labels, and other
86: # things
87: $tex = "\\documentclass{article}\n" . $tex;
88:
89: # We process these ourselves because TtH can't handle then without
90: # LaTeX .aux files
1.2 bowersj2 91: # absolute paths for use with help.loncapa.org
1.1 bowersj2 92: $tex =~ s| \\ref\{([^}]*)\}
1.2 bowersj2 93: |'\\begin{html}<a href="http://' . $serverroot ."/adm/help/".
1.1 bowersj2 94: substr($fragmentLabels{$1}, 0, -4) .
1.2 bowersj2 95: '.hlp#' . processLabelName($1) .
96: '"><img src="http://' . $serverroot . '/adm/help/gif/smallHelp.gif" border="0" /></a>' .
97: '\\end{html}'
1.1 bowersj2 98: |gxe;
99:
1.3 bowersj2 100: # Backslashes
101: $tex =~ s|\\textbackslash|###BACKSLASH###|g;
102:
1.1 bowersj2 103: # Figures leftover without captions
104: $tex =~ s| \\includegraphics(\[[^]]*\])*\{([^}]*)\}
1.2 bowersj2 105: | '\\begin{html}<img src="http://' . $serverroot . '/adm/help/gif/' . $2 . '.gif" border="2"'.
1.1 bowersj2 106: ' bordercolor="#000000"/>\\end{html}'
107: |gxe;
108:
1.3 bowersj2 109:
1.5 albertel 110: $tex=&Apache::lontexconvert::converted(\$tex);
111:
1.3 bowersj2 112: # Finish backslashes
113: $tex =~ s/###BACKSLASH###/'\\'/ge;
114:
115: # Fix the pretty quotes
116: $tex =~ s/('')|(``)/"/g; #" to get emacs syntax highlighter happy
1.1 bowersj2 117:
118: # For some reason all captions come out as "Figure 0:", so
119: # just duck the issue...
120:
121: $tex =~ s/Figure 0://g;
1.5 albertel 122: $tex.=$Apache::lontexconvert::errorstring;
1.1 bowersj2 123: untie %fragmentLabels;
124:
125: return $tex;
126: }
127:
128: sub handler
129: {
130: my $r = shift;
131:
1.2 bowersj2 132: my $docroot = $r->dir_config('lonDocRoot');
133: my $serverroot = $ENV{'HTTP_HOST'};
134:
1.6 bowersj2 135: my $filenames = substr ($ENV{'REQUEST_URI'} ,
1.1 bowersj2 136: rindex($ENV{'REQUEST_URI'}, '/') + 1, -4);
137:
138: # Security check on the file; the whole filename must consist
139: # of nothing but alphanums, ' ,, or ., or the file
140: # will be "not found", no matter what.
141:
1.7 ! bowersj2 142: return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
1.1 bowersj2 143:
1.6 bowersj2 144: # Join together the tex files, return HTTP_NOT_FOUND if any of
145: # them are not found
146: my $tex = '';
1.7 ! bowersj2 147: # Since in insertlist.tab I want to specify multiple files,
! 148: # and insertlist.tab also uses commas, I need something else
! 149: # so replace ! with ,
! 150: $filenames =~ s/:/,/g;
1.6 bowersj2 151: my @files = split(/,/, $filenames);
152:
153: for my $filename (@files) {
154: (my $file = Apache::File->new($docroot
155: . '/adm/help/tex/'.$filename.'.tex'))
156: or return HTTP_NOT_FOUND;
157: $tex .= join('', <$file>);
158: }
1.5 albertel 159:
160: # get me my environment if it exists
161: &Apache::lonacc::handler($r);
162:
163: if ($ENV{'browser.mathml'}) {
164: $r->content_type('text/xml');
165: &tth::ttminit();
166: if ($ENV{'browser.unicode'}) {
167: &tth::ttmoptions('-L -u1');
168: } else {
169: &tth::ttmoptions('-L -u0');
170: }
171: } else {
172: $r->content_type("text/html");
173: &tth::tthinit();
174: if ($ENV{'browser.unicode'}) {
175: &tth::tthoptions('-L -u1');
176: } else {
177: &tth::tthoptions('-L -u0');
178: }
179: }
180:
1.2 bowersj2 181: $tex = render($tex, $docroot, $serverroot);
1.1 bowersj2 182: serveTex($tex, $r);
183:
184: return OK;
185: }
186:
187: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>