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

Diff of /trunk/html/swish.cgi

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 82 by dpavlin, Sun Aug 29 18:17:15 2004 UTC revision 87 by dpavlin, Mon Aug 30 16:59:17 2004 UTC
# Line 9  use Lingua::Spelling::Alternative; Line 9  use Lingua::Spelling::Alternative;
9  use Text::Iconv;  use Text::Iconv;
10  use Data::Pageset;  use Data::Pageset;
11    
   
12  sub get_snippet {  sub get_snippet {
13          my $context_chars = 100;  
14          my $max_desc = 16384;   # 16k to filter          # maximum length of context in characters
15            my $cc = 50;
16    
17          my $desc = shift || return '';          my $desc = shift || return '';
18          $desc = substr($desc,0,$max_desc) if (length($desc) > $max_desc);          $desc = e($desc);
         # test if $desc contains any of our query words  
         my @snips;  
19    
20            # sort words from longer to shorter (for hilighting later)
21            my @words = sort { length($b) <=> length($a) } @_;
22    
23            # colors to highlite
24          my @colors = qw{#ffff66 #a0ffff #99ff99 #ff9999 #ff66ff};          my @colors = qw{#ffff66 #a0ffff #99ff99 #ff9999 #ff66ff};
25    
26          my $i = 0;          # construct regex
27            my $re = qq/^(.*?\\b)(/ . join('|', @words) . qq/)/;
28    
29            my $ellip = ' ... ';
30            my $snippet = '';
31    
32    #print "<ul>";
33    
34            while ($desc =~ s/$re//si) {
35                    my ($foo, $match) = ($1,$2);
36    
37    #print "<br>desc: <small>$desc</small>\n";
38    #print "<br>foo: <small>$foo<b>$match</b></small>\n";
39    
40                    if (length($foo) < $cc * 2) {
41                            $snippet .= $foo . $match;
42                    } else {
43    
44                            if ($foo =~ m/^(.{0,$cc})(\s.*?\s|\s|)?(.{0,$cc})$/) {
45    
46    #       print "<li><small>$snippet</small><br>
47    #       ",length($1),": <i>$1</i><br>
48    #       ",length($2),": <span style=\"color:grey\">$2</span><br>
49    #       ",length($3),": <i>$3</i><br>
50    #       <b>$match</b>\n";
51    
52                                    if ($snippet) {
53                                            $snippet .= $1 . $ellip . $3 . $match;
54                                    } else {
55                                            $snippet = $ellip . $3 . $match ;
56                                    }
57    
58                            } else {
59    #                               print "<li> <big>SKIP</big> $foo\n";
60                                    print STDERR "this shouldn't happen!\n";
61                            }
62    
         for my $q (@_) {  
                 if ($desc =~ m/(.*?)(\Q$q\E)(.*)/si) {  
                         my $bef = $1;  
                         my $qm = $2;  
                         my $af = $3;  
                         $bef = substr $bef, -$context_chars;  
                         $af = substr $af, 0, $context_chars;  
                           
                         # no partial words...  
                         $af =~ s,^\S+\s+|\s+\S+$,,gs;  
                         $bef =~ s,^\S+\s+|\s+\S+$,,gs;  
   
                         push(@snips, "$bef <span style=\"background:".$colors[$i]."; color:black;\">$qm</span> $af");  
                         $i++;  
                         $i = 0 if ($i > $#colors);  
63                  }                  }
64    
65          }          }
66          my $ellip = ' ... ';  #print "</ul>";
67          my $snippet = $ellip. join($ellip, @snips) . $ellip if (@snips);  
68              # color offset
69            my $i = 0;
70    
71            foreach my $w (@words) {
72                    $snippet =~ s,(\b\Q$w\E),<span style="background: $colors[$i]; color:black;">$1</span>,gsi;
73                    $i++;
74                    $i = 0 if ($i > $#colors);
75            }
76    
77            $snippet .= $ellip if ($snippet);
78    
79          return $snippet;          return $snippet;
80  }  }
81    
# Line 108  $path .= '"'.join('*" or "',param('path' Line 141  $path .= '"'.join('*" or "',param('path'
141  my %path_label;  my %path_label;
142  my @path_name;  my @path_name;
143  foreach (@{$config->{paths}->{path}}) {  foreach (@{$config->{paths}->{path}}) {
   
 print STDERR "##: $_->{limit}",x($_->{content}),"\n";  
144          push @path_name,x($_->{limit});          push @path_name,x($_->{limit});
145          $path_label{$_->{limit}} = x($_->{content});          $path_label{$_->{limit}} = x($_->{content});
146  }  }
# Line 142  if (param('search')) { Line 173  if (param('search')) {
173          my $s;          my $s;
174          # re-write query from +/- to and/and not          # re-write query from +/- to and/and not
175    
176            my @s_elem;
177    
178          my $search = param('search');          my $search = param('search');
179          my $s_phrase = "";  
180            # strip spaces
181            $search =~ s/^\s+//;
182            $search =~ s/\s+$//;
183            # fixup search string
184            $search=~tr/¹ð¾èæ©Ð®ÈÆ/¹ð¾èæ©Ð®ÈÆ/;     # 1250 -> iso8859-2
185            $search=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;
186    
187            # extract phrases and put them first
188          while ($search =~ s/\s*("[^"]+")\s*/ /) {          while ($search =~ s/\s*("[^"]+")\s*/ /) {
189                  $s .= "$1 ";                  $s .= "$1 ";
190                    push @s_elem, $1;
191          }          }
         $search =~ s/^\s+//;  
         $search =~ s/\s+$//;  
192    
193          my %words;          my %words;
194    
# Line 167  if (param('search')) { Line 207  if (param('search')) {
207                          } else {                          } else {
208                                  $s.="$2* ";                                  $s.="$2* ";
209                          }                          }
210                            push @s_elem, $2;
211                  } else {                  } else {
212                          if (@spellings && !param('no_affix')) {                          if (@spellings && !param('no_affix')) {
213                                  my $w = $_; $w =~ s/[\*\s]+//g;                                  my $w = $_; $w =~ s/[\*\s]+//g;
                                 #$s.="(".join("* or ",$spelling_alt->alternatives($w))."*) ";  
214                                  my $or="";                                  my $or="";
215                                  foreach my $spelling_alt (@spellings) {                                  foreach my $spelling_alt (@spellings) {
216                                          $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";                                          $s.="$or(".join("* or ",$spelling_alt->alternatives($w))."*) ";
# Line 179  if (param('search')) { Line 219  if (param('search')) {
219                          } else {                          } else {
220                                  $s.="$_* ";                                  $s.="$_* ";
221                          }                          }
222                            push @s_elem, $_;
223                  }                  }
224          }          }
225    
226          # fixup search string          # fix multiple stars
         $s=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/;  # 1250 -> iso8859-2  
         $s=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;  
