]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Encode/Posting.pm
395f71fdea37cbeae9e557460482238d53576a7e
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 ( $_ -> [1] =~ m
<^(?
:\
.?\
.?
/(?!/)|\?)>
97 and is_URL
(rel_uri
($_ -> [1], $base) => 'http'))
100 # collect all [msg:...] strings
102 $params -> {messages
} = {} unless (defined $params -> {messages
});
103 my %msg = map {lc($_) => $params -> {messages
} -> {$_}} keys %{$params -> {messages
}};
106 push @rawmsgs => [$1 => $2] while ($posting =~ /\[([Mm][Ss][Gg]):\s*([^\]\s]+)\s*\]/g);
107 my @msgs = grep {exists ($msg{lc($_ -> [1])})} @rawmsgs;
109 # encode Entities and special characters
111 $posting = ${plain
(\
$posting)};
113 # encode the special syntaxes
115 $posting =~ s!$_!<a href="$1">$1</a>!
116 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @links);
118 $posting =~ s!$_!<img src="$1" border=0 alt="">!
119 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @images);
121 $posting =~ s!$_!<iframe src="$1" width="90%" height="90%"><a href="$1">$1</a></iframe>!
122 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @iframes);
124 %msg = map {plain
($_) => $msg{$_}} keys %msg;
125 $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
126 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @msgs);
128 # normalize quote characters (quote => \177)
130 my $quote = plain
(defined $params -> {quoteChars
} ?
$params -> {quoteChars
} : '');
131 my $len = length ($quote);
132 $posting =~ s!^((?:\Q$quote\E)+)!"\177" x (length($1)/$len)!gem if ($len);
134 # \n => <br>, fix spaces
136 $posting = ${multiline
(\
$posting)};
143 ### sub answer_field ($$) ######################################################
145 # create the content of the answer textarea
147 # Params: $posting - scalar reference
148 # (posting text, 'encoded_body' encoded)
149 # $params - hash reference
150 # (quoteArea quoteChars messages)
152 # Return: scalar reference
154 sub answer_field
($$) {
156 my $params = shift || {};
158 my $area = $$posting;
159 my $qchar = $params -> {quoteChars
};
161 $area =~ s/<br(?:\/| \/)?
>/\n/g; # <br> => \n
162 $area =~ s/&(?:#160|nbsp);/ /g; # nbsp => ' '
164 $area =~ s/^(.)/\177$1/gm if ($params -> {quoteArea
}); # shift a quoting character
165 $area =~ s/^(\177+)/$qchar x length ($1)/gem; # decode normalized quoting characters
167 # recode special syntaxes
168 # from HTML to [...] constructions
170 $params -> {messages
} = {} unless (defined $params -> {messages
}); # avoid error messages
172 $params -> {messages
} -> {$_} -> {src
} => $_
173 } keys %{$params -> {messages
}}; # we have to lookup reverse ...
176 $area =~ s
{(<img\s
+src
="([^"]+)"\s+width[^>]+>)} {
183 $area =~ s{<iframe\s+src="([^"]*)"[^>]+>.*?</iframe>} {[iframe: $1]}g;
186 $area =~ s{<img src="([^"]*)"[^>]*>}{[image: $1]}g;
189 $area =~ s{<a href="([^"]*)">.*?</a>}{[link: $1]}g;
196 ### sub message_field ($$) #####################################################
198 # prepare the posting text for visual output
200 # Params: $posting - scalar reference
201 # (raw posting text, 'encoded_body' encoded)
202 # $params - hash reference
203 # (quoteChars quoting startCite endCite)
205 # Return: scalar rerence (prepared posting text)
207 sub message_field ($$) {
208 my $posting = ${+shift};
209 my $params = shift || {};
211 my $break = '<br />';
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]};
228 $posting = join $break => map {
230 ? (($ll and $ll != $_->[0]) ? $break : '') .
231 join join ($break => @{$_->[-1]})
232 => ($params->{startCite}, $params->{endCite})
233 : (join $break => @{$_->[-1]});
234 $ll = $_->[0]; $string;
238 my $qchar = $params -> {quoteChars};
239 $posting =~ s/\177/$qchar/g; # \177 => quote chars
246 # keeping 'require' happy
251 ### end of Encode::Posting #####################################################
patrick-canterino.de