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

Annotation of /trunk/spider/filter.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 69 - (hide annotations)
Thu Mar 18 23:07:21 2004 UTC (20 years ago) by dpavlin
File size: 3619 byte(s)
more verbose adding of titles

1 dpavlin 46 sub filter {
2     my $contents = shift || return;
3 dpavlin 57
4 dpavlin 61 my $verbose = 0;
5 dpavlin 57
6 dpavlin 46 # if you don't want content to be indexed, include it in
7     # <noindex> foobar </noindex> tags or surround it with comments
8     # <!-- noindex --> foobar <!-- /noindex -->
9     # <!-- noindex --> foobar <!-- index --> (also supported by swish)
10     $contents =~ s,<noindex>.+?</noindex>,,isg;
11     $contents =~ s,<!--\s*noindex\s*-->.+?<!--\s*/noindex\s*-->,,isg;
12     $contents =~ s,<!--\s*noindex\s*-->.+?<!--\s*index\s*-->,,isg;
13     # this will remove all script from indexing content
14 dpavlin 51 $contents =~ s,<script[^>]*>.+?</script>,,isg;
15 dpavlin 46 # remap Windows charset to ISO-8859-2
16     $contents =~ tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/; # 1250 -> iso8859-2
17     # this will fix badly formatted html in form:
18     # <head><title>some text</title
19     # ></head>
20     # which will confuse indexer (or libxml2?)
21     $contents =~ s/[\n\r]^(>)/$1\n/msg;
22     # remove comments between <html> and <head> texi2html inserts them
23     # there and swish can't find document title then (libxml or swish bug?)
24     while ($contents =~ s/(<html>.*)<!--.*?-->(.*<head>)/$1$2/msi) { };
25    
26     # remote TPJ left column
27     if ($contents =~ s,<!-- BEGIN LEFT SIDE BAR CELL -->.+?<!-- END LEFT SIDE BAR CELL -->,,isg) {
28     my $title;
29     # extract title and add to title
30     if ($contents =~ m,<!-- the article goes here -->\s*<h2[^>]*>(.+?)</h2>,si) {
31     $title = $1;
32     } elsif ($contents =~ m,<h1[^>]*>(.+?)</h1>,is) {
33     $title = $1;
34     } elsif ($contents =~ m,<h2[^>]*>(.+?)</h2>,is) {
35     $title = $1;
36     } else {
37     $title = "no detail title";
38     }
39     $contents =~ s,(<title>)([^<]+)(</title>),$1$2: $title$3,gsi if ($title);
40    
41     }
42 dpavlin 51
43 dpavlin 56 # is second argument collection?
44     my $collection = shift || return $contents;
45    
46     # construct new title (from various parts of DocBook if available)
47     my $new_title;
48    
49     if ($contents =~ m,<!--SafTocEntry="([^"]+)"-->,is) {
50     $new_title = $1;
51 dpavlin 65 } elsif ($contents =~ m,<(h\d)\sclass="docPartTitle"[^>]*>(.+?)<\1>,is) {
52     $new_title = $2;
53     } elsif ($contents =~ m,<(h\d)\sclass="docChapterTitle"[^>]*>(.+?)<\1>,is) {
54     $new_title = $2;
55     } elsif ($contents =~ m,<(h\d)\sclass="docSection1Title"[^>]*>(.+?)<\1>,is) {
56     $new_title = $2;
57     } elsif ($contents =~ m,<(h\d)\sclass="chapter"[^>]*>(.+?)<\1>,is) {
58     $new_title = $2;
59     } elsif ($contents =~ m,<(h\d)\sclass="sect1"[^>]*>(.+?)<\1>,is) {
60     $new_title = $2;
61 dpavlin 56 } else {
62     if ($contents =~ m,<title>([^<]+)</title>,is) {
63     $new_title = $1;
64     } elsif ($contents =~ m,<h\d[^>]*>([^<]+)</h\d>,is) {
65     $new_title = $1;
66     }
67     }
68    
69     if ($new_title) {
70 dpavlin 61 # nuke html in title
71     $new_title =~ s/<br>\s+/: /gs;
72     $new_title =~ s/<\/*[^>]+>//gs;
73    
74 dpavlin 56 # check if new title is same as collection name
75     my ($a,$b) = ($new_title,$collection);
76     $a =~ s/([^a-zA-Z])+/ /gs;
77     $b =~ s/([^a-zA-Z])+/ /gs;
78     if ($a =~ m/$b/i) {
79     $new_title = $collection;
80     } else {
81     $new_title = "$collection :: $new_title";
82     }
83     } else {
84     # fall-back to collection title
85     $new_title = $collection;
86     }
87    
88     $new_title =~ s/\s\s+/ /g;
89    
90 dpavlin 57 if ($contents =~ s,<title>(.*)</title>,<title>$new_title</title>,is) {
91     print STDERR "replace title '$1' with '$new_title'\n" if ($verbose);
92 dpavlin 69 } elsif ($contents =~ s,(<head>),$1<title>$new_title</title>,is) {
93     print STDERR "adding title '$new_title' after <head>\n" if ($verbose);
94    
95     } elsif ($contents =~ s,(<html>),$1<head><title>$new_title</title></head>,is) {
96     print STDERR "adding title '$new_title' after <html>\n" if ($verbose);
97    
98     } elsif ($contents =~ s,^,<title>$new_title</title>,) {
99 dpavlin 57 print STDERR "adding new title '$new_title'\n" if ($verbose);
100 dpavlin 69 } else {
101     print STDERR "WARNING: filter couldn't add new title '$new_title' anywhere!";
102 dpavlin 56 }
103    
104 dpavlin 46 return $contents;
105     }
106    
107     1;

Properties

Name Value
cvs2svn:cvs-rev 1.5

  ViewVC Help
Powered by ViewVC 1.1.26