]> git.deb.at Git - deb/packages.git/blob - bin/parse-debtags-voc
Replace non working volatile mirror debian.domainmail.org with mirror.csclub.uwaterloo.ca
[deb/packages.git] / bin / parse-debtags-voc
1 #!/usr/bin/perl -w
2 # Convert Debtags vocabulary.gz files into Sleepycat db files
3 #
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.
9
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.
14
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.
18
19 use strict;
20 use warnings;
21 use lib './lib';
22
23 $| = 1;
24
25 use POSIX;
26 use DB_File;
27 use File::Path;
28 use Data::Dumper;
29 use HTML::Entities;
30 use URI::Escape;
31
32 use Deb::Versions;
33 use Packages::Template;
34 use Packages::Config qw( $TOPDIR @LANGUAGES );
35 use Packages::I18N::Locale;
36 &Packages::Config::init( './' );
37 my $debtagsdir = "$TOPDIR/files/debtags";
38 my $wwwdir = "$TOPDIR/www/about";
39 my $voc_file = "$debtagsdir/vocabulary";
40 my (%voc, %voc_db);
41
42 $/ = "";
43
44 sub process_desc {
45     my ($desc) = @_;
46
47     if ($desc) {
48         $desc = encode_entities($desc);
49
50         $desc =~ s,((ftp|http|https)://[\S~-]+?/?)((\&gt\;)?[)]?[']?[:.\,]?(\s|$)),<a href=\"$1\">$1</a>$3,go; # syntax highlighting -> '];
51         $desc =~ s/\A //o;
52         $desc =~ s/\n /\n/sgo;
53         $desc =~ s/\n.\n/\n<p>\n/go;
54         $desc =~ s/(((\n|\A) [^\n]*)+)/\n<pre>$1\n<\/pre>/sgo;
55     }
56     return $desc;
57 }
58
59 print "Parsing Vocabulary...\n";
60 tie %voc_db, "DB_File", "$debtagsdir/vocabulary.db.new",
61     O_RDWR|O_CREAT, 0666, $DB_BTREE
62     or die "Error creating DB: $!";
63 open VOC, '<', $voc_file or die "Error opening vocabulary: $!";
64
65 while (<VOC>) {
66     next if /^\s*$/;
67     my $data = "";
68     my %data = ();
69     chomp;
70     s/\n /\377/g;
71     while (/^(\S+):\s*(.*)\s*$/mg) {
72         my ($key, $value) = ($1, $2);
73         $value =~ s/\377/\n /g;
74         $key =~ tr [A-Z] [a-z];
75         $data{$key} = $value;
76     }
77     my $voc_key = $data{facet} || $data{tag};
78     unless ($voc_key) {
79         warn "No key found in ".Dumper(\%data);
80         next;
81     }
82     if ($voc{$voc_key}) {
83         warn "Duplicated key found: $voc_key\n";
84         next;
85     }
86     my ($sdesc,$ldesc) = split /\n/, $data{description}, 2;
87
88     $data{html_description} = [ encode_entities($sdesc), process_desc($ldesc)||"" ];
89     $voc_db{$voc_key} = $sdesc || "";
90
91     foreach my $lang (@LANGUAGES) {
92         next if $lang eq 'en';
93
94         my $cat = Packages::I18N::Locale->get_handle($lang)
95             or die "get_handle failed for $lang";
96
97         my $sdesc_trans = $cat->maketext($sdesc);
98         $voc_db{"$voc_key-$lang"} = $sdesc_trans
99             if $sdesc_trans and $sdesc_trans ne $sdesc;
100     }
101
102     $voc{$voc_key} = \%data;
103 }
104
105 close VOC or warn "Couldn't close vocabulary: $!";
106
107 #print Dumper(\%voc,\%voc_db);
108
109 print "Creating tag list...\n";
110
111 -d $wwwdir || mkpath( $wwwdir );
112 open TAGLST, '>', "$wwwdir/debtags.en.html.new"
113     or die "Error creating tag list: $!";
114
115 my $template = new Packages::Template( "$TOPDIR/templates", 'html', {} );
116 my @facets = sort( grep { exists $voc{$_}{facet} } keys %voc );
117 my @tags = sort( grep { exists $voc{$_}{tag} } keys %voc );
118 my %tags_by_facet;
119 foreach (@tags) {
120     my ($facet, $tag) = split m/::/, $_, 2;
121     warn "No facet data available for $facet\n"
122         unless exists $voc{$facet};
123     $tags_by_facet{$facet} ||= [];
124     push @{$tags_by_facet{$facet}}, $_;
125 }
126 my %content = ( vocabulary => \%voc,
127                 facets => \@facets, tags => \@tags,
128                 tags_by_facet => \%tags_by_facet,
129                 used_langs => [ 'en' ]);
130 print TAGLST $template->page( 'tag_index', \%content );
131 close TAGLST or warn "Couldn't close tag list: $!";
132
133 rename( "$wwwdir/debtags.en.html.new",
134         "$wwwdir/debtags.en.html" );
135
136 untie %voc_db;
137 rename( "$debtagsdir/vocabulary.db.new",
138         "$debtagsdir/vocabulary.db" );