/[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 100 - (hide annotations)
Sat Apr 30 20:21:02 2005 UTC (19 years, 1 month ago) by dpavlin
File size: 5718 byte(s)
extract title from beginning of document if no other data is found

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 dpavlin 71
26     # remove empty lines before/after <html>
27     $contents =~ s/^\s+(<html>)/$1/is;
28     $contents =~ s/(<\/html>)\s+$/$1/is;
29     # remove cr
30     $contents =~ s/\r//gs;
31 dpavlin 46
32 dpavlin 71 # remove SQL Magazine header and footer
33     $contents =~ s/<!-- begin topnav area -->.+?<!-- end topnav area -->/<\/table>/is;
34     $contents =~ s/<!--Begin Footer-->.+?<\/table>/<\/table>/is;
35    
36 dpavlin 46 # remote TPJ left column
37     if ($contents =~ s,<!-- BEGIN LEFT SIDE BAR CELL -->.+?<!-- END LEFT SIDE BAR CELL -->,,isg) {
38     my $title;
39     # extract title and add to title
40     if ($contents =~ m,<!-- the article goes here -->\s*<h2[^>]*>(.+?)</h2>,si) {
41     $title = $1;
42     } elsif ($contents =~ m,<h1[^>]*>(.+?)</h1>,is) {
43     $title = $1;
44     } elsif ($contents =~ m,<h2[^>]*>(.+?)</h2>,is) {
45     $title = $1;
46     } else {
47     $title = "no detail title";
48     }
49     $contents =~ s,(<title>)([^<]+)(</title>),$1$2: $title$3,gsi if ($title);
50    
51 dpavlin 85 } elsif ($contents =~ m,<!--\s+METADATA(.*?)METADATA\s+-->,is) {
52     # LJ metadata
53     my $metadata = $1;
54     my $title = "LJ";
55    
56     $title .= "$1: " if ($metadata =~ m,<issue>(.+?)</issue>,is);
57     $title .= "$1 - " if ($metadata =~ m,<category>(.+?)</category>,is);
58     $title .= $1 if ($metadata =~ m,<title>(.+?)</title>,is);
59     $title =~ s/\s+/ /gsi;
60     $contents =~ s,<title>[^<]+</title>,<title>$title</title>,is;
61 dpavlin 46 }
62 dpavlin 51
63 dpavlin 56 # is second argument collection?
64     my $collection = shift || return $contents;
65    
66     # construct new title (from various parts of DocBook if available)
67     my $new_title;
68    
69 dpavlin 100 sub create_title($) {
70     my $contents = shift || return;
71     my $new_title = substr($contents, 0, 4096);
72     $new_title =~ s/<[^>]+>//gis;
73     $new_title =~ s/^\s+//s;
74     $new_title =~ s/^(.{50}.*?)[\n\r].+$/$1/s;
75     $new_title =~ s/\s\s+/ /gis;
76     print STDERR "using title '$new_title' from first lines in document\n" if ($verbose);
77     return $new_title;
78     }
79    
80 dpavlin 56 if ($contents =~ m,<!--SafTocEntry="([^"]+)"-->,is) {
81     $new_title = $1;
82 dpavlin 74 print STDERR "using title '$new_title' from <!--SafTocEntry-->\n" if ($verbose);
83     } elsif ($contents =~ m,<(h\d)\s+class="docPartTitle"[^>]*>(.+?)</\1>,is) {
84 dpavlin 65 $new_title = $2;
85 dpavlin 74 print STDERR "using title '$new_title' from docPartTitle\n" if ($verbose);
86     } elsif ($contents =~ m,<(h\d)\s+class="docChapterTitle"[^>]*>(.+?)</\1>,is) {
87 dpavlin 65 $new_title = $2;
88 dpavlin 74 print STDERR "using title '$new_title' from docChapterTitle\n" if ($verbose);
89     } elsif ($contents =~ m,<(h\d)\s+class="docSection1Title"[^>]*>(.+?)</\1>,is) {
90 dpavlin 65 $new_title = $2;
91 dpavlin 74 print STDERR "using title '$new_title' from docSection1Title\n" if ($verbose);
92     } elsif ($contents =~ m,<(h\d)\s+class="doc[^"]*Title"[^>]*>(.+?)</\1>,is) {
93 dpavlin 65 $new_title = $2;
94 dpavlin 74 print STDERR "using title '$new_title' from doc.+Title\n" if ($verbose);
95     } elsif ($contents =~ m,<(h\d)\s+class="chapter"[^>]*>(.+?)</\1>,is) {
96 dpavlin 65 $new_title = $2;
97 dpavlin 74 print STDERR "using title '$new_title' from chapter\n" if ($verbose);
98     } elsif ($contents =~ m,<(h\d)\s+class="sect1"[^>]*>(.+?)</\1>,is) {
99     $new_title = $2;
100     print STDERR "using title '$new_title' from sect1\n" if ($verbose);
101 dpavlin 56 } else {
102     if ($contents =~ m,<title>([^<]+)</title>,is) {
103     $new_title = $1;
104 dpavlin 74 print STDERR "using title '$new_title' from <title>\n" if ($verbose);
105 dpavlin 56 } elsif ($contents =~ m,<h\d[^>]*>([^<]+)</h\d>,is) {
106     $new_title = $1;
107 dpavlin 74 print STDERR "using title '$new_title' from <h_>\n" if ($verbose);
108 dpavlin 100 } else {
109     $new_title = create_title($contents);
110 dpavlin 56 }
111     }
112    
113     if ($new_title) {
114 dpavlin 61 # nuke html in title
115     $new_title =~ s/<br>\s+/: /gs;
116     $new_title =~ s/<\/*[^>]+>//gs;
117    
118 dpavlin 56 # check if new title is same as collection name
119     my ($a,$b) = ($new_title,$collection);
120     $a =~ s/([^a-zA-Z])+/ /gs;
121     $b =~ s/([^a-zA-Z])+/ /gs;
122     if ($a =~ m/$b/i) {
123     $new_title = $collection;
124 dpavlin 74 print STDERR "new_title and collection are same! [$new_title]\n" if ($verbose);
125 dpavlin 56 } else {
126     $new_title = "$collection :: $new_title";
127     }
128     } else {
129     # fall-back to collection title
130     $new_title = $collection;
131 dpavlin 100 my $tmp = create_title($ontents);
132     $new_title .= " :: $tmp" if ($tmp);
133 dpavlin 56 }
134    
135     $new_title =~ s/\s\s+/ /g;
136    
137 dpavlin 57 if ($contents =~ s,<title>(.*)</title>,<title>$new_title</title>,is) {
138     print STDERR "replace title '$1' with '$new_title'\n" if ($verbose);
139 dpavlin 69 } elsif ($contents =~ s,(<head>),$1<title>$new_title</title>,is) {
140     print STDERR "adding title '$new_title' after <head>\n" if ($verbose);
141    
142     } elsif ($contents =~ s,(<html>),$1<head><title>$new_title</title></head>,is) {
143     print STDERR "adding title '$new_title' after <html>\n" if ($verbose);
144    
145     } elsif ($contents =~ s,^,<title>$new_title</title>,) {
146 dpavlin 57 print STDERR "adding new title '$new_title'\n" if ($verbose);
147 dpavlin 69 } else {
148     print STDERR "WARNING: filter couldn't add new title '$new_title' anywhere!";
149 dpavlin 56 }
150    
151 dpavlin 46 return $contents;
152     }
153    
154     1;

Properties

Name Value
cvs2svn:cvs-rev 1.5

  ViewVC Help
Powered by ViewVC 1.1.26