Skip to content

Commit

Permalink
Add hierarchical argument
Browse files Browse the repository at this point in the history
This will add the directory path in class name.

img/
  /A
    /B
       1.png

You will get .a_b_1
  • Loading branch information
Alexandre Paré committed Jun 4, 2014
1 parent ac60f5c commit 59c6503
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion glue/formats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def get_context(self, *args, **kwargs):
'hash': self.sprite.hash,
'name': self.sprite.name,
'sprite_path': sprite_path,
'path': self.sprite.path,
'sprite_filename': os.path.basename(sprite_path),
'width': round_up(self.sprite.canvas_size[0] / self.sprite.max_ratio),
'height': round_up(self.sprite.canvas_size[1] / self.sprite.max_ratio),
Expand All @@ -101,7 +102,8 @@ def get_context(self, *args, **kwargs):
width=round_up((img.width / self.sprite.max_ratio) + img.padding[1] + img.padding[3]),
original_width=img.original_width,
original_height=img.original_height,
ratios={})
ratios={},
path=img.path)

for r in self.sprite.ratios:
image['ratios'][r] = dict(filename=img.filename,
Expand Down
25 changes: 20 additions & 5 deletions glue/formats/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ def populate_argument_parser(cls, parser):
default=os.environ.get('GLUE_GENERATE_CSS', True),
help="Don't genereate CSS files.")

group.add_argument("--hierarchical",
dest="hierarchical",
action="store_true",
default=os.environ.get('GLUE_HIERARCHICAL', False),
help="This will add the directory path in class name.")

@classmethod
def apply_parser_contraints(cls, parser, options):
cachebusters = (options.css_cachebuster, options.css_cachebuster_filename, options.css_cachebuster_only_sprites)
Expand All @@ -139,10 +145,10 @@ def needs_rebuild(self):
return False

def validate(self):
class_names = [':'.join(self.generate_css_name(i.filename)) for i in self.sprite.images]
class_names = [':'.join(self.generate_css_name(i.filename, i.path, self.sprite.path)) for i in self.sprite.images]
if len(set(class_names)) != len(self.sprite.images):
dup = [i for i in self.sprite.images if class_names.count(':'.join(self.generate_css_name(i.filename))) > 1]
duptext = '\n'.join(['\t{0} => .{1}'.format(os.path.relpath(d.path), ':'.join(self.generate_css_name(d.filename))) for d in dup])
dup = [i for i in self.sprite.images if class_names.count(':'.join(self.generate_css_name(i.filename, i.path, self.sprite.path))) > 1]
duptext = '\n'.join(['\t{0} => .{1}'.format(os.path.relpath(d.path), ':'.join(self.generate_css_name(d.filename, i.path, self.sprite.path))) for d in dup])
raise ValidationError("Error: Some images will have the same class name:\n{0}".format(duptext))
return True

Expand All @@ -158,7 +164,8 @@ def get_context(self, *args, **kwargs):

# Generate css labels
for image in context['images']:
image['label'], image['pseudo'] = self.generate_css_name(image['filename'])

image['label'], image['pseudo'] = self.generate_css_name(image['filename'], image['path'], context['path'])

if self.sprite.config['css_url']:
context['sprite_path'] = '{0}{1}'.format(self.sprite.config['css_url'], context['sprite_filename'])
Expand All @@ -179,11 +186,19 @@ def apply_cachebuster(path):

return context

def generate_css_name(self, filename):
def generate_css_name(self, filename, filepath, spritePath):
filename = filename.rsplit('.', 1)[0]
separator = self.sprite.config['css_separator']
namespace = [re.sub(r'[^\w\-_]', '', filename)]

# Add sub directory
if self.sprite.config['hierarchical']:
dirPath = os.path.split(os.path.relpath(filepath, spritePath))[0]
while dirPath != '':
(dirPath, dirName) = os.path.split(dirPath)
dirName = re.sub(r'[^\w\-_]', '', dirName)
namespace.insert(0, dirName)

# Add sprite namespace if required
if self.sprite.config['css_sprite_namespace']:
sprite_name = re.sub(r'[^\w\-_]', '', self.sprite.name)
Expand Down

0 comments on commit 59c6503

Please sign in to comment.