summaryrefslogtreecommitdiff
path: root/biblio/bibtex/utils/bib2xhtml/bibsearch
blob: 066199307f4cda231686b4e1b61f2b4590b84b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/local/bin/perl -T -w

# $Id: bibsearch,v 1.20 1998/02/11 19:22:30 hull Exp hull $
#
# CGI script for search bibliographies.
#
# Copyright 1995, 1996 David Hull.
# David Hull / hull@cs.uiuc.edu / http://www.uiuc.edu/ph/www/dlhull

# Call this script using the HTML form something like:
#
#  <FORM ACTION="/cgi-bin/bibsearch/path/to/database" method="POST">
#  <PRE><SMALL>
#  Keywords: <INPUT TYPE=TEXT NAME="keyword" SIZE=40>
#  Authors:  <INPUT TYPE=TEXT NAME="author" SIZE=40>
#            <INPUT TYPE=SUBMIT VALUE="Submit search"> <INPUT TYPE=RESET> <INPUT TYPE=SUBMIT NAME="help" VALUE="Help">
#  </SMALL></PRE>
#  </FORM>
#
# If the "database" input tag is omitted, $DEFAULT_BIB will be searched.
# It should *always* have a leading "/", even when tilde expansion
# is to be performed.
#
# Note: if the database file specified in the "database"
# input tag contains slashes, they should be encoded as %2F
# to insure that someone's browser does not munge them.
#
# You can also write the URL and put it directly in an HTML file.
# Here's an example:
#
#   <A HREF="/cgi-bin/bibsearch/path/to/database?author=hull"
#   >my publications</A>
#

### Configuration section.

# Path of default bibliography database, if not specified in URL.
$DEFAULT_BIB_PATH = "/usr/dcs/www/www-root/papers/index.html";

# URL of default biblipgraphy database, if not specified in URL.
$DEFAULT_BIB_URL = "http://pertsserver.cs.uiuc.edu/papers/index.html";

### End of configuration section.

%iso_map = (
    "\300", 'A',	# (192) A grave
    "\301", 'A',	# (193) A acute
    "\302", 'A',	# (194) A circumflex
    "\303", 'A',	# (195) A tilde
    "\304", 'A',	# (196) A umlaut
    "\305", 'A',	# (197) A ring
    "\306", 'AE',	# (198) AE ligature
    "\307", 'C',	# (199) C cedilla
    "\310", 'E',	# (200) E grave
    "\311", 'E',	# (201) E acute
    "\312", 'E',	# (202) E circumflex
    "\313", 'E',	# (203) E umlaut
    "\314", 'I',	# (204) I grave
    "\315", 'I',	# (205) I acute
    "\316", 'I',	# (206) I circumflex
    "\317", 'I',	# (207) I umlaut
    "\321", 'N',	# (209) N tilde
    "\322", 'O',	# (210) O grave
    "\323", 'O',	# (211) O acute
    "\324", 'O',	# (212) O circumflex
    "\325", 'O',	# (213) O tilde
    "\326", 'O',	# (214) O umlaut
    "\330", 'O',	# (216) O slash
    "\331", 'U',	# (217) U grave
    "\332", 'U',	# (218) U acute
    "\333", 'U',	# (219) U circumflex
    "\334", 'U',	# (220) U umlaut
    "\335", 'Y',	# (221) Y acute
    "\337", 'ss',	# (223) sz ligature
    "\340", 'a',	# (224) a grave
    "\341", 'a',	# (225) a acute
    "\342", 'a',	# (226) a circumflex
    "\343", 'a',	# (227) a tilde
    "\344", 'a',	# (228) a umlaut
    "\345", 'a',	# (229) a ring
    "\346", 'ae',	# (230) ae ligature
    "\347", 'c',	# (231) c cedilla
    "\350", 'e',	# (232) e grave
    "\351", 'e',	# (233) e acute
    "\352", 'e',	# (234) e circumflex
    "\353", 'e',	# (235) e umlaut
    "\354", 'i',	# (236) i grave
    "\355", 'i',	# (237) i acute
    "\356", 'i',	# (238) i circumflex
    "\357", 'i',	# (239) i umlaut
    "\361", 'n',	# (241) n tilde
    "\362", 'o',	# (242) o grave
    "\363", 'o',	# (243) o acute
    "\364", 'o',	# (244) o circumflex
    "\365", 'o',	# (245) o tilde
    "\366", 'o',	# (246) o umlaut
    "\370", 'o',	# (248) o slash
    "\371", 'u',	# (249) u grave
    "\372", 'u',	# (250) u acute
    "\373", 'u',	# (251) u circumflex
    "\374", 'u',	# (252) u umlaut
    "\375", 'y',	# (253) y acute
    "\377", 'y',	# (255) y umlaut
);
$iso_pat = join('', keys %iso_map);
$iso_pat =~ s/(\W)/\\$1/g;
$iso_pat = '[' . $iso_pat . ']';

&get_args;

if ($in{'help'}) {
    print STDOUT <<_EOF_;
Content-type: text/html

<HTML><HEAD>
<TITLE>Search Help</TITLE>
</HEAD><BODY>
<H1>Search help</H1>
To find a paper, you can specify keywords, authors, or both.
If you specify both keywords and authors, only papers that match
on both the keywords and authors will be found.
Matches are case insensitive.<P>

For the keyword field, you can specify multiple keywords separated by commas.
A paper will match if <EM>any one</EM> of the keywords matches.
A keyword will match only at the beginning of a word, so that
``do'' will match ``domain'' but not ``tandom.''
The keyword search finds words anywhere in the bibliography entry.<P>

For author field, you can specify multiply authors separated by commas.
A paper will match only if <EM>all</EM> of the authors match.
An author must match a whole word, so that ``smith'' will not match
``Smithy.''
Because first names are often abbreviated in bibliographies,
it is safer to search only on last names.<P>
</BODY></HTML>
_EOF_
    exit 0;
}

