]> git.deb.at Git - deb/packages.git/blob - bin/debtags-xgettext
debtags-xgettext: Handle linebreaks (and other whitespace) a little more sanely
[deb/packages.git] / bin / debtags-xgettext
1 #! /usr/bin/perl -w
2
3 # copied from webwml/english/po/wmlxgettext.pl and
4 # changed to a crude intltool replacement
5 #  Copyright © 2002-2003  Denis Barbier <barbier@debian.org>
6 #  Copyright © 2007 Frank Lichtenheld <frank@lichtenheld.de>
7 #
8 #  This program is free software; you can redistribute it and/or modify
9 #  it under the terms of the GNU General Public License as published by
10 #  the Free Software Foundation; either version 2 of the License, or
11 #  (at your option) any later version.
12 use strict;
13
14 my $messages = {};
15 my @msgids = ();
16
17 sub escape {
18         my $text = shift;
19         $text =~ s/\\/\\\\/g;
20         $text =~ s/"/\\"/g;
21         $text =~ s/\n\s*\.\n\s?/\\n/g;
22         $text =~ s/\n//g;
23         $text =~ s/\t/\\t/g;
24         return $text;
25 }
26
27 sub processFile {
28         my $file = shift;
29         my ($text, $comment);
30         my (@messages) = ();
31         my (%msgids) = ();
32
33         open(IN, "<", $file) || die "Unable to open $file\n";
34         local ($/) = "";
35         while (<IN>) {
36             my %data = ();
37             chomp;
38             s/\n /\377/g;
39             while (/^(\S+):\s*(.*)\s*$/mg) {
40                 my ($key, $value) = ($1, $2);
41                 $value =~ s/\377/\n /g;
42                 $key =~ tr [A-Z] [a-z];
43                 $data{$key} = $value;
44             }
45             next unless $data{description};
46             my $comment = '';
47             if ($data{facet}) {
48                 $comment = "Facet: $data{facet}";
49             } elsif ($data{tag}) {
50                 $comment = "Tag: $data{tag}";
51             } else {
52                 die "Neither Facet nor Tag found.";
53             }
54
55             my ($short, $long) = split /\n/, $data{description}, 2;
56
57             $short = escape($short);
58             push (@msgids, $short);
59             push (@{$messages->{$short}}, $comment.", short desc", $file);
60
61             if ($long) {
62                 $long = escape($long);
63                 $long =~ s/^\s//;
64                 push (@msgids, $long);
65                 push (@{$messages->{$long}}, $comment.", long desc", $file);
66             }
67         }
68         close(IN);
69 }
70
71 foreach (@ARGV) {
72         processFile($_);
73 }
74
75 print "msgid \"\"\nmsgstr \"\"\n".
76         "\"Content-Type: text/plain; charset=UTF-8\\n\"\n".
77         "\"Content-Transfer-Encoding: 8bit\\n\"\n\n";
78
79 foreach my $msgid (@msgids) {
80         next unless $messages->{$msgid};
81         while (@{$messages->{$msgid}}) {
82                 $_ = shift(@{$messages->{$msgid}});
83                 s/\n/\n#. /g;
84                 print "#. ".$_."\n" if $_;
85                 print "#: ".shift(@{$messages->{$msgid}})."\n";
86         }
87         print "msgid \"$msgid\"\nmsgstr \"\"\n\n";
88         undef $messages->{$msgid};
89 }