Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/aymerick/douceur
- Owner: aymerick
- License: mit
- Created: 2015-04-09T10:21:26.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-09-11T15:20:35.000Z (over 2 years ago)
- Last Synced: 2024-07-31T20:51:16.310Z (5 months ago)
- Topics: css-parser, go
- Language: Go
- Homepage:
- Size: 44.9 KB
- Stars: 243
- Watchers: 5
- Forks: 43
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - douceur - CSS inliner for your HTML emails. (Email / Search and Analytic Databases)
- awesome-go - douceur - A simple CSS parser and inliner in Go - ★ 135 (Email)
- awesome-go-extra - douceur - 04-09T10:21:26Z|2021-06-05T19:55:34Z| (Email / Advanced Console UIs)
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 mainimport (
"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 mainimport (
"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)