227          $s=~s/\*\*+/*/g;          $s=~s/\*\*+/*/g;
228    
229          # limit to some path          # limit to some path
# Line 195  if (param('search')) { Line 234  if (param('search')) {
234          # default format for output          # default format for output
235          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";          my $hit_fmt = "<a href=\"%s\">%s</a> [%s]<br>\n";
236    
         # output start of table  
         print qq{  
 <table border="0">  
         };  
         # html before and after each hit  
         my $tr_pre = qq{  
 <tr><td>  
         };  
         my $tr_post = qq{  
 </td></tr>  
         };  
   
237          if (@properties) {          if (@properties) {
238                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));                  $hit_fmt = x($config->{hit}) if (! param('no_properties'));
239                  $params{properties} = \@properties;                  $params{properties} = \@properties;
# Line 243  if (param('search')) { Line 270  if (param('search')) {
270          }          }
271    
272          my %path2title;          my %path2title;
         use Data::Dumper;  
273          foreach my $p (@{$config->{path2title}->{path}}) {          foreach my $p (@{$config->{path2title}->{path}}) {
274                  $path2title{$p->{dir}} = $p->{content};                  $path2title{$p->{dir}} = $p->{content};
275          }          }
276    
277            # output start of table
278            print qq{
279    <table border="0">
280            };
281            # html before and after each hit
282            my $tr_pre = qq{
283    <tr><td>
284            };
285            my $tr_post = qq{
286    </td></tr>
287            };
288    
289          for(my $i=$pager->first; $i<=$pager->last; $i++) {          for(my $i=$pager->first; $i<=$pager->last; $i++) {
290    
291                  my $result = $results->NextResult;                  my $result = $results->NextResult;
# Line 259  if (param('search')) { Line 297  if (param('search')) {
297                          if ($prop =~ m/swishdescription/) {                          if ($prop =~ m/swishdescription/) {
298                                  my $tmp = get_snippet(                                  my $tmp = get_snippet(
299                                          $result->Property($prop),                                          $result->Property($prop),
300                                          split(/\s+/,$search)                                          @s_elem,
301                                  );                                  );
302                                                                    
303                                  push @arr, $tmp;                                  push @arr, $tmp;

Legend:
Removed from v.82  
changed lines
  Added in v.87

  ViewVC Help
Powered by ViewVC 1.1.26