Skip to content

Commit

Permalink
now supports additional blank lines
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikJoreteg committed Jan 5, 2010
1 parent 2693dd1 commit a8186e9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 49 deletions.
6 changes: 4 additions & 2 deletions includes/css/test.sass
Expand Up @@ -13,9 +13,11 @@ h1
font-size: 26px
text-align: right

// test
//comment

.hello
stuff: 23px
stuff: 23px //comment

&:hover, &:active
background-color: #FFF
Expand Down
100 changes: 56 additions & 44 deletions model/sass.cfc
Expand Up @@ -6,7 +6,6 @@
function sass2css(file){
var result = '';
var line_result = '';
var unclosed = 0;
var indent = 0;
var prev_indent = 0;
var selector_array = arrayNew(1);
Expand All @@ -20,12 +19,32 @@
var delim = "#chr(10)#";
var j = 0;
var ignore = false;

var clean_file = '';
var previous_type = 0;
var unclosed = false;

// first run, cleans out comments and sets variable definitions
for(j=1;j lte listLen(file,delim);j=j+1){
x = rtrim(listGetAt(file,j,delim));
x = listGetAt(file,j,delim);
ignore = false;
if(isAssignment(x)){
instance.definitions[removeChars(trim(listGetAt(x,1,"=")),1,1)] = getDefinitionVarValue(x);
ignore = true;
}
// clean out comments
if(REFind("^//",trim(x))){
ignore = true;
}
x = REReplace(x,"//.+$","","one");
if(not ignore){
clean_file = clean_file & x & delim;
}
}

for(j=1;j lte listLen(clean_file,delim);j=j+1){
x = rtrim(listGetAt(clean_file,j,delim));

// this is our ignore flag
// these are our processing flags
ignore = false;

// if it's validly indented
Expand All @@ -46,7 +65,9 @@

switch(whatIsIt(x)){
case "selector":{
// clear our working array
ArrayClear(partial_selector_array);
// loop through selectors in this line, in case there are multiple
for(i=1; i lte listLen(x,','); i=i+1){
current_selector = trim(listGetAt(x,i));

Expand All @@ -61,17 +82,16 @@

// tack on the opening bracket and spacing count it
line_result = RepeatString(" ", (position-1)*2) & ArrayToList(partial_selector_array, ', ') & "{";
unclosed = unclosed + 1;
break;
}
case "isVar":{
// this is a variable definition record it
instance.definitions[removeChars(trim(listGetAt(x,1,"=")),1,1)] = getDefinitionVarValue(x);
break;
}
case "quietComment":{
// do nothing we just want to ignore this comment
ignore = true;

if(unclosed){
result = result & "}#delim#";
unclosed = false;
}

// set our unclosed flag
unclosed = true;

result = result & "#line_result##delim#";
break;
}
case "cssRule":{
Expand All @@ -80,28 +100,19 @@
x = insertVariablesInRule(x);
}
// this is a css rule, append semi-colon
line_result = x & ";";
result = result & "#x#;#delim#";

break;
}
case "blank":{
if(unclosed gt 0){
line_result = "}";
unclosed = unclosed - 1;
}
break;
}
}

// add newline character and append to result
if(not ignore){
result = result & "#line_result##delim#";
}
}

// there may be unclosed brackets at the very end, we need to close those up
while(unclosed gt 0){
if(unclosed){
result = result & "}";
unclosed = unclosed - 1;
}

return result;
Expand All @@ -118,7 +129,6 @@
}

function cssRuleContainsVar(string){
// left off here
if(REFind("=( +)?!\S", arguments.string)){
return true;
}
Expand Down Expand Up @@ -240,28 +250,30 @@

// tries to figure out what the line is
function whatIsIt(line){
if(not len(trim(arguments.line))){
if(isBlank(arguments.line)){
return "blank";
}
if(isSelector(arguments.line)){
return "selector";
}
if(isQuietComment(arguments.line)){
return "quietComment";
}
if(isVarDefinition(arguments.line)){
return "assignment";
}
return "cssRule";
}

// blank test
function isBlank(line){
if(not len(trim(arguments.line))){
return true;
}
return false;
}

// selector test
function isSelector(line){
var i = 1;
var result = false;

for(i = 1; i lte listLen(arguments.line, ','); i=i+1){
if(REFind("^([##\.a-zA-Z_-]|(&:)|(&\.)|(&##))([ ""'>=0-9a-zA-Z_\[\]\*\.\$\)\(\-]+)?(?!:)([ 0-9a-zA-Z_-]+)?$", trim(listGetAt(arguments.line,i)))){
if(REFind("^([##\.a-zA-Z_-]|(&:)|(&>)|(&\.)|(&##))([ ""'>=0-9a-zA-Z_\[\]\*\.\$\)\(\-]+)?(?!:)([ 0-9a-zA-Z_-]+)?$", trim(listGetAt(arguments.line,i)))){
result = true;
}
else{
Expand All @@ -274,23 +286,19 @@
}

// variable definition test
function isVarDefinition(line){
function isAssignment(line){
if(REFind("^![0-9a-zA-Z_-]+( +)?=( +)?\S", trim(arguments.line))){
return true;
}
else{
return false;
}
return false;
}

// quiet comment test
function isQuietComment(line){
if(REFind("^//", trim(arguments.line))){
return true;
}
else{
return false;
}
return false;
}
</cfscript>

Expand Down Expand Up @@ -363,11 +371,15 @@
<cffile action="write" output="#css#" file="#arguments.directoryPath#\#getFileName(arguments.SASSFile)#.css">
</cffunction>


<!--- dump --->
<cffunction name="dump" access="public" returntype="any" output="false">
<cfargument name="myvar">
<cfargument name="abort" default="false">
<cfdump var="#arguments.myvar#"><cfif arguments.abort><cfabort></cfif>
</cffunction>

<!--- abort --->
<cffunction name="abort" access="public" returntype="any" output="false">
<cfabort>
</cffunction>
</cfcomponent>
6 changes: 3 additions & 3 deletions test/sassTest.cfc
Expand Up @@ -108,7 +108,7 @@ component extends="coldbox.system.testing.BasePluginTest" {

}

function test_isVar(){
function test_isAssignment(){
var i = 1;
var j = 1;
var valid_vars = [
Expand All @@ -126,13 +126,13 @@ component extends="coldbox.system.testing.BasePluginTest" {
// test the valid ones
for(i = 1; i LTE arrayLen(valid_vars); i = i + 1){
debug(valid_vars[i]);
assertTrue(sass.isVar(valid_vars[i]));
assertTrue(sass.isAssignment(valid_vars[i]));
}

// test the invalid ones
for(j = 1; j LTE arrayLen(invalid_vars); j = j + 1){
debug(invalid_vars[j]);
assertFalse(sass.isVar(invalid_vars[j]));
assertFalse(sass.isAssignment(invalid_vars[j]));
}
}

Expand Down

0 comments on commit a8186e9

Please sign in to comment.