Created
January 27, 2011 03:56
-
-
Save onishi/798043 to your computer and use it in GitHub Desktop.
indent_html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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