Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / khelpcenter / searchhandlers / khc_htsearch.pl
blob451e858ddd89a89e82e569a98fa85508cff9873c
1 #!/usr/bin/perl
3 use strict;
5 use Encode;
6 use Getopt::Long;
8 use open IO => ':utf8';
9 use open ':std';
11 my $htsearchpath="/srv/www/cgi-bin/htsearch";
13 my $config;
14 my $format;
15 my $method;
16 my $words;
17 my $lang;
18 my $docbook;
19 my $indexdir;
20 my $maxnum;
22 GetOptions (
23 'config=s' => \$config,
24 'format=s' => \$format,
25 'method=s' => \$method,
26 'words=s' => \$words,
27 'lang=s' => \$lang,
28 'docbook' => \$docbook,
29 'indexdir=s' => \$indexdir,
30 'maxnum=s' => \$maxnum
33 if ( !$indexdir ) {
34 print STDERR "No index dir given.\n";
35 exit 1;
38 if ( !$lang ) { $lang = "en"; }
40 my $charset = langCharset( $lang );
42 $words = encode( $charset, $words );
44 if ( !open( HTSEARCH, "-|", "$htsearchpath", "-c", "$indexdir/$config.conf",
45 "format=$format&method=$method&words=$words" ) )
47 print "Can't execute htsearch at '$htsearchpath'.\n";
48 exit 1;
51 my ($body,$liststart,$ref,$link,$error,$errorOut);
53 while( <HTSEARCH> ) {
54 if ( !$body ) {
55 print;
56 if ( /^<body/ ) { $body = 1; }
58 if ( /^<h3>/ ) {
59 print;
60 print "<ul>\n";
61 $liststart = 1;
63 if ( /^<img src.*<a href="(.*)">(.*)<\/a>/ ) {
64 $ref = $1;
65 $link = $2;
67 print STDERR "REF: $ref LINK: $link\n";
69 $ref =~ s/file:\/\/localhost//;
71 $ref =~ s/http:\/\/localhost\//file:\//;
73 if ( $docbook ) {
74 $ref =~ /help:\/\/(.*)\/index.docbook/;
75 my $app = $1;
76 $ref = "help:$app";
78 $link =~ s/apptitle/$app/;
81 print " <li><a href=\"$ref\">$link</a></li>\n";
83 if ( /^<h1>ht:\/\/Dig error/ ) {
84 $error = 1;
85 print "Htdig error:\n";
87 if ( $error && /^<pre>/ ) {
88 $errorOut = 1;
90 if ( $errorOut ) {
91 print;
92 if ( /^<\/pre>/ ) { $errorOut = 0; }
96 close HTSEARCH;
98 if ( $liststart ) { print "</ul>\n"; }
100 print "</body></html>\n";
102 if ( $? != 0 ) { exit $?; }
106 # Return charset used for given language
107 sub langCharset( $ )
109 my $lang = shift;
110 if ( $lang eq "cz" || $lang eq "hu" ) {
111 return "latin2";
112 } elsif ( $lang eq "kr" ) {
113 return "utf8";
114 } else {
115 return "latin1";