Annotation of loncom/interface/lonhelp.pm, revision 1.3
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);
34: use Apache::File;
35: use Apache::loncommon;
36: use tth;
37: use GDBM_File;
38:
39: # This sub takes the name of a label in, and converts it to something
40: # that is a valid anchor name.
41: sub processLabelName
42: {
43: my ($name) = @_;
44: $name =~ tr/a-zA-Z0-9/_/cs;
45: return $name;
46: }
47:
48: # Serve out the Tex
49: sub serveTex
50: {
51: my ($tex, $r) = @_;
52:
53: $r->print(<<HEADER);
54: <html>
55: <head>
56: <title>LON-CAPA Help</title>
57: </head>
58: <body bgcolor="#FFFFFF">
1.2 bowersj2 59: <!-- BEGIN -->
1.1 bowersj2 60: HEADER
61:
62: $r->print($tex);
63:
64: $r->print(<<FOOTER);
1.2 bowersj2 65: <!-- END -->
1.1 bowersj2 66: </body>
67: </html>
68: FOOTER
69: }
70:
71: # Render takes a tex fragment, transforms it for TtH, and returns the
72: # HTML equivalent
73: sub render
74: {
1.2 bowersj2 75: my ($tex, $docroot, $serverroot) = @_;
76: tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
1.1 bowersj2 77:
78: # This tells TtH what to do with captions, labels, and other
79: # things
80: $tex = "\\documentclass{article}\n" . $tex;
81:
82: # We process these ourselves because TtH can't handle then without
83: # LaTeX .aux files
1.2 bowersj2 84: # absolute paths for use with help.loncapa.org
1.1 bowersj2 85: $tex =~ s| \\ref\{([^}]*)\}
1.2 bowersj2 86: |'\\begin{html}<a href="http://' . $serverroot ."/adm/help/".
1.1 bowersj2 87: substr($fragmentLabels{$1}, 0, -4) .
1.2 bowersj2 88: '.hlp#' . processLabelName($1) .
89: '"><img src="http://' . $serverroot . '/adm/help/gif/smallHelp.gif" border="0" /></a>' .
90: '\\end{html}'
1.1 bowersj2 91: |gxe;
92:
1.3 ! bowersj2 93: # Backslashes
! 94: $tex =~ s|\\textbackslash|###BACKSLASH###|g;
! 95:
1.1 bowersj2 96: # Figures leftover without captions
97: $tex =~ s| \\includegraphics(\[[^]]*\])*\{([^}]*)\}
1.2 bowersj2 98: | '\\begin{html}<img src="http://' . $serverroot . '/adm/help/gif/' . $2 . '.gif" border="2"'.
1.1 bowersj2 99: ' bordercolor="#000000"/>\\end{html}'
100: |gxe;
101:
102: $tex = tth::tth($tex);
1.3 ! bowersj2 103:
! 104: # Finish backslashes
! 105: $tex =~ s/###BACKSLASH###/'\\'/ge;
! 106:
! 107: # Fix the pretty quotes
! 108: $tex =~ s/('')|(``)/"/g; #" to get emacs syntax highlighter happy
1.1 bowersj2 109:
110: # For some reason all captions come out as "Figure 0:", so
111: # just duck the issue...
112:
113: $tex =~ s/Figure 0://g;
114:
115: untie %fragmentLabels;
116:
117: return $tex;
118: }
119:
120: sub handler
121: {
122: my $r = shift;
123:
1.2 bowersj2 124: my $docroot = $r->dir_config('lonDocRoot');
125: my $serverroot = $ENV{'HTTP_HOST'};
126:
1.1 bowersj2 127: my $filename = substr ($ENV{'REQUEST_URI'} ,
128: rindex($ENV{'REQUEST_URI'}, '/') + 1, -4);
129:
130: # Security check on the file; the whole filename must consist
131: # of nothing but alphanums, ' ,, or ., or the file
132: # will be "not found", no matter what.
133:
134: return 404 if ($filename !~ /\A[-0-9a-zA-z_'',.]+\Z/);
135:
1.2 bowersj2 136: (my $file = Apache::File->new($docroot . "/adm/help/tex/".$filename.'.tex'))
1.1 bowersj2 137: or return 404;
138: my $tex = join('', <$file>);
1.2 bowersj2 139: $tex = render($tex, $docroot, $serverroot);
1.1 bowersj2 140: $r->content_type("text/html");
141: serveTex($tex, $r);
142:
143: return OK;
144: }
145:
146: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>