70 lines
1.3 KiB
Perl
Executable file
70 lines
1.3 KiB
Perl
Executable file
#!/usr/bin/env perl
|
|
|
|
# old
|
|
# use strict;
|
|
# use warnings;
|
|
|
|
# my $Indent = 0;
|
|
# my $Blank_Lines = 0;
|
|
# my $Open = qr/[{\[(]$/;
|
|
# my $Close = qr/^[}\])]/;
|
|
# while (<>) {
|
|
# s/^\h*//;
|
|
# $Blank_Lines++, next if m/^$/;
|
|
# my $Comment = m/^#/;
|
|
# $Indent-- if m/$Close/ and not $Comment;
|
|
# $Blank_Lines = 0, print "\n" if $Blank_Lines;
|
|
# print "\t" x $Indent, $_;
|
|
# $Indent++ if m/$Open/ and not $Comment;
|
|
# }
|
|
#!/usr/bin/env perl
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
my $Indent = 0;
|
|
my $Blank_Lines = 0;
|
|
my $Open = qr/[{\[(]$/;
|
|
my $Close = qr/^[}\])]/;
|
|
my $InShell = 0;
|
|
my $ShellIndent = 0;
|
|
|
|
while (<>) {
|
|
s/^\h*//;
|
|
|
|
# Track blank lines
|
|
if (/^$/) {
|
|
$Blank_Lines++;
|
|
next;
|
|
}
|
|
|
|
# Detect start of %sh{
|
|
if (/^%sh\s*{/) {
|
|
print "\t" x $Indent, $_;
|
|
$InShell = 1;
|
|
$ShellIndent = 0;
|
|
next;
|
|
}
|
|
|
|
# Detect end of shell block
|
|
if ($InShell && /^\}/) {
|
|
$InShell = 0;
|
|
print "\t" x $Indent, $_;
|
|
next;
|
|
}
|
|
|
|
if ($InShell) {
|
|
$ShellIndent-- if /^\s*(?:fi|done|esac|elif|else|elif|;;)/;
|
|
print "\t" x ($Indent + $ShellIndent), $_;
|
|
$ShellIndent++ if /\b(?:then|do|case)\b(?:\s*#.*)?$/ or /{\s*$/;
|
|
next;
|
|
}
|
|
|
|
my $Comment = m/^#/;
|
|
$Indent-- if m/$Close/ and not $Comment;
|
|
print "\n" if $Blank_Lines;
|
|
$Blank_Lines = 0;
|
|
print "\t" x $Indent, $_;
|
|
$Indent++ if m/$Open/ and not $Comment;
|
|
}
|
|
|