1 # Hey emacs! This is a -*- Perl -*- script!
2 # Text_utils -- Perl utility functions for lintian
4 # Copyright (C) 1998 Christian Schwarz and Richard Braakman
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.
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.
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,
25 our @ISA = qw(Exporter);
26 our @EXPORT = qw(split_paragraphs wrap_paragraphs dtml_to_html dtml_to_text);
30 # requires wrap() function
32 # requires fail() function
35 # html_wrap -- word-wrap a paragaph. The wrap() function from Text::Wrap
36 # is not suitable, because it chops words that are longer than the line
39 my ($lead, @text) = @_;
40 my @words = split(' ', join(' ', @text));
41 # subtract 1 to compensate for the lack of a space before the first word.
42 my $ll = length($lead) - 1;
46 while ($cnt <= $#words) {
47 if ($ll + 1 + length($words[$cnt]) > 76) {
49 # We're at the start of a line, and word still does not
51 $r .= $lead . shift(@words) . "\n";
54 $r .= $lead . join(' ', splice(@words, 0, $cnt)) . "\n";
55 $ll = length($lead) - 1;
59 $ll += 1 + length($words[$cnt]);
66 $r .= $lead . join(' ', @words) . "\n";
72 # split_paragraphs -- splits a bunch of text lines into paragraphs.
73 # This function returns a list of paragraphs.
74 # Paragraphs are separated by empty lines. Each empty line is a
75 # paragraph. Furthermore, indented lines are considered a paragraph.
76 sub split_paragraphs {
77 return "" unless (@_);
79 my $t = join("\n",@_);
84 # starts with space or empty line?
85 if (($t =~ s/^([ \t][^\n]*)\n?//o) or ($t =~ s/^()\n//o)) {
98 elsif ($t =~ s/^([^\n]*)\n?//o) {
101 # what else can happen?
103 fail("internal error in wrap");
124 s,\&maint\;,<a href=\"mailto:lintian-maint\@debian.org\">Maemian maintainer</a>,o; # "
125 s,\&debdev\;,<a href=\"mailto:debian-devel\@lists.debian.org\">debian-devel</a>,o; # "
160 # substitute Maemian &tags;
161 s,&maint;,lintian-maint\@debian.org,go;
162 s,&debdev;,debian-devel\@lists.debian.org,go;
164 # substitute HTML <tags>
169 # substitute HTML &tags;
187 # wrap_paragraphs -- wrap paragraphs in dpkg/dselect style.
188 # indented lines are not wrapped but displayed "as is"
189 sub wrap_paragraphs {
193 if ($lead eq 'HTML') {
199 for my $t (split_paragraphs(@_)) {
200 # empty or indented line?
201 if ($t =~ /^$/ or $t =~ /^\s/) {
205 $o .= html_wrap($lead, "$t\n");
207 $o .= wrap($lead, $lead, "$t\n");