Created
June 14, 2018 05:41
-
-
Save S2/dd0366d62f75a0a70b205158f56445cb to your computer and use it in GitHub Desktop.
むしゃくしゃしてCSS整形するやつ作った
This file contains 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
#!/usr/bin/env perl | |
use 5.16.2; | |
use strict; | |
use warnings; | |
use utf8; | |
for my $f (@ARGV){ | |
my $s = ""; | |
{ | |
open my $fh , '<' , $f or die 'cant open file !'; | |
while(<$fh>){ | |
$s .= $_; | |
} | |
close $fh; | |
} | |
$s =~ s/\r|\n//gm; | |
$s =~ s/\{/\{\n/gm; | |
$s =~ s/\}/\n\}/gm; | |
$s =~ s/;/;\n/gm; | |
$s =~ s/^ *//gm; | |
$s =~ s/^$(\r|\n)//gm; | |
$s =~ s/([^(\{|\})][^;])$/$1;/gm; | |
$s =~ s/\{;/{/gm; | |
$s =~ s/\}/\}\n\n/gm; | |
$s =~ s/^([^@](.*;))/ $1/gm; | |
$s =~ s!/\*.*?\*/!!gm; | |
my @s = split("\n" => $s); | |
my $indent = 0; | |
my $media_indent = 0; | |
for (0..@s -1){ | |
if($s[$_] =~ /\{/){ | |
$indent++; | |
} | |
if($s[$_] =~ /\}/){ | |
$indent--; | |
} | |
if($media_indent && $s[$_] =~ /\}/ && $indent == 0){ | |
$media_indent--; | |
} | |
if($media_indent){ | |
if($s[$_] eq ";"){ | |
$s[$_] = "" | |
}else{ | |
$s[$_] = " " . $s[$_]; | |
} | |
} | |
if($s[$_] =~ /\{/ && $s[$_] =~ /\@/){ | |
$media_indent ++; | |
} | |
} | |
@s = map{$_ eq ";" ? "" : $_}@s; | |
$s = join("\n" => @s); | |
{ | |
open my $fh , '>' , $f or die 'cant open file !'; | |
print $fh $s; | |
close $fh; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment