Permalink
Browse files

better CSS optimization

  • Loading branch information...
1 parent a57a28a commit d06da808f774c122efb86768a2ba6ea822afcdb0 Mark Pilgrim committed Sep 27, 2009
Showing with 19 additions and 20 deletions.
  1. +1 −2 mobile.css
  2. +2 −6 print.css
  3. +6 −9 publish
  4. +10 −3 util/lesscss.py
View
@@ -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;
@@ -60,7 +59,7 @@ html {
color:#000;
}
body {
- margin:4px 1px 0 1px;
+ margin:4px 2px 0 2px;
}
/* links */
View
@@ -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;
@@ -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;
}
View
15 publish
@@ -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
@@ -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 "." && \
View
@@ -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:
@@ -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.