Scripts

Simpele css parser

Dit is een hele simpele css parser. Ik ben er zelf mee bezig om iets anders te maken, maar ik dacht hem hier maar te droppen voor het geval mensen het wouden gebruiken voor iets anders. De volledige set classes staat in het script, samen met het voorbeeld erbij. voorbeeld gebruik: OUTPUT:

cssBeautifier.php
<pre>
<?php    
class Solow_Css_Property
{
    protected $property;
    protected $value;
    
    public function __construct($property, $value)
    {
        $this->property = $property;
        $this->value = $value;
    }
    
    public function parseProperty()
    {
        return $this->property.": ".$this->value.";";
    }
}

class Solow_Css_DeclarationGroup
{
    protected $properties = array();  
    
    public function __construct($selector, $declarations)
    {
        $this->setProperties($selector, $declarations);   
    }
    
    protected function setProperties($selector, $declarations)
    {        
        $properties = $this->getCleanedProperties($declarations);             
        foreach($properties as $property)
        {
            $propertyValue = explode(':',$property, 2);
            $this->setProperty(trim($propertyValue[0]),  trim($propertyValue[1]));
        }                                                 
    }
    
    protected function getCleanedProperties($declarations)
    {
        $properties = array_filter(array_map('trim', explode(";", $declarations)));
        return $properties;
    }
    
    public function setProperty($property, $value)
    {
        $this->properties[$property] = new Solow_Css_Property($property, $value);
    }
    
    public function removeProperty($property)
    {
        unset($this->properties[$property]);
    }
    
    public function getParsedProperties($spacing="    ")
    {
        $temp="";
        foreach($this->properties as $property => $propertyObject)
        {
            $temp.=$spacing.$propertyObject->parseProperty().PHP_EOL;    
        }
        return $temp;
    }
}
          
class Solow_Css_Parser
{                    
    protected $declerationGroups = array();
    protected $variables = array();
    protected $constants = array();      
    protected $_spacing = '    ';
    
    public function __construct($cssString)
    {                               
        $this->extractCss($cssString);
    }   
    
    public function __toString()
    {
        return ''.$this->parseStylesheet();  
    }  
    
    public function parseStylesheet()
    {
        $temp = "";
        foreach($this->declerationGroups as $selector=>$declerationGroup)
        {
            $temp.=$selector.PHP_EOL."{".PHP_EOL;
            $parsedProperties=$declerationGroup->getParsedProperties();                   
            $parsedProperties = preg_replace_callback('/var\(([^)]+)\)/', array($this, "getVariableRegex"), $parsedProperties);
            $parsedProperties = preg_replace_callback('/constant\(([^)]+)\)/', array($this, "getConstantRegex"), $parsedProperties);
            $temp .= substr($parsedProperties, 0, -1);
            $temp.=PHP_EOL."}".PHP_EOL.PHP_EOL;
        }
        return $temp;         
    }
    
    protected function getVariableRegex($match)
    {         
        return $this->variables[$match[1]];   
    }
    
    protected function getConstantRegex($match)
    {         
        return $this->constants[$match[1]];   
    }
    
    protected function extractCss($cssString)
    {
        preg_match_all('#([^{]+?)\{([^}]+?)\}#i', $cssString, $matches);   
        foreach($matches[1] as $key=>$tag)
        {             
            $tag=trim($tag);
            if(strtolower($tag) == "@variables")
            {
                $this->setVariables($tag, $matches[2][$key]);        
            }
            elseif(strtolower($tag) == "@define")
            {
                $this->setConstants($tag, $matches[2][$key]);        
            }  
            else
            {
                $this->setDeclerationGroup($tag, $matches[2][$key]);    
            }         
        }
    }
    
    public function setSpacing($spacing)
    {
        $this->_spacing = $spacing;
    }
    
    public function setDeclerationGroup($tag, $string)
    {
        $this->declerationGroups[$tag] = new Solow_Css_DeclarationGroup($tag, $string);   
    }
    
    public function removeDeclerationGroup($tag)
    {
        unset($this->declerationGroups[$tag]);
    }                    
    
    public function setProperty($declerationGroup, $property, $value)
    {
        $this->declerationGroups[$declerationGroup]->setProperty($property, $value);  
    }
    
    public function removeProperty($declerationGroup, $property)
    {
        $this->declerationGroups[$declerationGroup]->removeProperty($property);  
    }
    
    protected function setVariables($selector, $declarations)
    {                        
        $properties = $this->getCleanedProperties($declarations);
        foreach($properties as $property)
        {
            $propertyValue = explode(':',$property);
            $this->setVariable(trim($propertyValue[0]),  trim($propertyValue[1]));
        }                                                 
    }
    
    protected function setConstants($selector, $declarations)
    {                        
        $properties = $this->getCleanedProperties($declarations);
        foreach($properties as $property)
        {
            $propertyValue = explode(':',$property);
            $this->setConstant(trim($propertyValue[0]),  trim($propertyValue[1]));
        }                                                 
    }
    
    public function setVariable($variable, $value)
    {
        $this->variables[$variable] = $value;
    }
    
    public function unsetVariable($variable)
    {
        unset($this->variables[$variable]);
    }
    
    public function setConstant($constant, $value)
    {
        $this->constants[$constant] = $value;
    }
    
    public function unsetConstant($constant)
    {
        unset($this->constants[$constant]);
    }
    
    protected function getCleanedProperties($declarations)
    {
        $properties = array_filter(array_map('trim', explode(";", $declarations)));
        return $properties;
    }
}    

$test = <<<EOI
@variables
{
        someVar: SomeKey;
        someOtherVar: SomeOtherKey;
        widthGen: 100%;
        image: someCrappyImage.jpg;
}

@Define
{
    someMarginConstant: nothing to do with margin ROFL!;
}
        
#header{             
    width: var(widthGen);
    height: 95px;   
    background-image: url("var(image)");
    background-repeat: no-repeat;
    background-position: left;
    clear: both;
}
        
        #stranglyPlaced
        {
        width: var(widthGen); height: 150px;
        clear: both;
            margin: constant(someMarginConstant);
        }
        
        .oneLiner{width:100%;height:150px;clear:both; margin:0 auto;}
EOI;

$testInstance = new Solow_Css_Parser($test);  
$testInstance->setProperty('.oneLiner', 'non-existant', 'bullcrap');
echo $testInstance;      
?>                                  

OUTPUT:

[code]
#header
{
    width: 100%;
    height: 95px;
    background-image: url("someCrappyImage.jpg");
    background-repeat: no-repeat;
    background-position: left;
    clear: both;
    color: #000;
}

#stranglyPlaced
{
    width: 100%;
    height: 150px;
    clear: both;
    margin: nothing to do with margin ROFL!;
}

.oneLiner
{
    width: 100%;
    height: 150px;
    clear: both;
    margin: 0 auto;
    non-existant: bullcrap;
}
[/code]

Reacties

0
Nog geen reacties.