sub get_text { my ($text, $cols) = @_; $cols ||= 80; require Text::Wrap; $Text::Wrap::columns = $cols; # tabs to 4 spaces $text =~ s/\t/ /g; # now wrap it $text = Text::Wrap::wrap('','',$text); # iterate over chunks, escaping HTML and linking $text =~ s{(\S+)} { local $_ = $1; m/^(?:http|ftp)/ ? qq!!.escapeHTML($_).qq!! : m/^www\./ ? qq!!.escapeHTML($_).qq!! : m/^[^@]+@[^@]/ ? qq!!.escapeHTML($_).qq!! : escapeHTML($_); }ge; # fix whitespace (and newlines if not using pre tags) $text =~ s/( {2,})/" " x length $1/eg; #$text =~ s/\n/
\n/g; # wrap in pre tags $text = '
' . $text . '
'; return $text; } sub escapeHTML { my ( $escape ) = @_; return '' unless defined $escape ; $escape =~ s/&/&/g; $escape =~ s/"/"/g; $escape =~ s//>/g; $escape =~ s/([^\000-\177])/'&#' . (sprintf "%3d", ord $1) . ';'/eg; return $escape; }