1 |
sub filter { |
sub filter { |
2 |
my $contents = shift || return; |
my $contents = shift || return; |
3 |
|
|
4 |
|
my $verbose = 0; |
5 |
|
|
6 |
# if you don't want content to be indexed, include it in |
# if you don't want content to be indexed, include it in |
7 |
# <noindex> foobar </noindex> tags or surround it with comments |
# <noindex> foobar </noindex> tags or surround it with comments |
8 |
# <!-- noindex --> foobar <!-- /noindex --> |
# <!-- noindex --> foobar <!-- /noindex --> |
22 |
# remove comments between <html> and <head> texi2html inserts them |
# remove comments between <html> and <head> texi2html inserts them |
23 |
# there and swish can't find document title then (libxml or swish bug?) |
# there and swish can't find document title then (libxml or swish bug?) |
24 |
while ($contents =~ s/(<html>.*)<!--.*?-->(.*<head>)/$1$2/msi) { }; |
while ($contents =~ s/(<html>.*)<!--.*?-->(.*<head>)/$1$2/msi) { }; |
25 |
|
|
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 |
|
|
32 |
|
# 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 |
# remote TPJ left column |
# remote TPJ left column |
37 |
if ($contents =~ s,<!-- BEGIN LEFT SIDE BAR CELL -->.+?<!-- END LEFT SIDE BAR CELL -->,,isg) { |
if ($contents =~ s,<!-- BEGIN LEFT SIDE BAR CELL -->.+?<!-- END LEFT SIDE BAR CELL -->,,isg) { |
58 |
|
|
59 |
if ($contents =~ m,<!--SafTocEntry="([^"]+)"-->,is) { |
if ($contents =~ m,<!--SafTocEntry="([^"]+)"-->,is) { |
60 |
$new_title = $1; |
$new_title = $1; |
61 |
} elsif ($contents =~ m,<h\d\sclass="docPartTitle"[^>]*>([^<]+)</h\d>,is) { |
print STDERR "using title '$new_title' from <!--SafTocEntry-->\n" if ($verbose); |
62 |
$new_title = $1; |
} elsif ($contents =~ m,<(h\d)\s+class="docPartTitle"[^>]*>(.+?)</\1>,is) { |
63 |
} elsif ($contents =~ m,<h\d\sclass="docChapterTitle"[^>]*>([^<]+)</h\d>,is) { |
$new_title = $2; |
64 |
$new_title = $1; |
print STDERR "using title '$new_title' from docPartTitle\n" if ($verbose); |
65 |
} elsif ($contents =~ m,<h\d\sclass="docSection1Title"[^>]*>([^<]+)</h\d>,is) { |
} elsif ($contents =~ m,<(h\d)\s+class="docChapterTitle"[^>]*>(.+?)</\1>,is) { |
66 |
$new_title = $1; |
$new_title = $2; |
67 |
|
print STDERR "using title '$new_title' from docChapterTitle\n" if ($verbose); |
68 |
|
} elsif ($contents =~ m,<(h\d)\s+class="docSection1Title"[^>]*>(.+?)</\1>,is) { |
69 |
|
$new_title = $2; |
70 |
|
print STDERR "using title '$new_title' from docSection1Title\n" if ($verbose); |
71 |
|
} elsif ($contents =~ m,<(h\d)\s+class="doc[^"]*Title"[^>]*>(.+?)</\1>,is) { |
72 |
|
$new_title = $2; |
73 |
|
print STDERR "using title '$new_title' from doc.+Title\n" if ($verbose); |
74 |
|
} elsif ($contents =~ m,<(h\d)\s+class="chapter"[^>]*>(.+?)</\1>,is) { |
75 |
|
$new_title = $2; |
76 |
|
print STDERR "using title '$new_title' from chapter\n" if ($verbose); |
77 |
|
} elsif ($contents =~ m,<(h\d)\s+class="sect1"[^>]*>(.+?)</\1>,is) { |
78 |
|
$new_title = $2; |
79 |
|
print STDERR "using title '$new_title' from sect1\n" if ($verbose); |
80 |
} else { |
} else { |
81 |
if ($contents =~ m,<title>([^<]+)</title>,is) { |
if ($contents =~ m,<title>([^<]+)</title>,is) { |
82 |
$new_title = $1; |
$new_title = $1; |
83 |
|
print STDERR "using title '$new_title' from <title>\n" if ($verbose); |
84 |
} elsif ($contents =~ m,<h\d[^>]*>([^<]+)</h\d>,is) { |
} elsif ($contents =~ m,<h\d[^>]*>([^<]+)</h\d>,is) { |
85 |
$new_title = $1; |
$new_title = $1; |
86 |
|
print STDERR "using title '$new_title' from <h_>\n" if ($verbose); |
87 |
} |
} |
88 |
} |
} |
89 |
|
|
90 |
if ($new_title) { |
if ($new_title) { |
91 |
|
# nuke html in title |
92 |
|
$new_title =~ s/<br>\s+/: /gs; |
93 |
|
$new_title =~ s/<\/*[^>]+>//gs; |
94 |
|
|
95 |
# check if new title is same as collection name |
# check if new title is same as collection name |
96 |
my ($a,$b) = ($new_title,$collection); |
my ($a,$b) = ($new_title,$collection); |
97 |
$a =~ s/([^a-zA-Z])+/ /gs; |
$a =~ s/([^a-zA-Z])+/ /gs; |
98 |
$b =~ s/([^a-zA-Z])+/ /gs; |
$b =~ s/([^a-zA-Z])+/ /gs; |
99 |
if ($a =~ m/$b/i) { |
if ($a =~ m/$b/i) { |
100 |
$new_title = $collection; |
$new_title = $collection; |
101 |
|
print STDERR "new_title and collection are same! [$new_title]\n" if ($verbose); |
102 |
} else { |
} else { |
103 |
$new_title = "$collection :: $new_title"; |
$new_title = "$collection :: $new_title"; |
104 |
} |
} |
109 |
|
|
110 |
$new_title =~ s/\s\s+/ /g; |
$new_title =~ s/\s\s+/ /g; |
111 |
|
|
112 |
if ($contents =~ s,<title>(.*)</title>,$new_title,is) { |
if ($contents =~ s,<title>(.*)</title>,<title>$new_title</title>,is) { |
113 |
print STDERR "replace title '$1' with '$new_title'\n"; |
print STDERR "replace title '$1' with '$new_title'\n" if ($verbose); |
114 |
|
} elsif ($contents =~ s,(<head>),$1<title>$new_title</title>,is) { |
115 |
|
print STDERR "adding title '$new_title' after <head>\n" if ($verbose); |
116 |
|
|
117 |
|
} elsif ($contents =~ s,(<html>),$1<head><title>$new_title</title></head>,is) { |
118 |
|
print STDERR "adding title '$new_title' after <html>\n" if ($verbose); |
119 |
|
|
120 |
|
} elsif ($contents =~ s,^,<title>$new_title</title>,) { |
121 |
|
print STDERR "adding new title '$new_title'\n" if ($verbose); |
122 |
} else { |
} else { |
123 |
print STDERR "adding new title '$new_title'\n"; |
print STDERR "WARNING: filter couldn't add new title '$new_title' anywhere!"; |
|
# try to insert after <head>, <html> or at top |
|
|
$contents =~ s,(<head>),$1<title>$new_title</title>,is || |
|
|
$contents =~ s,(<html>),$1<title>$new_title</title>,is || |
|
|
$contents =~ s,^,<title>$new_title</title>,; |
|
124 |
} |
} |
125 |
|
|
126 |
return $contents; |
return $contents; |