# Keywords match on any keyword.
# Keywords match only at start of word.
if (defined($in{'keyword'}) && ($in{'keyword'} ne '')) {
    $keyword = $in{'keyword'};
    local(@keywords) = split(/\s*,\s*/, $keyword);
    local($s);

    foreach $s (@keywords) {
	$s =~ s/\s+/\\s\+/g;	# Whitespace matches any amount of whitespace.
	$s =~ s/(\w+)/\\b$1/g;	# Match only at start of words.
    }
    $keyword_pat = join('|', @keywords);
}
# Authors match on all authors.
# Authors match only whole words.
if (defined($in{'author'}) && ($in{'author'} ne "")) {
    $author = $in{'author'};
    @author_pat = split(/\s*,\s*/, &map_iso($author));
    local($s);
    foreach $s (@author_pat) {
	$s =~ s/\W/ /g;		# Change non-word to whitespace.
	$s =~ s/(\w+)/\\b$1\\b/g;	# Match only at whole words.
	$s =~ s/\s+/\\s\+/g;	# Whitespace matches any amount of whitespace.
    }
}

if (defined($ENV{'PATH_INFO'})) {
    $db_url = "http://${ENV{SERVER_NAME}}${ENV{'PATH_INFO'}}";
} else {
    $db_url = $DEFAULT_BIB_URL;
}

if (defined($ENV{'PATH_TRANSLATED'})) {
    $db_file = $ENV{'PATH_TRANSLATED'};
} else {
    $db_file = $DEFAULT_BIB_PATH;
}

# Fake ".nosuch" URL in BASE tag is so that browser caching does not get
# confused (as Netscape 1.1N did without it).
if (!$in{"noheader"}) {
    print STDOUT <<_EOF_;
Content-type: text/html

<HTML><HEAD>
<BASE HREF="$db_url.nosuch">
<TITLE>Search Results</TITLE>
</HEAD>
<BODY>
<H1>Search results for
_EOF_
    print "author `$author'\n" if (defined($author));
    print "keyword `$keyword'\n" if (defined($keyword));
    print STDOUT <<_EOF_;
</H1>
Search performed on <A HREF="$db_url">$db_url</A>.<P>
<HR>
_EOF_
#    print "keyword_pat = $keyword_pat<P>\n";
#    print "author_pat = " . join(', ', @author_pat) . "<P>\n";
}

#&print_env;
#&print_request;

open(FILE, "<$db_file") || die "Can't open $db_file: $!";

$found = 0;

while (1) {
    # Skip to start of bibliography.
    while(<FILE>) {
	last if /^<!-- BEGIN BIBLIOGRAPHY/;
    }

    # Read the next couple of lines to determine what kind of list
    # the bibliography is.
    $open_list = '<UL>';
    $close_list = '</UL>';
line:
    while (<FILE>) {
	if (m/^\<[DU]L/) {
	    $close_list = $open_list = $_;
	    $close_list =~ s/^\<(\w+).*/<\/$1>/;
	}
	last line if (m/^\s*$/);
    }
    print "$open_list";

    $entry = '';
bib_entry:
    while(<FILE>) {
	last bib_entry if (/^<!-- END BIBLIOGRAPHY/);

	if (m/^$/) {
	    if (defined($keyword)) {
		if ($entry !~ m/$keyword_pat/i) {
		    $entry = ''; next bib_entry;
		}
	    }
	    if (defined($author)) {
		if ($entry !~ m/<!-- Authors: ([^-]*)-->/) {
		    $entry = ''; next bib_entry;
		}
		local($bib_authors) = $1;
		foreach $s (@author_pat) {
		    if ($bib_authors !~ m/$s/i) {
			$entry = ''; next bib_entry;
		    }
		}
	    }

	    $found = 1;

	    # Fully qualify local HREFs.
	    $entry =~ s/HREF=\"(\#[^\"]*)\"/HREF=\"$db_url$1\"/g;
	    print STDOUT $entry;
	    $entry = '';
	} else {
	    $entry .= $_;
	}
    }

    if (!$in{"noheader"}) {
	print "$close_list";
    }

    last if eof;
}

if (!$found) {
    print "No matches found.<P>\n";
}

if (!$in{"noheader"}) {
    print STDOUT <<_EOF_;
</BODY></HTML>
_EOF_
}

close(FILE);
exit;

sub print_env {
    local ($key);

    print "Environment:<BR>\n<UL>\n";
    for $key (keys(%ENV)) {
	print "<LI> $key : $ENV{$key}\n";
    }
    print "</UL><P>\n";
}

sub print_request {
    local ($key);

    print "Request:<BR>\n<UL>\n";
    for $key (keys(%in)) {
	print "<LI> $key : $in{$key}\n";
    }
    print "</UL>\n";
}

# Some of the following code stolen from James Tappin's cgi_handlers.pl

sub url_decode {
    foreach (@_) {
	tr/+/ /;
	s/%(..)/pack("c",hex($1))/ge;
    }
    @_;
}

sub get_args {
    local($request) = '';

    return if (!defined($ENV{'REQUEST_METHOD'}));

    if ($ENV{'REQUEST_METHOD'} eq "GET") {
	$request = $ENV{'QUERY_STRING'};
    } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $request, $ENV{'CONTENT_LENGTH'});
    }

    %in = &url_decode(split(/[&=]/, $request));
}

sub map_iso {
    local ($s) = @_;

    $s =~ s/($iso_pat)/$iso_map{$1}/ge;
    $s;
}