/g;
}
} elsif ($contentType eq "text") {
$content =~ s/ / /g;
} elsif ($contentType eq "code") {
$content =~ s/ / /g;
$content = '
'.$content.'
';
}
return $content;
}
#-------------------------------------------------------------------
=head2 html2text ( html )
Converts html to text. It currently handles only text, so tables
or forms are not converted.
=head3 html
The html segment you want to convert to text.
=cut
# for recursive function
my $text = "";
my $inside = {};
sub html2text {
my $html = shift;
$text = "";
$inside = {};
my $tagHandler = sub {
my($tag, $num) = @_;
$inside->{$tag} += $num;
if($tag eq "br" || $tag eq "p") {
$text .= "\n";
}
};
my $textHandler = sub {
return if $inside->{script} || $inside->{style};
if ($_[0] =~ /\S+/) {
$text .= $_[0];
}
};
HTML::Parser->new(api_version => 3,
handlers => [start => [$tagHandler, "tagname, '+1'"],
end => [$tagHandler, "tagname, '-1'"],
text => [$textHandler, "dtext"],
],
marked_sections => 1,
)->parse($html);
return $text;
}
#-------------------------------------------------------------------
=head2 makeAbsolute ( session, html , [ baseURL ] )
Returns html with all relative links converted to absolute.
=head3 session
A reference to the current session.
=head3 html
The html to be made absolute.
=head3 baseURL
The base URL to use. Defaults to current page's url.
=cut
my $absolute = "";
sub makeAbsolute {
my $session = shift;
my $html = shift;
my $baseURL = shift;
$absolute = "";
my $linkParser = sub {
my ($tagname, $attr, $text) = @_;
my %linkElements = # from HTML::Element.pm
(
body => 'background',
base => 'href',
a => 'href',
img => [qw(src lowsrc usemap)], # lowsrc is a Netscape invention
form => 'action',
input => 'src',
'link' => 'href', # need quoting since link is a perl builtin
frame => 'src',
iframe => 'src',
applet => 'codebase',
area => 'href',
script => 'src',
iframe => 'src',
);
if(not exists $linkElements{$tagname}) { # no need to touch this tag
$absolute .= $text;
return;
}
# Build a hash with tag attributes
my %tag_attr;
for my $tag (keys %linkElements) {
my $tagval = $linkElements{$tag};
for my $attr (ref $tagval ? @$tagval : $tagval) {
$tag_attr{"$tag $attr"}++;
}
}
$absolute .= "<".$tagname;
foreach (keys %$attr) {
if($_ eq '/') {
$absolute .= '/';
next;
}
if ($tag_attr{"$tagname $_"}) { # make this absolute
$attr->{$_} = $session->url->makeAbsolute($attr->{$_}, $baseURL);
}
$absolute .= qq' $_="$attr->{$_}"';
}
$absolute .= '>';
};
HTML::Parser->new(
default_h => [ sub { $absolute .= shift }, 'text' ],
start_h => [ $linkParser , 'tagname, attr, text' ],
)->parse($html);
return $absolute;
}
#-------------------------------------------------------------------
=head2 processReplacements ( session, content )
Processes text using the WebGUI replacements system.
=head3 session
A reference to the current session.
=head3 content
The content to be processed through the replacements filter.
=cut
sub processReplacements {
my $session = shift;
my ($content) = @_;
my $replacements = $session->stow->get("replacements");
if (defined $replacements) {
foreach my $searchFor (keys %{$replacements}) {
my $replaceWith = $replacements->{$searchFor};
$content =~ s/\Q$searchFor/$replaceWith/gs;
}
} else {
my $sth = $session->db->read("select searchFor,replaceWith from replacements",$session->db->getSlave);
while (my ($searchFor,$replaceWith) = $sth->array) {
$replacements->{$searchFor} = $replaceWith;
$content =~ s/\Q$searchFor/$replaceWith/gs;
}
$sth->finish;
$session->stow->set("replacements",$replacements);
}
return $content;
}
1;