Skip to content

Instantly share code, notes, and snippets.

@onishi
Created January 27, 2011 03:56
Show Gist options
  • Save onishi/798043 to your computer and use it in GitHub Desktop.
Save onishi/798043 to your computer and use it in GitHub Desktop.
indent_html
sub indent_html {
my $html = shift;
my $indent = 0;
return join "\n", grep { $_ !~ /^\s*$/ } map {
s{(^\s+|\s+$)}{}g;
if (m{</}) {
$indent--;
}
$indent = 0 if $indent < 0;
my $res = ' ' x $indent . $_;
if (m{/>}) {
} elsif (m{<(?!/)}) {
$indent++;
}
$res;
} (
$html =~ m{
<[^>]*>
|
[^<>]+
}xg
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment