Skip to content

Instantly share code, notes, and snippets.

@masak
Created November 13, 2008 22:32
Show Gist options
  • Save masak/24663 to your computer and use it in GitHub Desktop.
Save masak/24663 to your computer and use it in GitHub Desktop.
method format($text) {
my @result_pars;
my @split;
my $text_copy = $text;
while $text_copy.index("\n\n") -> $ix {
push @split, $text_copy.substr(0, $ix);
$text_copy .= substr($ix);
while $text_copy.substr(0,1) eq "\n" {
$text_copy .= substr(1);
}
}
if $text_copy {
push @split, $text_copy;
}
for @split -> $paragraph {
my @cleaned;
my $paragraph_copy = $paragraph;
while $paragraph_copy ~~ /\s/ {
push @cleaned, $paragraph_copy.substr(0, $/.from);
$paragraph_copy .= substr($/.from);
while $paragraph_copy ~~ /^\s/ {
$paragraph_copy .= substr(1);
}
}
if $paragraph_copy {
push @cleaned, $paragraph_copy;
}
my $cleaned_of_whitespace = join ' ', @cleaned;
push @result_pars, "<p>$cleaned_of_whitespace</p>";
}
return join "\n\n", @result_pars;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment