• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

css/H03-May-2022-

inliner/H03-May-2022-

parser/H03-May-2022-

.gitignoreH A D27-Aug-201517

.travis.ymlH A D27-Aug-201578

CHANGELOG.mdH A D27-Aug-2015287

LICENSEH A D27-Aug-20151.1 KiB

README.mdH A D27-Aug-20153.2 KiB

douceur.goH A D27-Aug-20151.5 KiB

README.md

1# douceur [![Build Status](https://secure.travis-ci.org/aymerick/douceur.svg?branch=master)](http://travis-ci.org/aymerick/douceur)
2
3A simple CSS parser and inliner in Golang.
4
5![Douceur Logo](https://github.com/aymerick/douceur/blob/master/douceur.png?raw=true "Douceur")
6
7Parser 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).
8
9Inliner only parses CSS defined in HTML document, it *DOES NOT* fetch external stylesheets (for now).
10
11Inliner inserts additional attributes when possible, for example:
12
13```html
14<html>
15  <head>
16  <style type="text/css">
17    body {
18      background-color: #f2f2f2;
19    }
20  </style>
21  </head>
22  <body>
23    <p>Inline me !</p>
24  </body>
25</html>`
26```
27
28Becomes:
29
30```html
31<html>
32  <head>
33  </head>
34  <body style="background-color: #f2f2f2;" bgcolor="#f2f2f2">
35    <p>Inline me !</p>
36  </body>
37</html>`
38```
39
40The `bgcolor` attribute is inserted, in addition to the inlined `background-color` style.
41
42
43## Tool usage
44
45Install tool:
46
47    $ go install github.com/aymerick/douceur
48
49Parse a CSS file and display result:
50
51    $ douceur parse inputfile.css
52
53Inline CSS in an HTML document and display result:
54
55    $ douceur inline inputfile.html
56
57
58## Library usage
59
60Fetch package:
61
62    $ go get github.com/aymerick/douceur
63
64
65### Parse CSS
66
67```go
68package main
69
70import (
71    "fmt"
72
73    "github.com/aymerick/douceur/parser"
74)
75
76func main() {
77    input := `body {
78    /* D4rK s1T3 */
79    background-color: black;
80        }
81
82  p     {
83    /* Try to read that ! HAHA! */
84    color: red; /* L O L */
85 }
86`
87
88    stylesheet, err := parser.Parse(input)
89    if err != nil {
90        panic("Please fill a bug :)")
91    }
92
93    fmt.Print(stylesheet.String())
94}
95```
96
97Displays:
98
99```css
100body {
101  background-color: black;
102}
103p {
104  color: red;
105}
106```
107
108
109### Inline HTML
110
111```go
112package main
113
114import (
115    "fmt"
116
117    "github.com/aymerick/douceur/inliner"
118)
119
120func main() {
121    input := `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
122<html xmlns="http://www.w3.org/1999/xhtml">
123  <head>
124<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
125<style type="text/css">
126  p {
127    font-family: 'Helvetica Neue', Verdana, sans-serif;
128    color: #eee;
129  }
130</style>
131  </head>
132  <body>
133    <p>
134      Inline me please!
135    </p>
136</body>
137</html>`
138
139    html, err := inliner.Inline(input)
140    if err != nil {
141        panic("Please fill a bug :)")
142    }
143
144    fmt.Print(html)
145}
146```
147
148Displays:
149
150```css
151<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>
152<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
153
154  </head>
155  <body>
156    <p style="color: #eee; font-family: &#39;Helvetica Neue&#39;, Verdana, sans-serif;">
157      Inline me please!
158    </p>
159
160</body></html>
161```
162
163## Test
164
165    go test ./... -v
166
167
168## Dependencies
169
170  - Parser uses [Gorilla CSS3 tokenizer](https://github.com/gorilla/css).
171  - Inliner uses [goquery](github.com/PuerkitoBio/goquery) to manipulate HTML.
172
173
174## Similar projects
175
176  - [premailer](https://github.com/premailer/premailer)
177  - [roadie](https://github.com/Mange/roadie)
178