Created
April 25, 2014 11:24
-
-
Save MiguelDebruyne/11286216 to your computer and use it in GitHub Desktop.
Ruby: Remove the random string in the sprite filename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Make a copy of sprites with a name that has no uniqueness of the hash. | |
| on_sprite_saved do |filename| | |
| if File.exists?(filename) | |
| FileUtils.cp filename, filename.gsub(%r{-s[a-z0-9]{10}\.png$}, '.png') | |
| # Note: Compass outputs both with and without random hash images. | |
| # To not keep the one with hash, add: (Thanks to RaphaelDDL for this) | |
| FileUtils.rm_rf(filename) | |
| end | |
| end | |
| # Replace in stylesheets generated references to sprites | |
| # by their counterparts without the hash uniqueness. | |
| on_stylesheet_saved do |filename| | |
| if File.exists?(filename) | |
| css = File.read filename | |
| File.open(filename, 'w+') do |f| | |
| f << css.gsub(%r{-s[a-z0-9]{10}\.png}, '.png') | |
| # Add a query string to the end of the filename | |
| # background-image: url('../img/ico.png?v=ab47cfd764'); | |
| # f << css.gsub(%r{(?<start>-s)(?<hash>[a-z0-9]{10})(?<file>\.png)}, '.png?v=\k<hash>') | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment