]>
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> #
9 # Description: prepare a Posting text for saving and visual (HTML) output #
11 ################################################################################
18 use Encode
::Plain
; $Encode::Plain
::utf8
= 1;
21 ################################################################################
29 sub VERSION
{(q
$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'}
31 ################################################################################
35 use base
qw(Exporter);
42 ### sub rel_uri ($$) ###########################################################
44 # generate an absolute URI from a absolute|relative one
45 # (not for public use)
50 # Return: abs URI as string
53 my ($uri, $base) = @_;
55 "http://$ENV{HTTP_HOST}".
61 ### sub encoded_body ($;$) #####################################################
63 # prepare posting text for saving
65 # Params: $posting - scalar reference of the raw text
66 # $params - hash reference
67 # (quoteChars messages)
69 # Return: scalar reference of the encoded text
71 sub encoded_body
($;$) {
72 my $posting = ${+shift};
75 $posting =~ s/\015\012|\015|\012/\n/g; # normalize newlines
76 $posting =~ s/[^\S\n]+$//gm; # kill whitespaces at the end of all lines
77 $posting =~ s/\s+$//; # kill whitespaces (newlines) at the end of the string (text)
79 # check the special syntaxes:
81 my $base = $params -> {base_uri
};
82 # collect all [link:...] strings
85 push @rawlinks => [$1 => $2] while ($posting =~ /\[([Ll][Ii][Nn][Kk]):\s*([^\]\s]+)\s*\]/g);
87 is_URL
( $_ -> [1] => qw(http ftp news nntp telnet gopher mailto))
88 or is_URL
(($_ -> [1] =~ /^[Vv][Ii][Ee][Ww]-[Ss][Oo][Uu][Rr][Cc][Ee]:(.+)/)[0] || '' => 'http')
89 or ( $_ -> [1] =~ m
<^(?
:\
.?\
.?
/(?!/)|\?)>
90 and is_URL
(rel_uri
($_ -> [1], $base) => 'http'))
93 # collect all [image:...] strings
96 push @rawimages => [$1 => $2] while ($posting =~ /\[([Ii][Mm][Aa][Gg][Ee]):\s*([^\]\s]+)\s*\]/g);
98 is_URL
($_ -> [1] => 'strict_http')
99 or ( $_ -> [1] =~ m
<^(?
:\
.?\
.?
/(?!/)|\?)>
100 and is_URL
(rel_uri
($_ -> [1], $base) => 'http'))
103 # collect all [iframe:...] strings
106 push @rawiframes => [$1 => $2] while ($posting =~ /\[([Ii][Ff][Rr][Aa][Mm][Ee]):\s*([^\]\s]+)\s*\]/g);
108 is_URL
($_ -> [1] => 'http')
109 or ( $_ -> [1] =~ m
<^(?
:\
.?\
.?
/(?!/)|\?)>
110 and is_URL
(rel_uri
($_ -> [1], $base) => 'http'))
113 # collect all [msg:...] strings
115 $params -> {messages
} = {} unless (defined $params -> {messages
});
116 my %msg = map {lc($_) => $params -> {messages
} -> {$_}} keys %{$params -> {messages
}};
119 push @rawmsgs => [$1 => $2] while ($posting =~ /\[([Mm][Ss][Gg]):\s*([^\]\s]+)\s*\]/g);
120 my @msgs = grep {exists ($msg{lc($_ -> [1])})} @rawmsgs;
122 # encode Entities and special characters
124 $posting = ${plain
(\
$posting)};
126 # encode the special syntaxes
128 $posting =~ s!$_!<a href="$1">$1</a>!
129 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @links);
131 $posting =~ s!$_!<img src="$1" border=0 alt="">!
132 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @images);
134 $posting =~ s!$_!<iframe src="$1" width="90%" height="90%"><a href="$1">$1</a></iframe>!
135 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @iframes);
137 %msg = map {plain
($_) => $msg{$_}} keys %msg;
138 $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
139 for (map {qr/\[\Q${plain(\$_->[0])}\E:\s*(\Q${plain(\$_->[1])}\E)\s*\]/} @msgs);
141 # normalize quote characters (quote => \177)
143 my $quote = plain
(defined $params -> {quoteChars
} ?
$params -> {quoteChars
} : '');
144 my $len = length ($quote);
145 $posting =~ s!^((?:\Q$quote\E)+)!"\177" x (length($1)/$len)!gem if ($len);
147 # \n => <br>, fix spaces
149 $posting = ${multiline
(\
$posting)};
156 ### sub answer_field ($$) ######################################################
158 # create the content of the answer textarea
160 # Params: $posting - scalar reference
161 # (posting text, 'encoded_body' encoded)
162 # $params - hash reference
163 # (quoteArea quoteChars messages)
165 # Return: scalar reference
167 sub answer_field
($$) {
169 my $params = shift || {};
171 my $area = $$posting;
172 my $qchar = $params -> {quoteChars
};
174 $area =~ s/<br(?:\s*\/)?>/\n/g
; # <br> => \n
175 $area =~ s/&(?:#160|nbsp);/ /g; # nbsp => ' '
177 $area =~ s/^(.)/\177$1/gm if ($params -> {quoteArea
}); # shift a quoting character
178 $area =~ s/^(\177+)/$qchar x length ($1)/gem; # decode normalized quoting characters
180 # recode special syntaxes
181 # from HTML to [...] constructions
183 $params -> {messages
} = {} unless (defined $params -> {messages
}); # avoid error messages
185 $params -> {messages
} -> {$_} -> {src
} => $_
186 } keys %{$params -> {messages
}}; # we have to lookup reverse ...
189 $area =~ s
{(<img\s
+src
="([^"]+)"\s+width[^>]+>)} {
196 $area =~ s{<iframe\s+src="([^"]*)"[^>]+>.*?</iframe>} {[iframe: $1]}g;
199 $area =~ s{<img src="([^"]*)"[^>]*>}{[image: $1]}g;
202 $area =~ s{<a href="([^"]*)">.*?</a>}{[link: $1]}g;
209 ### sub message_field ($$) #####################################################
211 # prepare the posting text for visual output
213 # Params: $posting - scalar reference
214 # (raw posting text, 'encoded_body' encoded)
215 # $params - hash reference
216 # (quoteChars quoting startCite endCite)
218 # Return: scalar rerence (prepared posting text)
220 sub message_field ($$) {
221 my $posting = ${+shift};
222 my $params = shift || {};
224 my $break = '<br />';
226 if ($params -> {quoting}) { # quotes are displayed as special?
227 my @array = [0 => []];
229 for (split /<br(?:\s*\/)?>/ => $posting) {
230 my $l = length ((/^(\177*)/)[0]);
231 if ($array[-1][0] == $l) {
232 push @{$array[-1][-1]} => $_;
235 push @array => [$l => [$_]];
238 shift @array unless @{$array[0][-1]};
241 $posting = join $break => map {
243 ? (($ll and $ll != $_->[0]) ? $break : '') .
244 join join ($break => @{$_->[-1]})
245 => ($params->{startCite}, $params->{endCite})
246 : (join $break => @{$_->[-1]});
247 $ll = $_->[0]; $string;
251 my $qchar = $params -> {quoteChars};
252 $posting =~ s/\177/$qchar/g; # \177 => quote chars
259 # keep 'require' happy
264 ### end of Encode::Posting #####################################################
patrick-canterino.de