Skip to content

Commit

Permalink
better CSS optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Pilgrim committed Sep 27, 2009
1 parent a57a28a commit d06da80
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
3 changes: 1 addition & 2 deletions mobile.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Acknowledgements & Inspirations

body, .c, span, pre span, .c, .note, p, ul, ol {
font:normal 12px/18px sans-serif;
word-spacing:0;
}
pre, kbd, samp, code, var {
font:normal 12px/18px monospace;
Expand All @@ -60,7 +59,7 @@ html {
color:#000;
}
body {
margin:4px 1px 0 1px;
margin:4px 2px 0 2px;
}

/* links */
Expand Down
8 changes: 2 additions & 6 deletions print.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ POSSIBILITY OF SUCH DAMAGE.
/* typography */

body, .w a {
font: 12pt/1.75 'Gill Sans', 'Gill Sans MT', Helvetica, Corbel, 'Nimbus Sans L', sans-serif;
word-spacing: 0;
font-size: 12pt;
}
pre, kbd, samp, code, var, .b {
font-size: 10pt;
Expand All @@ -50,16 +49,13 @@ span {
font: normal 48pt/0.68 serif;
}
p, ul, ol {
margin: 1.75em 0;
font-size: 12pt;
}

/* basics */

html {
background: #fff;
}
html, body {
background: #fff;
margin: 0;
padding: 0;
}
Expand Down
15 changes: 6 additions & 9 deletions publish
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,13 @@ java -jar util/yuicompressor-2.4.2.jar build/dip3.css > build/$revision.css && \
sed -i -e "s|;}|}|g" build/m-$revision.css && \
sed -i -e "s|;}|}|g" build/p-$revision.css || die "Failed to minimize CSS"

# put CSS inline
# put CSS inline and remove unused CSS properties on a page-by-page basis
echo "inlining CSS"
css=`cat build/$revision.css`
mobilecss=`cat build/m-$revision.css`
printcss=`cat build/p-$revision.css`
sed -i -e "s|<link rel=stylesheet href=dip3.css>|<style>${css}</style>|g" -e "s|<link rel=stylesheet media='only screen and (max-device-width: 480px)' href=mobile.css>|<style>@media screen and (max-device-width:480px){${mobilecss}}</style>|g" -e "s|<link rel=stylesheet media=print href=print.css>|<style>@media print{${printcss}}</style>|g" -e "s|</style><style>||g" build/*.html || die "Failed to inline CSS"

# remove unused CSS properties on a page-by-page basis
echo "removing unused CSS"
for f in build/*.html; do
python2.6 util/lesscss.py "$f" || die "Failed to remove unused CSS"
css=`python2.6 util/lesscss.py "$f" "build/$revision.css"` || die "Failed to remove unused CSS"
mobilecss=`python2.6 util/lesscss.py "$f" "build/m-$revision.css"` || die "Failed to remove unused CSS"
printcss=`python2.6 util/lesscss.py "$f" "build/p-$revision.css"` || die "Failed to remove unused CSS"
sed -i -e "s|<link rel=stylesheet href=dip3.css>|<style>${css}</style>|g" -e "s|<link rel=stylesheet media='only screen and (max-device-width: 480px)' href=mobile.css>|<style>@media screen and (max-device-width:480px){${mobilecss}}</style>|g" -e "s|<link rel=stylesheet media=print href=print.css>|<style>@media print{${printcss}}</style>|g" -e "s|</style><style>||g" "$f" || die "Failed to inline CSS"
done

# minimize URLs
Expand All @@ -127,6 +123,7 @@ chmod 755 build/examples build/j build/i build/d && \
chmod 644 build/*.html build/*.css build/*.txt build/*.zip build/examples/* build/examples/.htaccess build/j/* build/j/.htaccess build/i/* build/i/.htaccess build/d/.htaccess build/.htaccess || die "Failed to reset file permissions"

# ship it!
#die "Aborting without publishing"
echo -n "publishing"
rsync -essh -a build/d/.htaccess build/*.zip diveintomark.org:~/web/diveintopython3.org/d/ && \
echo -n "." && \
Expand Down
13 changes: 10 additions & 3 deletions util/lesscss.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
SELECTOR_EXCEPTIONS = ('.w', '.b', '.str', '.kwd', '.com', '.typ', '.lit', '.pun', '.tag', '.atn', '.atv', '.dec', 'pre .u', 'pre .u span', 'td .u', 'li ol', 'a.hl:link', 'a.hl:visited', 'a.hl:hover', 'h2[id]:hover a.hl', 'h3[id]:hover a.hl')

filename = sys.argv[1]
cssfilename = sys.argv[2]
pqd = pq(filename=filename)
raw_data = open(filename, 'rb').read()

with open(filename, 'rb') as fopen:
raw_data = fopen.read()

if raw_data.count('</a><script src=j/'): # HACK HACK HACK
def keep(s):
for selector in SELECTOR_EXCEPTIONS:
Expand All @@ -22,11 +26,14 @@ def keep(s):
def keep(s):
return False

original_css = raw_data.split('<style>', 1)[1].split('@media', 1)[0]
with open(cssfilename, 'rb') as fopen:
original_css = fopen.read()

new_css = ''
for rule in original_css.split('}')[:-1]:
selectors, properties = rule.split('{', 1)
selectors = ','.join([s for s in selectors.split(',') if keep(s) or pqd(s.split(':', 1)[0])])
if selectors:
new_css += '%s{%s}' % (selectors, properties)
open(filename, 'wb').write(raw_data.replace(original_css, new_css))

sys.stdout.write(new_css)

0 comments on commit d06da80

Please sign in to comment.