Skip to content

Instantly share code, notes, and snippets.

@masak
Created November 13, 2008 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/24656 to your computer and use it in GitHub Desktop.
Save masak/24656 to your computer and use it in GitHub Desktop.
method format($text) {
my @result_pars;
my @split = gather {
my $text_copy = $text;
while $text_copy.index("\n\n") -> $ix {
take $text_copy.substr(0, $ix);
$text_copy .= substr($ix);
while $text_copy.substr(0,1) eq "\n" {
$text_copy .= substr(1);
}
}
if $text_copy {
take $text_copy;
}
}
for @split -> $paragraph {
my $cleaned_of_whitespace = join ' ', gather {
my $paragraph_copy = $paragraph;
while $paragraph_copy ~~ /\s/ {
take $paragraph_copy.substr(0, $/.from);
$paragraph_copy .= substr($/.from);
while $paragraph_copy ~~ /^\s/ {
$paragraph_copy .= substr(1);
}
}
if $paragraph_copy {
take $paragraph_copy;
}
}
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