csslex

package module
v0.0.0-...-7894d8a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2016 License: Apache-2.0 Imports: 3 Imported by: 2

README

csslex

A simple CSS lexer written in Go, without using regexp.

Docs and examples are on godoc: https://godoc.org/github.com/crhym3/csslex

License

(c) Google, 2015. Licensed under Apache-2 license.

Documentation

Overview

Package csslex provides a simple CSS lexer, without using regexp.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lex

func Lex(input string) chan *Item

Lex creates a new lexer and returns channel which will be sent Item tokens. The lexing is started in a goroutine right away, before returing from this method.

Example
const cssText = `
	/* comment **/
	@import url('style.css') print;
	body {
	  background-color: white;
	  color: #222
	}
	div p,
	  #id:first-line {
	    white-space: nowrap; }
	@media print {
	  body { font-size: 10pt }
	}
	.c1{color:red}.c2{color:blue}
	`
style := make(map[string]map[string]string)
var skip bool
var sel []string
for item := range Lex(cssText) {
	switch item.Typ {
	case ItemError:
		log.Fatal(item)
	case ItemAtRuleIdent, ItemAtRule:
		continue
	case ItemAtRuleBlockStart:
		skip = true
		continue
	case ItemAtRuleBlockEnd:
		skip = false
		continue
	case ItemBlockEnd:
		sel = nil
	case ItemSelector:
		if skip {
			continue
		}
		sel = append(sel, item.Val)
		if _, ok := style[item.Val]; !ok {
			style[item.Val] = make(map[string]string)
		}
	case ItemDecl:
		if skip || len(sel) == 0 {
			continue
		}
		decl := strings.SplitN(item.Val, ":", 2)
		decl[0] = strings.TrimSpace(decl[0])
		decl[1] = strings.TrimSpace(decl[1])
		for _, s := range sel {
			style[s][decl[0]] = decl[1]
		}
	}
}
sb, err := json.MarshalIndent(style, "", "  ")
if err != nil {
	log.Fatal(err)
}
fmt.Println(string(sb))
Output:

{
  "#id:first-line": {
    "white-space": "nowrap"
  },
  ".c1": {
    "color": "red"
  },
  ".c2": {
    "color": "blue"
  },
  "body": {
    "background-color": "white",
    "color": "#222"
  },
  "div p": {
    "white-space": "nowrap"
  }
}

Types

type Item

type Item struct {
	Typ ItemType
	Pos int
	Val string
}

Item is an atom of lexing process.

type ItemType

type ItemType int

ItemType specifies type of Item.

const (
	ItemError            ItemType = iota // Parsing error. Lex stops at first error.
	ItemSelector                         // CSS selector.
	ItemDecl                             // CSS declaration in a block.
	ItemBlockStart                       // Beginning of a regular CSS block, not inside At-Rule.
	ItemBlockEnd                         // Ending of a regular CSS block, not inside At-Rule.
	ItemAtRuleIdent                      // At-Rule identifier, including @ symbol.
	ItemAtRule                           // The content of an At-Rule.
	ItemAtRuleBlockStart                 // Beginning of an At-Rule block.
	ItemAtRuleBlockEnd                   // Ending of an At-Rule block.
)

Item types emitted by the lexer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL