1: # The LearningOnline Network with CAPA
2: #
3: # $Id: lonhelp.pm,v 1.49 2023/04/06 17:36:12 raeburn 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,$uri,$text,$is_mobile,$firstfile) = @_;
61: my $bugs;
62: my %helpconfig = &Apache::lonnet::get_dom('configuration',['helpsettings'],
63: $env{'request.role.domain'});
64: if (ref($helpconfig{'helpsettings'}) eq 'HASH') {
65: if ($helpconfig{'helpsettings'}{'submitbugs'} eq '1') {
66: $bugs = &Apache::loncommon::help_open_bug('Documentation',&mt('Report a documentation bug'));
67: }
68: }
69: my %lt = &Apache::lonlocal::texthash(
70: header => 'LON-CAPA Help',
71: search => 'Search LON-CAPA help',
72: query => 'Search',
73: );
74: my $goback;
75: if ((($env{'request.lti.login'}) && ($env{'request.lti.target'} eq 'iframe')) ||
76: (($env{'request.deeplink.login'}) && ($env{'request.deeplink.target'} eq '_self'))) {
77: my $backicon = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL').'/').'clickhere.gif';
78: $goback = '<a href="javascript:history.go(-1);" class="LC_menubuttons_link" title="Go Back">'.
79: '<img src="'.$backicon.'" alt="'.&mt('Go Back').'" class="LC_icon" border="0" />'.
80: '</a> ';
81: }
82: $r->print(<<HEADER);
83: <h3 style="font: sans-serif"><img align="right" alt="help logo"
84: src="/adm/help/gif/lonhelpheader.gif" />$goback$lt{'header'}</h3><hr />
85: <!-- BEGIN -->
86: HEADER
87: if ($is_mobile) {
88: my $width = 500;
89: my $height = 400;
90: my $machine = &Apache::lonnet::absolute_url();
91: $r->print(&Apache::loncommon::nicescroll_javascript('helpwrapper',
92: {cursorcolor => '#00F',
93: railalign => 'right',
94: railoffset => '{top:5,left:40}'},
95: undef,1,$machine.$firstfile));
96: $r->print('<div id="helpwrapper" style="height:'.$height.'px; width:'.$width.'px; overflow: auto;">'.
97: $text.
98: '</div>');
99: } else {
100: $r->print($text);
101: }
102:
103: if (&Apache::lonnavmaps::advancedUser()) {
104: $r->print(<<FOOTER);
105: <hr /><div class="LC_left_float">
106: <form action="$uri" method="post">
107: <fieldset><legend>$lt{'search'}</legend>
108: <input type="text" name="searchterm" size="40" />
109: <input type="submit" value="$lt{'query'}" />
110: </fieldset>
111: </form>
112: </div>
113: <div class="LC_left_float">
114: </div>
115: <div style="padding:0;clear:both;margin:0;border:0"></div>
116: $bugs
117: FOOTER
118: }
119:
120: $r->print(<<ENDBODY);
121: <!-- END -->
122: ENDBODY
123:
124: }
125:
126: # Render takes a tex fragment, transforms it for TtH, and returns the
127: # HTML equivalent
128: sub render {
129: my ($tex, $docroot) = @_;
130: tie (my %fragmentLabels, 'GDBM_File', $docroot . '/adm/help/fragmentLabels.gdbm', 0, 0);
131:
132: # This tells TtH what to do with captions, labels, and other
133: # things
134: $tex = "\\documentclass{article}\n" . $tex;
135:
136: # We process these ourselves because TtH can't handle then without
137: # LaTeX .aux files
138: # absolute paths for use with help.loncapa.org
139: $tex =~ s| \\ref\{([^}]*)\}
140: |
141: my $label=$1;
142: my $icon='/adm/help/help.png';
143: my $ext;
144: if ($1!~/\.hlp$/) {
145: if (($1 =~ /^\w+\.manual\.pdf$/) && (-e $docroot.'/adm/help/'.$1)) {
146: $icon = '/adm/lonIcons/pdf.gif';
147: } elsif ((!exists($fragmentLabels{$1})) && ($1!~/\.hlp$/)) {
148: &Apache::lonnet::logthis("ERROR: $1 not a valid help label");
149: $label='Error';
150: } else {
151: $label=substr($fragmentLabels{$1}, 0, -4);
152: $ext = '.hlp#' . &processLabelName($1);
153: }
154: } else {
155: $label=~s/\.hlp$//;
156: $ext = '.hlp#' . &processLabelName($1);
157: }
158: '\\begin{html}<a href="/adm/help/'.
159: $label .
160: $ext .
161: '"><img src="'.$icon.'" border="0" alt="'.&mt('Help').'" /></a>' .
162: '\\end{html}'
163: |gxe;
164:
165: # Backslashes
166: $tex =~ s|\\textbackslash|###BACKSLASH###|g;
167:
168: # Figures leftover without captions
169: $tex =~ s| \\includegraphics(\[[^]]*\])*\{([^}]*)\}
170: | '\\begin{html}<img src="/adm/help/gif/' . $2 . '.gif" border="2"'.
171: ' bordercolor="#000000"/>\\end{html}'
172: |gxe;
173:
174:
175: $tex=&Apache::lontexconvert::tth_converted(\$tex);
176:
177: # Finish backslashes
178: $tex =~ s/###BACKSLASH###/'\\'/ge;
179:
180: # Fix the pretty quotes
181: $tex =~ s/('')|(``)/"/g; #" to get emacs syntax highlighter happy
182:
183: $tex =~ s/`/'/g;
184:
185: # For some reason all captions come out as "Figure 0:", so
186: # just duck the issue...
187:
188: $tex =~ s/Figure 0://g;
189: $tex.=$Apache::lontexconvert::errorstring;
190: untie %fragmentLabels;
191:
192: return $tex;
193: }
194:
195: sub listmatches {
196: my ($docroot,$term,$subdir) =@_;
197: unless ($subdir) { $subdir=''; }
198: my $output='';
199: opendir(DIR,$docroot.'/adm/help/tex/'.$subdir);
200: foreach my $filename (sort readdir(DIR)) {
201: if ($filename=~/\.tex$/) {
202: open(FH,$docroot.'/adm/help/tex/'.$subdir.$filename);
203: my $quote='';
204: while (my $line=<FH>) {
205: if ($line=~/\Q$term\E/i) {
206: $line=~s/\\\w+//gs;
207: $line=~s/\{//gs;
208: $line=~s/\}//gs;
209: $line=~s/\\/ /gs;
210: $line=~s/\</\<\;/gs;
211: $line=~s/\>/\>\;/gs;
212: $line=~s/(\Q$term\E)/\<b\>$1\<\/b\>/gsi;
213: $quote.='<br />...'.$line.'...';
214: }
215: }
216: close(FH);
217: if ($quote) {
218: my $title=$filename;
219: $title=~s/\_/ /gs;
220: $title=~s/\.tex$//;
221: $filename=~s/\.tex$/\.hlp/;
222: $output.='<li><a href="/adm/help/tex/'.$subdir.$filename.'">'.$title.'</a>'.$quote.'</li>';
223: }
224: }
225: }
226: closedir(DIR);
227: return (($output?'<ul>'.$output.'</ul>':&mt('"[_1]" not found',$term)),$output);
228: }
229:
230: sub handler {
231: my $r = shift;
232:
233: my $docroot = $r->dir_config('lonDocRoot');
234:
235: &Apache::lonlocal::get_language_handle($r);
236: &Apache::loncommon::content_type($r,"text/html");
237: my $caller;
238: if ($env{'form.searchterm'}=~/\w/) {
239: $caller = 'search';
240: }
241:
242: my $starthash;
243:
244: if ($env{'browser.mobile'}) {
245: $starthash = {
246: only_body => 1,
247: add_entries => {
248: 'onload' => "javascript:expand_div('$caller');",
249: },
250: };
251: } else {
252: $starthash = {
253: only_body => 1,
254: };
255: }
256: my $firstfile;
257: $env{'form.texengine'} = 'tth';
258: my $start_page=
259: &Apache::loncommon::start_page('LON-CAPA Help',undef,$starthash);
260: delete($env{'form.texengine'});
261: my $text='';
262: my $uri = $r->uri;
263: if ($env{'form.searchterm'}=~/\w/) {
264: ($text,my $matches)=&listmatches($docroot,$env{'form.searchterm'},&Apache::lonlocal::current_language().'/');
265: if ($matches) {
266: my ($englishresult,$englishmatches)=&listmatches($docroot,$env{'form.searchterm'});
267: if ($englishmatches) {
268: $text.='<hr />'.$englishresult;
269: }
270: } else {
271: $text=&listmatches($docroot,$env{'form.searchterm'});
272: }
273: } else {
274: my $filenames = &unescape(substr($uri,rindex($uri,'/')+1,-4));
275:
276: # Security check on the file; the whole filename must consist
277: # of nothing but alphanums, ' ,, or ., or the file
278: # will be "not found", no matter what.
279:
280: return HTTP_NOT_FOUND if ($filenames !~ /\A[-0-9a-zA-z_'',:.]+\Z/);
281:
282: # Join together the tex files, return HTTP_NOT_FOUND if any of
283: # them are not found
284: my $tex = '';
285: # Since in insertlist.tab I want to specify multiple files,
286: # and insertlist.tab also uses commas, I need something else
287: # so replace : with ,
288: $filenames =~ s/:/,/g;
289: my @files = split(/,/, $filenames);
290: $firstfile = '/adm/help/'.$files[0].'.hlp';
291: my $count = 0;
292:
293: for my $filename (@files) {
294: if (-e $docroot.'/adm/help/tex/'.
295: &Apache::lonlocal::current_language().'/'.
296: $filename.'.tex') {
297: $filename=&Apache::lonlocal::current_language().'/'.$filename;
298: }
299: (my $file = Apache::File->new($docroot
300: . '/adm/help/tex/'.$filename.'.tex'))
301: or return HTTP_NOT_FOUND;
302: $tex .= join('', <$file>);
303: $count ++;
304: if (scalar(@files) > $count) {
305: $tex .= '\hrulefill';
306: }
307: }
308:
309: $text = &render($tex, $docroot);
310: }
311:
312: $r->send_http_header;
313: $r->print($start_page);
314: &servetext($r,$uri,$text,$env{'browser.mobile'},$firstfile);
315: $r->print(&Apache::loncommon::end_page());
316:
317: return OK;
318: }
319:
320: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>