Skip to content

Commit

Permalink
better css code
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Sep 24, 2015
1 parent 8ac712f commit cc03491
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/admin_scripts/extra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from . import lib
from . import log

from .css import css_slimmer
from .css import uniqify, simplify_hex_colors, css_slimmer
from .js import javascript_minify
from .lib import normalize_path, configuration
from .log import echo, warn
35 changes: 21 additions & 14 deletions src/admin_scripts/extra/css.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@
HEX_COLOR = re.compile(r"#\w{2}\w{2}\w{2}")

def uniqify(all):
"""
borrowed from Tim Peters" algorithm on ASPN Cookbook
"""

# REMEMBER! This will shuffle the order of the list
u = {}
for each in all:
u[each] = 1
return u.keys()
unique = dict()
for each in all: unique[each] = 1
return unique.keys()

def simplify_hex_colors(text):
"""
Replace all color declarations where pairs repeat.
I.e. #FFFFFF => #FFF; #CCEEFF => #CEF
and #EFEFEF, #EFCDI9 avoided.
(eg: #aabbcc becomes #abc).
@type text: String
@param text: The payload text value that is going to
be simplified.
@rtype: String
@return: The simplified value according to the defined
set of rules.
"""

colour_replacements = {}
Expand All @@ -69,15 +69,22 @@ def simplify_hex_colors(text):
if e[1] == e[2] and e[3] == e[4] and e[5] == e[6]:
colour_replacements[e] = "#" + e[1] + e[3] + e[5]

for k, v in colour_replacements.items():
text = text.replace(k, v)
for key, value in colour_replacements.items():
text = text.replace(key, value)

return text

def css_slimmer(css):
"""
Remove repeating whitespace characters like tab, newline
Removes repeating whitespace characters like tab, newline
or any other characters.
@type css: String
@param css: The string that contains the complete set of
css code that is going to the "slimmed".
@rtype: String
@return: The final simplified/reduced set of css code that
should represent the same original logic.
"""

# verifies the data type of the provided string
Expand Down
54 changes: 54 additions & 0 deletions src/admin_scripts/test/css.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Administration Scripts
# Copyright (c) 2008-2015 Hive Solutions Lda.
#
# This file is part of Hive Administration Scripts.
#
# Hive Administration Scripts is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Administration Scripts is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Administration Scripts. If not, see <http://www.apache.org/licenses/>.

__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2015 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

import unittest

import admin_scripts

class ModelTest(unittest.TestCase):

def test_simplify_hex_colors(self):
result = admin_scripts.simplify_hex_colors("#aabbcc")
self.assertEqual(result, "#abc")

result = admin_scripts.simplify_hex_colors("#AABBCC")
self.assertEqual(result, "#ABC")

result = admin_scripts.simplify_hex_colors("#AaBbCc")
self.assertEqual(result, "#AaBbCc")

0 comments on commit cc03491

Please sign in to comment.