/[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 86 by dpavlin, Mon Aug 30 11:16:39 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            # maximum length of context in characters
15            my $cc = 50;
16    
17          my $desc = shift || return '';          my $desc = shift || return '';
18          $desc = e($desc);          $desc = e($desc);
19    
20          # test if $desc contains any of our query words          # sort words from longer to shorter (for hilighting later)
21          my @snips;          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          # construct regex          # construct regex
27          my $re = qq/^.*?(.{$context_chars}?)(\Q/ . join("|",@_) .          my $re = qq/^(.*?\\b)(/ . join('|', @words) . qq/)/;
28                  qq/\E)(.{$context_chars})/;  
29            my $ellip = ' ... ';
30            my $snippet = '';
31    
32    #print "<ul>";
33    
34          while ($desc =~ s/$re//si) {          while ($desc =~ s/$re//si) {
35                  my ($bef,$qm,$af) = ($1, $2, $3);                  my ($foo, $match) = ($1,$2);
                           
                 # no partial words...  
                 $bef =~ s,^\S+\s+|\s+\S+$,,gs;  
                 $af =~ s,^\S+\s+|\s+\S+$,,gs;  
36    
37                  push @snips, "$bef $qm $af";  #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    
63                    }
64    
         my $ellip = ' ... ';  
         my $snippet;  
           
         if (@snips) {  
                 $snippet = $ellip. join($ellip, @snips) . $ellip;  
         } else {  
                 return '';  
65          }          }
66    #print "</ul>";
67    
68          # color offset          # color offset
69          my $i = 0;          my $i = 0;
70    
71          foreach my $w (@_) {          foreach my $w (@words) {
72                  $snippet =~ s,(\Q$w\E),<span style="background: $colors[$i]; color:black;">$1</span>,gsi;                  $snippet =~ s,(\b\Q$w\E),<span style="background: $colors[$i]; color:black;">$1</span>,gsi;
73                  $i++;                  $i++;
74                  $i = 0 if ($i > $#colors);                  $i = 0 if ($i > $#colors);
75          }          }
76    
77            $snippet .= $ellip if ($snippet);
78    
79          return $snippet;          return $snippet;
80  }  }
81    
# Line 158  if (param('search')) { Line 181  if (param('search')) {
181          $search =~ s/^\s+//;          $search =~ s/^\s+//;
182          $search =~ s/\s+$//;          $search =~ s/\s+$//;
183          # fixup search string          # fixup search string
184          $search=~tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/;     # 1250 -> iso8859-2          $search=~tr/¹ð¾èæ©Ð®ÈÆ/¹ð¾èæ©Ð®ÈÆ/;     # 1250 -> iso8859-2
185          $search=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;          $search=~tr/¹©ðÐèÈæƾ®/sSdDcCcCzZ/;
186    
187          # extract phrases and put them first          # extract phrases and put them first

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

  ViewVC Help
Powered by ViewVC 1.1.26