-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_minify.go
72 lines (57 loc) · 2.4 KB
/
html_minify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package html // import "aahframe.work/minify/html"
import (
"io"
"regexp"
"aahframe.work"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
"github.com/tdewolff/minify/js"
)
const htmlContentType = "text/html"
const cssContentType = "text/css"
var m *minify.M
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Global methods
//___________________________________
// HTML method calls the minify library func to minify content. It reads data
// from reader and writes it to the given writer.
func HTML(contentType string, w io.Writer, r io.Reader) error {
return m.Minify(htmlContentType, w, r)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported methods
//___________________________________
// AddHTMLMinifer method is aah's `OnStart` event func to initilize
// the minify instance and adding minifier into application.
func AddHTMLMinifer(e *aah.Event) {
m = minify.New()
app := aah.App()
cfg := app.Config()
m.Add(htmlContentType, &html.Minifier{
KeepConditionalComments: cfg.BoolDefault("render.minify.html.keep.conditional_comments", true),
KeepDocumentTags: cfg.BoolDefault("render.minify.html.keep.document_tags", true),
KeepWhitespace: cfg.BoolDefault("render.minify.html.keep.whitespace", false),
KeepDefaultAttrVals: cfg.BoolDefault("render.minify.html.keep.default_attr_vals", false),
KeepEndTags: cfg.BoolDefault("render.minify.html.keep.end_tags", false),
})
if !cfg.BoolDefault("render.minify.html.keep.inline_javascript_asis", false) {
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
}
if !cfg.BoolDefault("render.minify.html.keep.inline_css_asis", false) {
m.Add(cssContentType, &css.Minifier{
// number of decimals to preserve for numbers
Decimals: cfg.IntDefault("render.minify.html.css.decimals", -1),
// prohibits using CSS3 syntax (such as exponents in numbers, or rgba( → rgb(
KeepCSS2: cfg.BoolDefault("render.minify.html.keep.css2", false),
})
}
// set `HTML` minify func.
app.SetMinifier(HTML)
}
func init() {
aah.App().OnInit(AddHTMLMinifer)
}