From 2fa625ec63d666d5d2c2d13d67c78762c310e098 Mon Sep 17 00:00:00 2001 From: Joshua Sherman Date: Sun, 2 Feb 2014 16:41:52 -0500 Subject: [PATCH] Initial commit. --- README.md | 10 ++++++- dry.css | 15 ++++++++++ wet.css | 15 ++++++++++ wetness | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 dry.css create mode 100644 wet.css create mode 100755 wetness diff --git a/README.md b/README.md index 9643dc4..8fcc921 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,12 @@ wetness ======= -Check how wet your CSS is +`wetness` is a `bash` script for checking a CSS file for duplicate properties. + +Just like [Ellie Kemper][BJ], the aim of this script is to help make it DRY. + + [BJ]: http://www.collegehumor.com/video/1183463/derrick-comedy-blowjob + +## Usage + + ./wetness /path/to/file.css diff --git a/dry.css b/dry.css new file mode 100644 index 0000000..6b11054 --- /dev/null +++ b/dry.css @@ -0,0 +1,15 @@ +body, +html +{ + width: 100%; +} + +body +{ + font-size: 1em; +} + +.lead +{ + font-size: 150%; +} diff --git a/wet.css b/wet.css new file mode 100644 index 0000000..47f6592 --- /dev/null +++ b/wet.css @@ -0,0 +1,15 @@ +html +{ + width: 100%; +} + +body +{ + width: 100%; + font-size: 1em; +} + +.lead +{ + font-size: 150%; +} diff --git a/wetness b/wetness new file mode 100755 index 0000000..e7dd09a --- /dev/null +++ b/wetness @@ -0,0 +1,82 @@ +#!/usr/bin/env bash + +if [ -z $1 ]; +then + echo 'Usage: wetness /path/to/file.css' + exit 1 +fi + +css_file=$1 + +if [ ! -e $css_file ]; +then + echo 'Error: Supplied CSS file does not exist.' + exit 1 +fi + +declare -A properties +declare -A dupes +inside=false + +strip() +{ + echo `echo $1 | sed -e 's/^ *//g' -e 's/ *$//g' -e 's/;//'` +} + +cat $css_file | \ +{ + while read line + do + if [[ $line =~ '{' && ! $line =~ '//' ]] + then + inside=true + elif [[ $line =~ '}' && ! $line =~ '//' ]] + then + inside=false + elif [[ $inside == true && $line =~ ':' ]] + then + property=`echo $line | awk -F':' '{print $1}'` + property=`strip $property` + value=`echo $line | awk -F':' '{print $2}'` + value=`strip $value` + + line="$property: $value;" + + if [ ${properties[$line]+exists} ] + then + occurrences=$(( ${properties[$line]} + 1 )) + properties[$line]=$occurrences + dupes[$line]=$occurrences + else + properties[$line]=1 + fi + fi + done + + dupe_count=${#dupes[@]} + + if [ $dupe_count == 0 ] + then + echo -e '\e[32mGREAT JOB! No duplicate properties were detected :)\e[0m' + else + echo -en "\e[31mGood try, but I found $dupe_count duplicated propert" + + if [ $dupe_count == 1 ] + then + echo -n 'y' + else + echo -n 'ies' + fi + + echo -e " :(\e[0m" + + for i in "${!dupes[@]}" + do + echo -en " \e[1;37m${dupes[$i]}\e[0m: \e[34m" + echo $i | awk -F':' '{print $1}' | tr -d '\n' + echo -en '\e[0m:\e[33m' + echo $i | awk -F':' '{print $2}' | tr -d '\n' + echo -e "\e[0m" + done + fi +}