Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #8 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion media_bundler/bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ def from_dict(cls, attrs):
return PngSpriteBundle(attrs["name"], attrs["path"], attrs["url"],
attrs["files"], attrs["type"],
attrs["css_file"])
elif attrs["type"] == "less":
return LessBundle(attrs["name"], attrs["path"], attrs["url"],
attrs["type"], attrs["files"])
else:
raise InvalidBundleType(attrs["type"])

Expand Down Expand Up @@ -141,6 +144,34 @@ def _make_bundle(self):
self.do_text_bundle(minifier)


class LessBundle(Bundle):
"""Bundle for Less css complier."""

def __init__(self, name, path, url, type, files):
super(LessBundle, self).__init__(name, path, url, files, type)

def get_extension(self):
return ".css"

def _make_bundle(self):
css_path = self.get_bundle_path()
tmp_path = css_path + '.tmp'
os.mkdir(tmp_path)
destinations = list()
for path in self.get_paths():
filename = os.path.basename(path)
destination = os.path.join(tmp_path, filename)
subprocess.call(['lessc', path, destination])
destinations.append(destination)
self.do_text_bundle(destinations, css_path)
shutil.rmtree(tmp_path)

def do_text_bundle(self, source_css_files, destination_css_file):
with open(destination_css_file, "w") as output:
generator = concatenate_files(source_css_files)
output.write("".join(generator))


class PngSpriteBundle(Bundle):

"""Bundle for PNG sprites.
Expand Down Expand Up @@ -217,13 +248,16 @@ def generate_css(self, packing):
css.write(self.make_css(os.path.basename(box.filename), props))

CSS_REGEXP = re.compile(r"[^a-zA-Z\-_]")
VALID_CSS_CLASS_REGEXP = re.compile(r"[_a-zA-Z]+[_a-zA-Z0-9-]")

def css_class_name(self, rule_name):
name = self.name
if rule_name:
name += "-" + rule_name
name = name.replace(" ", "-").replace(".", "-")
return self.CSS_REGEXP.sub("", name)
if(not self.VALID_CSS_CLASS_REGEXP.match(name)):
name = self.CSS_REGEXP.sub("", name)
return name

def make_css(self, name, props):
# We try to format it nicely here in case the user actually looks at it.
Expand Down