Skip to content

Instantly share code, notes, and snippets.

@markandrus
Created June 11, 2011 00:29
Show Gist options
  • Select an option

  • Save markandrus/1020086 to your computer and use it in GitHub Desktop.

Select an option

Save markandrus/1020086 to your computer and use it in GitHub Desktop.
List CSS class and id usage within a directory of files
#! /bin/sh
# stylecheck.sh:
#
# Find unused CSS class and id selectors, given a CSS file and a directory
# of HTML documents.
#
# $1 = CSS document, $2 = HTML directory
#
# Returns a report in YAML.
#
# by Mark
echo -e "# Analyzing usage of $1 in $0\n---"
classes=`grep -o -P '(?<=\.)[a-zA-Z][a-zA-z0-9_\-]*(?=([ ,\n]*[a-zA-Z0-9_\-#\.\:]*)*{)' <"$1" | sort | uniq`
ids=`grep -o -P '(?<=#)[a-zA-Z][a-zA-z0-9_\-]*(?=([ ,\n]*[a-zA-Z0-9_\-#\.\:]*)*{)' <"$1" | sort | uniq`
echo "CSS classes:"
for s in $classes; do
echo " $s:"
for f in `ack "class=\"( |[a-zA-Z0-9]|-)*$s( |[a-zA-Z0-9]|-)*\"" $2 --type=nocss -l`; do
echo " - $f"
done
done
echo -e "\nCSS ids:"
for s in $ids; do
echo " $s:"
for f in `ack "id=\"$s\"" $2 --type=nocss -l`; do
echo " - $f"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment