/[bfilter]/trunk/bfilter.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/bfilter.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (show annotations)
Wed Sep 15 15:30:04 2004 UTC (19 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 1997 byte(s)
major refacture: 0 element is searched, all others can be used in result
function to generate html specific to one result. All that will be displayed
by calling display function which can add additional markup before or after
results.

1 #!/usr/bin/perl -w
2 #
3
4 use strict;
5 use locale;
6
7 # maximum entries
8 my $max = 0;
9 # minimum letters to search by
10 my $min_len = shift @ARGV || 3;
11 # if more than x elements, warn to increase min_len
12 my $increase_at = 500;
13
14 # name of generated index
15 my $headlines = 'headlines';
16
17 my $debug = 1;
18
19 sub print_file {
20 my $f = shift || return;
21 open(F, $f) || die "$f: $!";
22 while(<F>) {
23 print;
24 }
25 close(F);
26 }
27
28 print qq{
29 var $headlines = new Object();
30 };
31
32 my @part_arr;
33 my $last_part = '';
34 my $total = 0;
35
36 my $max_elements = 0;
37
38 sub escape_js {
39 my $t = shift || return;
40 $t =~ s/(['\\])/\\$1/g && print STDERR "ESCAPED '$t'\n";
41 return $t;
42 }
43
44 while(<STDIN>) {
45 chomp;
46
47 if (!m/\t/ || m/\t$/) {
48 print STDERR "SKIP '$_': no tab\n";
49 next;
50 }
51
52 my @data = split(/\t+/,$_);
53
54 my $headline = shift @data || die "need at least headline!";
55
56 if (length($headline) < $min_len) {
57 print STDERR "SKIP '$_': too short\n";
58 next;
59 }
60
61
62 # split into min_len part and rest
63 my ($part,$rest) = ( substr($headline,0,$min_len), substr($headline,$min_len) );
64
65 # make part lowercase
66 $part = lc($part);
67
68 $last_part = $part if (! $last_part);
69
70 # new part?
71 if ($part ne $last_part) {
72 print STDERR $last_part,"\t",$#part_arr+1,"\n" if ($debug && $#part_arr > $increase_at);
73 $max_elements = $#part_arr if ($#part_arr > $max_elements);
74 print "${headlines}['",escape_js($last_part),"'] = [\n ",join(",\n ",@part_arr),"];\n" if (@part_arr);
75 $total += $#part_arr;
76 @part_arr = ();
77 $last_part = $part;
78 }
79 push @part_arr, "['".escape_js($headline)."','".join("','",map { escape_js($_) } @data)."']";
80
81 # break out?
82 last if ($max && $total > $max);
83 }
84
85 print "${headlines}['",escape_js($last_part)."'] = [\n ",join(",\n ",@part_arr),"];\n" if (@part_arr);
86 print qq{
87
88 ${headlines}.min_len = $min_len;
89 ${headlines}.length = $total;
90
91 };
92
93 print STDERR "You have more than $increase_at elements, so you should\nincrease min_len to ",$min_len+1," or higher for performance benefit.\n" if ($max_elements > $increase_at);

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26