Skip to content

Instantly share code, notes, and snippets.

@S2
Created June 14, 2018 05:41
Show Gist options
  • Save S2/dd0366d62f75a0a70b205158f56445cb to your computer and use it in GitHub Desktop.
Save S2/dd0366d62f75a0a70b205158f56445cb to your computer and use it in GitHub Desktop.
むしゃくしゃしてCSS整形するやつ作った
#!/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