Added some code to peer into a data structure in Maemian/Schedule.pm. Also added the
[maemian] / nokia-lintian / lib / Text_utils.pm
1 # Hey emacs! This is a -*- Perl -*- script!
2 # Text_utils -- Perl utility functions for lintian
3
4 # Copyright (C) 1998 Christian Schwarz and Richard Braakman
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, you can find it on the World Wide
18 # Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
19 # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21
22 use strict;
23
24 # requires wrap() function
25 use Text::Wrap;
26
27 # html_wrap -- word-wrap a paragaph.  The wrap() function from Text::Wrap
28 # is not suitable, because it chops words that are longer than the line
29 # length.
30 sub html_wrap {
31     my ($lead, @text) = @_;
32     my @words = split(' ', join(' ', @text));
33     # subtract 1 to compensate for the lack of a space before the first word.
34     my $ll = length($lead) - 1;
35     my $cnt = 0;
36     my $r = "";
37
38     while ($cnt <= $#words) {
39         if ($ll + 1 + length($words[$cnt]) > 76) {
40             if ($cnt == 0) {
41                 # We're at the start of a line, and word still does not
42                 # fit.  Don't wrap it.
43                 $r .= $lead . shift(@words) . "\n";
44             } else {
45                 # start new line
46                 $r .= $lead . join(' ', splice(@words, 0, $cnt)) . "\n";
47                 $ll = length($lead) - 1;
48                 $cnt = 0;
49             }
50         } else {
51             $ll += 1 + length($words[$cnt]);
52             $cnt++;
53         }
54     }
55
56     if ($#words >= 0) {
57         # finish last line
58         $r .= $lead . join(' ', @words) . "\n";
59     }
60
61     return $r;
62 }
63
64 # split_paragraphs -- splits a bunch of text lines into paragraphs.
65 # This function returns a list of paragraphs.
66 # Paragraphs are separated by empty lines. Each empty line is a
67 # paragraph. Furthermore, indented lines are considered a paragraph.
68 sub split_paragraphs {
69     return "" unless (@_);
70
71     my $t = join("\n",@_);
72
73     my ($l,@o);
74     while ($t) {
75         $t =~ s/^\.\n/\n/o;
76         # starts with space or empty line?
77         if (($t =~ s/^([ \t][^\n]*)\n?//o) or ($t =~ s/^()\n//o)) {
78             #FLUSH;
79             if ($l) {
80                 $l =~ s/\s+/ /go;
81                 $l =~ s/^\s+//o;
82                 $l =~ s/\s+$//o;
83                 push(@o,$l);
84                 undef $l;
85             }
86             #
87             push(@o,$1);
88         }
89         # normal line?
90         elsif ($t =~ s/^([^\n]*)\n?//o) {
91             $l .= "$1 ";
92         }
93         # what else can happen?
94         else {
95             fail("internal error in wrap");
96         }
97     }
98     #FLUSH;
99     if ($l) {
100         $l =~ s/\s+/ /go;
101         $l =~ s/^\s+//o;
102         $l =~ s/\s+$//o;
103         push(@o,$l);
104         undef $l;
105     }
106     #
107
108     return @o;
109 }
110
111 sub dtml_to_html {
112     my @o;
113
114     my $pre=0;
115     for $_ (@_) {
116         s,\&maint\;,<a href=\"mailto:lintian-maint\@debian.org\">Lintian maintainer</a>,o; # "
117         s,\&debdev\;,<a href=\"mailto:debian-devel\@lists.debian.org\">debian-devel</a>,o; # "
118
119         # empty line?
120         if (/^\s*$/o) {
121             if ($pre) {
122                 push(@o,"\n");
123             }
124         }
125         # preformatted line?
126         elsif (/^\s/o) {
127             if (not $pre) {
128                 push(@o,"<pre>");
129                 $pre=1;
130             }
131             push(@o,"$_");
132         }
133         # normal line
134         else {
135             if ($pre) {
136                 push(@o,"</pre>");
137                 $pre=0;
138             }
139             push(@o,"<p>$_\n");
140         }
141     }
142     if ($pre) {
143         push(@o,"</pre>");
144         $pre=0;
145     }
146
147     return @o;
148 }
149
150 sub dtml_to_text {
151     for $_ (@_) {
152         # substitute Lintian &tags;
153         s,&maint;,lintian-maint\@debian.org,go;
154         s,&debdev;,debian-devel\@lists.debian.org,go;
155
156         # substitute HTML <tags>
157         s,<i>,&lt;,go;
158         s,</i>,&gt;,go;
159         s,<[^>]+>,,go;
160
161         # substitute HTML &tags;
162         s,&lt;,<,go;
163         s,&gt;,>,go;
164         s,&amp;,\&,go;
165
166         # preformatted?
167         if (not /^\s/o) {
168             # no.
169
170             s,\s\s+, ,go;
171             s,^ ,,o;
172             s, $,,o;
173         }
174     }
175
176     return @_;
177 }
178
179 # wrap_paragraphs -- wrap paragraphs in dpkg/dselect style.
180 # indented lines are not wrapped but displayed "as is"
181 sub wrap_paragraphs {
182     my $lead = shift;
183     my $html = 0;
184
185     if ($lead eq 'HTML') {
186         $html = 1;
187         $lead = shift;
188     }
189
190     my $o;
191     for my $t (split_paragraphs(@_)) {
192         # empty or indented line?
193         if ($t =~ /^$/ or $t =~ /^\s/) {
194             $o .= "$lead$t\n";
195         } else {
196             if ($html) {
197                 $o .= html_wrap($lead, "$t\n");
198             } else {
199                 $o .= wrap($lead, $lead, "$t\n");
200             }
201         }
202     }
203     return $o;
204 }
205
206 1;