Skip to content

Commit

Permalink
Improve CSS parsing performance removing bloat in the CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Bastida committed Jan 11, 2012
1 parent 0cbab90 commit 9d27c2f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Expand Up @@ -5,3 +5,5 @@ Changelog
^^^^^
* New command line argument ``-z``, ``--no-size`` to avoid adding the image width and height to the sprite.
* New command line argument ``--png8`` forces the output image format to be png8 instead of png32.
* Improve CSS parsing performance removing bloat in the CSS.
* Improved documentation.
19 changes: 13 additions & 6 deletions glue.py
Expand Up @@ -466,23 +466,30 @@ def save_css(self):

css_file = open(css_filename, 'w')

This comment has been minimized.

Copy link
@exupero

exupero Jan 12, 2012

Would a with open(css_filename, 'w') as css_file: be preferable here? It would eliminate the need for css_file.close() on line 495.

I'm not sure how you feel about indenting such a big chunk of the code.


# get all the class names and join them
class_names = ['.%s' % i.class_name for i in self.images]

This comment has been minimized.

Copy link
@exupero

exupero Jan 12, 2012

If your targeting Python 2.6 or above, you should probably use '.{0}'.format(i.class_name) for string formatting.

class_names = ',\n'.join(class_names)

# create an unique style for all the sprites for less bloat
style = "%s{background-image:url('%s');background-repeat:no-repeat;}\n"
css_file.write(style % (class_names, self.image_url))

for image in self.images:
data = {'namespace': image.sprite.namespace,
'sprite_url': image.sprite.image_url,
'image_class_name': image.class_name,
data = {'image_class_name': image.class_name,
'top': image.node.y * -1 if image.node.y else 0,
'left': image.node.x * -1 if image.node.x else 0,
'width': image.width,
'height': image.height}

style = (".%(image_class_name)s{ "
"background:url('%(sprite_url)s') no-repeat "
"%(left)ipx %(top)ipx; ")
style = (".%(image_class_name)s{"
"background-position:%(left)ipx %(top)ipx;")

if self.config.size:
# if it's required add the image size to the sprite
style += "width:%(width)spx; height:%(height)spx;"

style += "}\n"

css_file.write(style % data)

css_file.close()
Expand Down

0 comments on commit 9d27c2f

Please sign in to comment.