Last active
October 6, 2015 02:43
-
-
Save stecman/e65dc7c48a244aaaed8d to your computer and use it in GitHub Desktop.
Script to fix vertical whitespace in LESS/CSS-like files
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 php | |
<?php | |
/** | |
* Fix vertical whitespace in LESS/CSS-like files | |
* | |
* This script fixes selectors that are bunched together with no spacing, and selectors | |
* that are bunched together with properties and have no vertical spacing. This could be | |
* done with sed, but the regex escaping was a pain and it's harder to document. | |
* | |
* Usage: | |
* | |
* less-fix [file, ...] | |
* less-fix $(find . -name '*.less') | |
* | |
* Note that this script only cares about whitespace that is too tight; it won't remove | |
* extra spaces after selectors and properties, it just ignores them. | |
*/ | |
function applyReplacements($data, &$numReplacements = 0) | |
{ | |
$numReplacements = 0; | |
$replacements = [ | |
// Fix no breathing room between selectors | |
// Finds closing braces and add a space after, with some exclusion rules | |
'\}\n(?! | |
(\s*\}| # No extra space between multiple adjacent closing braces | |
\n+| # Skip already adequate spacing | |
$| # No extra line after last selector | |
.*%| # Exclude animation percentage selectors which are ok to bunch | |
.*\{.*\} # Single line selectors can bunch with each other (not perfect) | |
))' => "}\n\n\\1", | |
// Fix no breathing room between properties and nested selectors | |
// Finds property lines with a selector line immedidately after them | |
';\n(.*,|.*\{)\n' => ";\n\n\\1\n" | |
]; | |
foreach ($replacements as $pattern => $replacement) { | |
$data = preg_replace("/$pattern/ix", $replacement, $data, -1, $subcount); | |
$numReplacements += $subcount; | |
} | |
return $data; | |
} | |
array_shift($argv); | |
$files = $argv; | |
foreach ($files as $file) { | |
if (file_exists($file)) { | |
$data = applyReplacements( | |
file_get_contents($file), | |
$numReplacements | |
); | |
if ($numReplacements) { | |
echo "Fixed $file ($numReplacements replacements)\n"; | |
file_put_contents($file, $data); | |
} | |
} else { | |
fwrite(STDERR, "No file exists for $file\n"); | |
} | |
} |
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
# Fix vertical whitespace in LESS/CSS-like files | |
# | |
# This plugin fixes selectors bunched vertically (missing vertical whitespace), and | |
# selectors bunched against properties. Note that this only cares about whitespace | |
# being too tight; it won't remove superfluous vertical space. | |
import sublime, sublime_plugin | |
import re | |
class LessWhitespaceFixCommand(sublime_plugin.TextCommand): | |
def fix_whitespace(self, contents): | |
# Fix no breathing room between selectors | |
# Finds closing braces and add a space after, with some exclusion rules | |
selectorBreathingRoom = re.compile(r''' | |
\}\n(?! | |
( | |
\s*\}| # Don't add space between adjacent closing braces | |
\n+| # Skip already adequate spacing | |
$| # No extra line after last selector | |
.*%| # Exclude animation percentage selectors which are ok to bunch | |
.*\{.*\} # Single line selectors can bunch with each other (not ideal, but meh) | |
) | |
) | |
''', re.VERBOSE) | |
propertyBreathingRoom = re.compile(r';\n(.*,|.*\{)\n') | |
contents = re.sub(selectorBreathingRoom, r'}\n\n', contents) | |
contents = re.sub(propertyBreathingRoom, r';\n\n\1\n', contents) | |
return contents | |
def run(self, edit): | |
regionAll = sublime.Region(0, self.view.size()) | |
fixedText = self.fix_whitespace(self.view.substr(regionAll)) | |
self.view.replace(edit, regionAll, fixedText) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment