summaryrefslogtreecommitdiffstats
path: root/staticweb/man.pl
blob: 0baa69be4d9e008275bff0ff459215a38b8f2517 (plain) (blame)
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
118
119
120
121
122
123
#!/usr/bin/perl
# $Id: man.pl,v 1.5 2003/07/05 05:19:18 in2 Exp $
use CGI qw/:standard/;
use lib qw/./;
use LocalVars;
use DB_File;
use strict;
use Data::Dumper;
use Date::Calc qw(:all);
use Template;
use HTML::Calendar::Simple;
use OurNet::FuzzyIndex;
use Data::Serializer;
use Encode;
use vars qw/%db $brdname $fpath $isgb/;

sub main
{
    my($tmpl, $rh, $key);

    if( !(($brdname, $fpath) = $ENV{PATH_INFO} =~ m|^/([\w\-]+?)(/.*)|) ||
    !(tie %db, 'DB_File',
      "$MANDATA/$brdname.db", O_RDONLY, 0666, $DB_HASH) ){
    return redirect("/man.pl/$1/")
        if( $ENV{PATH_INFO} =~ m|^/([\w\-]+?)$| );
    print header(-status => 404);
    return;
    }

    $isgb = (param('gb') ? 1 : 0);
    charset('');
    print header();

    if( ($key = param('key')) ){
    $rh = search($key);
    }
    else{
    $rh = (($fpath =~ m|/$|) ? dirmode($fpath) : articlemode($fpath));
    }
    $rh->{brdname} = $brdname;
    $tmpl = Template->new({INCLUDE_PATH => '.',
               ABSOLUTE => 0,
               RELATIVE => 0,
               RECURSION => 0,
               EVAL_PERL => 0,
#              COMPILE_EXT => '.tmpl',
#              COMPILE_DIR => $MANCACHE,
               });
    if( $rh->{gb} = $isgb ){
    $rh->{encoding} = 'gb2312';
    $rh->{lang} = 'zh_CN';
    $rh->{charset} = 'gb2312';
    }
    else{
    $rh->{encoding} = 'Big5';
    $rh->{lang} = 'zh_TW';
    $rh->{charset} = 'big5';
    }
    $tmpl->process($rh->{tmpl}, $rh);
}

sub dirmode
{
    my(%th, $isdir);
    my $serial = Data::Serializer->new(serializer => 'Storable',
                       digester   => 'MD5',
                       compress   => 0,
                       );
    foreach( @{$serial->deserialize($db{$fpath})} ){
    Encode::from_to($_->[1], 'big5', 'gb2312') if( $isgb );
    $isdir = (($_->[0] =~ m|/$|) ? 1 : 0);
    push @{$th{dat}}, {isdir => $isdir,
               fn    => "man.pl/$brdname$_->[0]",
               title => $_->[1]};
    }

    $th{tmpl} = 'dir.html';
    $th{isroot} = ($fpath eq '/') ? 1 : 0;
    return \%th;
}

sub articlemode
{
    my(%th);
    $th{tmpl} = 'article.html';
    $th{content} = $db{$fpath};
    $th{content} =~ s/\033\[.*?m//g;

    $th{content} =~ s|(http://[\w\-\.\:\/\,@\?=]+)|<a href="$1">$1</a>|gs;
    $th{content} =~ s|(ftp://[\w\-\.\:\/\,@]+)|<a href="$1">$1</a>|gs;
    $th{content} =~
    s|批踢踢兔|<a href="http://blog.ptt2.cc">批踢踢兔</a>|gs;
    $th{content} =~
    s|發信站: 批踢踢實業坊|發信站: <a href="http://blog.ptt.cc">批踢踢實業坊</a>|gs;
    $th{content} =~
    s|ptt\.csie\.ntu\.edu\.tw|<a href="telnet://ptt.csie.ntu.edu.tw">ptt.csie.ntu.edu.tw</a>|gs;
    $th{content} =~
    s|ptt\.twbbs\.org|<a href="telnet://ptt.csie.ntu.edu.tw">ptt.twbbs.org</a>|gs;

    Encode::from_to($th{content}, 'big5', 'gb2312') if( $isgb );
    return \%th;
}

sub search($)
{
    my($key) = @_;
    my(%th, $idx, $k);
    $idx = OurNet::FuzzyIndex->new("$MANDATA/$brdname.idx");
    my %result = $idx->query($th{key} = $key, MATCH_FUZZY);
    foreach my $t (sort { $result{$b} <=> $result{$a} } keys(%result)) {
    $k = $idx->getkey($t);
    push @{$th{search}}, {title => $db{"title-$k"},
                  fn    => $k,
                  score => $result{$t} / 10};
    }

    $th{key} = $key;
    $th{tmpl} = 'search.html';
    return \%th;
}

main();
1;