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