in reply to Re^2: Text to HTML convert.
in thread Text to HTML convert.
Something like this is probably all you need
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!<a href="$_">!.escapeHTML($_).qq +!</a>! : m/^www\./ ? qq!<a href="http://$_">!.escapeHTML +($_).qq!</a>! : m/^[^@]+@[^@]/ ? qq!<a href="mailto:$_">!.escapeHTML +($_).qq!</a>! : escapeHTML($_); }ge; # fix whitespace (and newlines if not using pre tags) $text =~ s/( {2,})/" " x length $1/eg; #$text =~ s/\n/<br>\n/g; # wrap in pre tags $text = '<pre>' . $text . '</pre>'; return $text; } sub escapeHTML { my ( $escape ) = @_; return '' unless defined $escape ; $escape =~ s/&/&/g; $escape =~ s/"/"/g; $escape =~ s/</</g; $escape =~ s/>/>/g; $escape =~ s/([^\000-\177])/'&#' . (sprintf "%3d", ord $1) . ';'/e +g; return $escape; }
cheers
tachyon
|
|---|