Skip to content

m2omou/gocss

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Gocss

Gocss is a simple css parser written in GO, that provides a way to parse css selectors and properties.

Installation

$>go get github.com/m2omou/parser-css/src
import "github.com/m2omou/parser-css/src"

Code examples

####Type:####

The different type of selector, 'All' stand for the '*' css selector

const ( 
	Class = 1
	Id
	All
	Element
	Attribute
)

####Property:####

e.g: "color: white;"

type property struct {
        Name string 		// color
        Value string 		// white
}

####Selector:####

e.g ".container { color: white, float: left }"

type selector struct {
        Name string             // '.container'
        Properties []property   // [ { Name : 'color', Value : 'white' }, { Name : 'float', Value : 'left'} ]
        Type int                // Class
}

fd, err := os.Open("./style.css")
    
if err != nil {
        panic(err)
}
        
css := parserCSS(fd)
for s := range selectors {
        fmt.Println(selectors[s].Name)
        for p := range selectors[s].Properties {
                fmt.Println(selectors[s].Properties[p].Name + selectors[s].Properties[p].Value)
        }
}

For example this css :

body {
    width: 100%;
    height: 100%;
    Background: red;
}

.container {
    margin-bottom: 20px;
    height: 220px;
    width:300px;
    font-size: 14px;
}

Will return the following slice :

[
   {
      "Name":"body",
      "Properties":[
         {
            "Name":"width",
            "Value":"100%"
         },
         {
            "Name":"height",
            "Value":"100%"
         },
         {
            "Name":"Background",
            "Value":"red"
         }
      ],
      "Type":1
   },
   {
      "Name":".container",
      "Properties":[
         {
            "Name":"margin-bottom",
            "Value":"20px"
         },
         {
            "Name":"height",
            "Value":"220px"
         },
         {
            "Name":"width",
            "Value":"300px"
         },
         {
            "Name":"font-size",
            "Value":"14px"
         }
      ],
      "Type":1
   }
]

License

GOCSS Parser Copyright 2014 Mourad Sabour, mourad.sabour[at]gmail.com.

About

Gocss is a CSS parser written in GO

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published