2 # Convert Debtags vocabulary.gz files into Sleepycat db files
4 # Copyright (C) 2006 Frank Lichtenheld <djpig@debian.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 use Packages::Template;
33 use Packages::Config qw( $TOPDIR );
35 &Packages::Config::init( './' );
36 my $debtagsdir = "$TOPDIR/files/debtags";
37 my $wwwdir = "$TOPDIR/www/about";
38 my $voc_file = "$debtagsdir/vocabulary";
43 delete $ENV{'LANGUAGE'};
45 delete $ENV{'LC_ALL'};
46 delete $ENV{'LC_MESSAGES'};
48 print "Parsing Vocabulary...\n";
49 tie %voc_db, "DB_File", "$debtagsdir/vocabulary.db.new",
50 O_RDWR|O_CREAT, 0666, $DB_BTREE
51 or die "Error creating DB: $!";
52 open VOC, '<', $voc_file or die "Error opening vocabulary: $!";
60 while (/^(\S+):\s*(.*)\s*$/mg) {
61 my ($key, $value) = ($1, $2);
62 $value =~ s/\377/\n /g;
63 $key =~ tr [A-Z] [a-z];
66 my $voc_key = $data{facet} || $data{tag};
68 warn "No key found in ".Dumper(\%data);
72 warn "Duplicated key found: $voc_key\n";
75 my ($sdesc,$ldesc) = split /\n/, encode_entities($data{description}), 2;
78 $ldesc =~ s,((ftp|http|https)://[\S~-]+?/?)((\>\;)?[)]?[']?[:.\,]?(\s|$)),<a href=\"$1\">$1</a>$3,go; # syntax highlighting -> '];
80 $ldesc =~ s/\n /\n/sgo;
81 $ldesc =~ s/\n.\n/\n<p>\n/go;
82 $ldesc =~ s/(((\n|\A) [^\n]*)+)/\n<pre>$1\n<\/pre>/sgo;
84 $data{html_description} = [ $sdesc, $ldesc||"" ];
86 $voc_db{$voc_key} = $sdesc || "";
87 $voc{$voc_key} = \%data;
90 close VOC or warn "Couldn't close vocabulary: $!";
92 #print Dumper(\%voc,\%voc_db);
94 print "Creating tag list...\n";
96 -d $wwwdir || mkpath( $wwwdir );
97 open TAGLST, '>', "$wwwdir/debtags.en.html.new"
98 or die "Error creating tag list: $!";
100 my $template = new Packages::Template( "$TOPDIR/templates", 'html', {} );
101 my @facets = sort( grep { exists $voc{$_}{facet} } keys %voc );
102 my @tags = sort( grep { exists $voc{$_}{tag} } keys %voc );
105 my ($facet, $tag) = split /::/, $_, 2;
106 warn "No facet data available for $facet\n"
107 unless exists $voc{$facet};
108 $tags_by_facet{$facet} ||= [];
109 push @{$tags_by_facet{$facet}}, $_;
111 my %content = ( vocabulary => \%voc,
112 facets => \@facets, tags => \@tags,
113 tags_by_facet => \%tags_by_facet );
114 # needed to work around the limitations of the the FILTER syntax
115 $content{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') };
116 $content{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
117 $content{quotemeta} = sub { return quotemeta($_[0]) };
118 $content{string2id} = sub { return &Packages::CGI::string2id(@_) };
120 print TAGLST $template->page( 'tag_index', \%content );
121 close TAGLST or warn "Couldn't close tag list: $!";
123 rename( "$wwwdir/debtags.en.html.new",
124 "$wwwdir/debtags.en.html" );
127 rename( "$debtagsdir/vocabulary.db.new",
128 "$debtagsdir/vocabulary.db" );