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