Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aymerick/douceur

A simple CSS parser and inliner in Go
https://github.com/aymerick/douceur

css-parser go

Last synced: about 18 hours ago
JSON representation

A simple CSS parser and inliner in Go

Awesome Lists containing this project

README

        

# douceur [![Build Status](https://secure.travis-ci.org/aymerick/douceur.svg?branch=master)](http://travis-ci.org/aymerick/douceur)

A simple CSS parser and inliner in Golang.

![Douceur Logo](https://github.com/aymerick/douceur/blob/master/douceur.png?raw=true "Douceur")

Parser is vaguely inspired by [CSS Syntax Module Level 3](http://www.w3.org/TR/css3-syntax) and [corresponding JS parser](https://github.com/tabatkins/parse-css).

Inliner only parses CSS defined in HTML document, it *DOES NOT* fetch external stylesheets (for now).

Inliner inserts additional attributes when possible, for example:

```html



body {
background-color: #f2f2f2;
}



Inline me !



`
```

Becomes:

```html




Inline me !



`
```

The `bgcolor` attribute is inserted, in addition to the inlined `background-color` style.

## Tool usage

Install tool:

$ go install github.com/aymerick/douceur

Parse a CSS file and display result:

$ douceur parse inputfile.css

Inline CSS in an HTML document and display result:

$ douceur inline inputfile.html

## Library usage

Fetch package:

$ go get github.com/aymerick/douceur

### Parse CSS

```go
package main

import (
"fmt"

"github.com/aymerick/douceur/parser"
)

func main() {
input := `body {
/* D4rK s1T3 */
background-color: black;
}

p {
/* Try to read that ! HAHA! */
color: red; /* L O L */
}
`

stylesheet, err := parser.Parse(input)
if err != nil {
panic("Please fill a bug :)")
}

fmt.Print(stylesheet.String())
}
```

Displays:

```css
body {
background-color: black;
}
p {
color: red;
}
```

### Inline HTML

```go
package main

import (
"fmt"

"github.com/aymerick/douceur/inliner"
)

func main() {
input := `

p {
font-family: 'Helvetica Neue', Verdana, sans-serif;
color: #eee;
}




Inline me please!

`

html, err := inliner.Inline(input)
if err != nil {
panic("Please fill a bug :)")
}

fmt.Print(html)
}
```

Displays:

```css




Inline me please!

```

## Test

go test ./... -v

## Dependencies

- Parser uses [Gorilla CSS3 tokenizer](https://github.com/gorilla/css).
- Inliner uses [goquery](github.com/PuerkitoBio/goquery) to manipulate HTML.

## Similar projects

- [premailer](https://github.com/premailer/premailer)
- [roadie](https://github.com/Mange/roadie)