summaryrefslogtreecommitdiffstats
path: root/khelpcenter/searchhandlers/khc_htsearch.pl.in
blob: 28113bec25ffacaa121df8a9505e15e9b81dc935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/perl

use strict;

use Encode;
use Getopt::Long;

use open IO => ':utf8';
use open ':std';

my $htsearchpath="@HTDIG_SEARCH_BINARY@";
my $config;
my $format;
my $method;
my $words;
my $lang;
my $docbook;
my $indexdir;
my $maxnum;

GetOptions (
  'binary=s' => \$htsearchpath,
  'config=s' => \$config,
  'format=s' => \$format,
  'method=s' => \$method,
  'words=s' => \$words,
  'lang=s' => \$lang,
  'docbook' => \$docbook,
  'indexdir=s' => \$indexdir,
  'maxnum=s' => \$maxnum
);

if ( !$indexdir ) {
  print STDERR "No index dir given.\n";
  exit 1;
}

if ( !$lang ) { $lang = "en"; }

my $charset = langCharset( $lang );

$words = encode( $charset, $words );

if ( !open( HTSEARCH, "-|", "$htsearchpath", "-c", "$indexdir/$config.conf",
            "format=$format&method=$method&words=$words&matchesperpage=$maxnum&exclude=[index.html]" ) )
{
  print "Can't execute htsearch at '$htsearchpath'.\n";
  exit 1;
}

my ($body,$liststart,$ref,$link,$error,$errorOut);

while( <HTSEARCH> ) {
  if ( !$body ) {
    print;
    if ( /^<body/ ) { $body = 1; }
  }
  if ( /^<h3>/ ) {
    print;
    print "<ul>\n";
    $liststart = 1;
  }
  if ( /^<img src.*<a href="(.*)">(.*)<\/a>/ ) {
    $ref = $1;
    $link = $2;

    print STDERR "REF: $ref  LINK: $link\n";

    $ref =~ s/file:\/\/localhost//;
    
    $ref =~ s/http:\/\/localhost\//file:\//;

    if ( $docbook ) {
      $ref =~ /help:\/\/(.*)\/index.docbook/;
      my $app = $1;
      $ref = "help:$app";

      $link =~ s/apptitle/$app/;
    }
    
    print "  <li><a href=\"$ref\">$link</a></li>\n";
  }
  if ( /^<h1>ht:\/\/Dig error/ ) {
    $error = 1;
    print "Htdig error:\n";
  }
  if ( $error && /^<pre>/ ) {
    $errorOut = 1;
  }
  if ( $errorOut ) {
    print;
    if ( /^<\/pre>/ ) { $errorOut = 0; }
  }
}

close HTSEARCH;

if ( $liststart ) { print "</ul>\n"; }

print "</body></html>\n";

if ( $? != 0 ) { exit $?; }

1;

# Return charset used for given language
sub langCharset( $ )
{
  my $lang = shift;
  if ( $lang eq "cz" || $lang eq "hu" ) {
    return "latin2";
  } elsif ( $lang eq "kr" ) {
    return "utf8";
  } else {
    return "latin1";
  }
}