Skip to content

Commit

Permalink
Added python implementation with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mokkabonna committed Nov 10, 2013
1 parent 9789983 commit 564b84a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
31 changes: 25 additions & 6 deletions python/cssbeautifier/__init__.py
Expand Up @@ -42,7 +42,7 @@ def __repr__(self):
indent_char = [%s]
separate_selectors_newline = [%s]
end_with_newline = [%s]
""" % (self.indent_size, self.indent_char,
""" % (self.indent_size, self.indent_char,
self.separate_selectors, self.end_with_newline)


Expand Down Expand Up @@ -91,7 +91,7 @@ def __init__(self, indent_char, indent_size, default_indent=""):

def __lastCharWhitespace(self):
return WHITE_RE.search(self.output[len(self.output) - 1]) is not None

def indent(self):
self.indentString += self.singleIndent

Expand Down Expand Up @@ -129,7 +129,7 @@ def newLine(self, keepWhitespace=False):

if len(self.output) > 0:
self.output.append("\n")

if len(self.indentString) > 0:
self.output.append(self.indentString)

Expand Down Expand Up @@ -189,19 +189,28 @@ def skipWhitespace(self):
pass
return self.pos != start + 1

def eatComment(self):
def eatComment(self, singleLine):
start = self.pos
self.next()
while self.next():
if self.ch == "*" and self.peek() == "/":
self.pos = self.pos + 1
break
elif singleLine and self.ch == "\n":
break
return self.source_text[start:self.pos + 1]

def lookBack(self, string):
past = self.source_text[self.pos - len(string):self.pos]
return past.lower() == string

def isCommentOnLine(self):
endOfLine = self.source_text.find('\n', self.pos)
if endOfLine == -1:
return False;
restOfLine = self.source_text[self.pos:endOfLine]
return restOfLine.find('//') != -1

def beautify(self):
m = re.search("^[\r\n]*[\t ]*", self.source_text)
indentString = m.group(0)
Expand All @@ -214,11 +223,14 @@ def beautify(self):
if not self.ch:
break
elif self.ch == '/' and self.peek() == '*':
comment = self.eatComment()
comment = self.eatComment(False)
printer.comment(comment)
header = self.lookBack("")
if header:
printer.push("\n\n")
elif self.ch == '/' and self.peek() == '/':
printer.comment(self.eatComment(True)[0:-1])
printer.newLine()
elif self.ch == '{':
self.eatWhitespace()
if self.peek() == '}':
Expand All @@ -238,7 +250,14 @@ def beautify(self):
elif self.ch == '"' or self.ch == '\'':
printer.push(self.eatString(self.ch))
elif self.ch == ';':
printer.semicolon()
if self.isCommentOnLine():
beforeComment = self.eatString('/')
comment = self.eatComment(True)
printer.push(beforeComment)
printer.push(comment[1:-1])
printer.newLine()
else:
printer.semicolon()
elif self.ch == '(':
# may be a url
if self.lookBack("url"):
Expand Down
9 changes: 9 additions & 0 deletions python/cssbeautifier/tests/test.py
Expand Up @@ -32,6 +32,15 @@ def testComments(self):
t(".tabs{/* test */}", ".tabs {\n\t/* test */\n}\n")
t("/* header */.tabs {}", "/* header */\n\n.tabs {}\n")

#single line comment support (less/sass)
t(".tabs{\n// comment\nwidth:10px;\n}", ".tabs {\n\t// comment\n\twidth: 10px;\n}\n")
t(".tabs{// comment\nwidth:10px;\n}", ".tabs {\n\t// comment\n\twidth: 10px;\n}\n")
t("//comment\n.tabs{width:10px;}", "//comment\n.tabs {\n\twidth: 10px;\n}\n")
t(".tabs{//comment\n//2nd single line comment\nwidth:10px;}", ".tabs {\n\t//comment\n\t//2nd single line comment\n\twidth: 10px;\n}\n")
t(".tabs{width:10px;//end of line comment\n}", ".tabs {\n\twidth: 10px;//end of line comment\n}\n")
t(".tabs{width:10px;//end of line comment\nheight:10px;}", ".tabs {\n\twidth: 10px;//end of line comment\n\theight: 10px;\n}\n")
t(".tabs{width:10px;//end of line comment\nheight:10px;//another\n}", ".tabs {\n\twidth: 10px;//end of line comment\n\theight: 10px;//another\n}\n")


def testSeperateSelectors(self):
self.resetOptions()
Expand Down

0 comments on commit 564b84a

Please sign in to comment.