]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Encode/Posting.pm
1 package Encode
::Posting
;
3 ################################################################################
5 # File: shared/Encode/Posting.pm #
7 # Authors: André Malo <nd@o3media.de>, 2001-04-10 #
9 # Description: prepare a Posting text for saving and visual (HTML) output #
11 ################################################################################
15 use Encode
::Plain
; $Encode::Plain
::utf8
= 1;
18 ################################################################################
22 use base
qw(Exporter);
23 @Encode::Posting
::EXPORT
= qw(
29 ### sub rel_uri ($$) ###########################################################
31 # generate an absolute URI from a absolute|relative one
32 # (not for public use)
37 # Return: abs URI as string
40 my ($uri, $base) = @_;
42 "http://$ENV{HTTP_HOST}".
48 ### sub encoded_body ($;$) #####################################################
50 # prepare posting text for saving
52 # Params: $posting - scalar reference of the raw text
53 # $params - hash reference
54 # (quoteChars messages)
56 # Return: scalar reference of the encoded text
58 sub encoded_body
($;$) {
59 my $posting = ${+shift};
62 $posting =~ s/\015\012|\015|\012/\n/g; # normalize newlines
63 $posting =~ s/[^\S\n]$//gm; # kill whitespaces at the end of all lines
64 $posting =~ s/\s+$//; # kill whitespaces (newlines) at the end of the string (text)
66 # check the special syntaxes:
68 my $base = $params -> {base_uri
};
69 # collect all [link:...] strings
72 push @rawlinks => [$1 => $2] while ($posting =~ /\[([Ll][Ii][Nn][Kk]):\s*([^\]\s]+)\s*\]/g);
74 is_URL
( $_ -> [1] => ':ALL')
75 or is_URL
(($_ -> [1] =~ /^[Vv][Ii][Ee][Ww]-[Ss][Oo][Uu][Rr][Cc][Ee]:(.+)/)[0] || '' => 'http')
76 or ( $_ -> [1] =~ m
<^\
.?\
.?
/(?!/)|\?>
77 and is_URL
(rel_uri
($_ -> [1], $base) => 'http'))
80 # collect all [image:...] strings
83 push @rawimages => [$1 => $2] while ($posting =~ /\[([Ii][Mm][Aa][Gg][Ee]):\s*([^\]\s]+)\s*\]/g);
85 is_URL
($_ -> [1] => 'strict_http')
86 or ( $_ -> [1] =~ m
<^\
.?\
.?
/(?!/)|\?>
87 and is_URL
(rel_uri
($_ -> [1], $base) => 'http'))
90 # collect all [iframe:...] strings
93 push @rawiframes => [$1 => $2] while ($posting =~ /\[([Ii][Ff][Rr][Aa][Mm][Ee]):\s*([^\]\s]+)\s*\]/g);
95 is_URL
($_ -> [1] => 'http')
96 or is_URL
(($_ -> [1] =~ /^[Vv][Ii][Ee][Ww]-[Ss][Oo][Uu][Rr][Cc][Ee]:(.+)/)[0] || '' => 'http')
97 or ( $_ -> [1] =~ m
<^\
.?\
.?
/(?!/)|\?>
98 and is_URL
(rel_uri
($_ -> [1], $base) => 'http'))
101 # collect all [msg:...] strings
103 $params -> {messages
} = {} unless (defined $params -> {messages
});
104 my %msg = map {lc($_) => $params -> {messages
} -> {$_}} keys %{$params -> {messages
}};
107 push @rawmsgs => [$1 => $2] while ($posting =~ /\[([Mm][Ss][Gg]):\s*([^\]\s]+)\s*\]/g);
108 my @msgs = grep {exists ($msg{lc($_ -> [1])})} @rawmsgs;
110 # encode Entities and special characters
112 $posting = ${plain
(\
$posting)};
114 # encode the special syntaxes
116 $posting =~ s!$_!<a href="$1">$1</a>!
117 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @links);
119 $posting =~ s!$_!<img src="$1" border=0 alt="">!
120 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @images);
122 $posting =~ s!$_!<iframe src="$1" width="90%" height="90%"><a href="$1">$1</a></iframe>!
123 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @iframes);
125 %msg = map {plain
($_) => $msg{$_}} keys %msg;
126 $posting =~ s!$_!'<img src="'.$msg{lc $1} -> {src}.'" width='.$msg{lc $1}->{width}.' height='.$msg{lc $1}->{height}.' border=0 alt="'.plain($msg{lc $1}->{alt}).'">'!e
127 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @msgs);
129 # normalize quote characters (quote => \177)
131 my $quote = plain
(defined $params -> {quoteChars
} ?
$params -> {quoteChars
} : '');
132 my $len = length ($quote);
133 $posting =~ s!^((?:\Q$quote\E)+)!"\177" x (length($1)/$len)!gem if ($len);
135 # \n => <br>, fix spaces
137 $posting = ${multiline
(\
$posting)};
144 ### sub answer_field ($$) ######################################################
146 # create the content of the answer textarea
148 # Params: $posting - scalar reference
149 # (posting text, 'encoded_body' encoded)
150 # $params - hash reference
151 # (quoteArea quoteChars messages)
153 # Return: scalar reference
155 sub answer_field
($$) {
157 my $params = shift || {};
159 my $area = $$posting;
160 my $qchar = $params -> {quoteChars
};
162 $area =~ s/<br>/\n/g; # <br> => \n
163 $area =~ s/&(?:#160|nbsp);/ /g; # nbsp => ' '
165 $area =~ s/^(.)/\177$1/gm if ($params -> {quoteArea
}); # shift a quoting character
166 $area =~ s/^(\177+)/$qchar x length ($1)/gem; # decode normalized quoting characters
168 # recode special syntaxes
169 # from HTML to [...] constructions
171 $params -> {messages
} = {} unless (defined $params -> {messages
}); # avoid error messages
173 $params -> {messages
} -> {$_} -> {src
} => $_
174 } keys %{$params -> {messages
}}; # we have to lookup reverse ...
177 $area =~ s
{(<img\s
+src
="([^"]+)"\s+width[^>]+>)} {
184 $area =~ s{<iframe\s+src="([^"]*)"[^>]+>.*?</iframe>} {[iframe: $1]}g;
187 $area =~ s{<img src="([^"]*)"[^>]*>}{[image: $1]}g;
190 $area =~ s{<a href="([^"]*)">.*?</a>}{[link: $1]}g;
197 ### sub message_field ($$) #####################################################
199 # prepare the posting text for visual output
201 # Params: $posting - scalar reference
202 # (raw posting text, 'encoded_body' encoded)
203 # $params - hash reference
204 # (quoteChars quoting startCite endCite)
206 # Return: scalar rerence (prepared posting text)
208 sub message_field ($$) {
209 my $posting = ${+shift};
210 my $params = shift || {};
213 if ($params -> {quoting}) { # quotes are displayed as special?
214 my @array = [0 => []];
216 for (split /<br>/ => $posting) {
217 my $l = length ((/^(\177*)/)[0]);
218 if ($array[-1][0] == $l) {
219 push @{$array[-1][-1]} => $_;
222 push @array => [$l => [$_]];
225 shift @array unless @{$array[0][-1]};
227 $posting = join '<br>' => map {
229 ? join join ('<br>' => @{$_->[-1]}) => ($params->{startCite}, $params->{endCite})
230 : (join '<br>' => @{$_->[-1]});
234 my $qchar = $params -> {quoteChars};
235 $posting =~ s/\177/$qchar/g; # \177 => quote chars
242 # keeping 'require' happy
247 ### end of Encode::Posting #####################################################
patrick-canterino.de