Documentation
¶
Overview ¶
Package minify relates MIME type to minifiers. Several minifiers are provided in the subpackages.
Index ¶
- Constants
- Variables
- func DataURI(m *M, dataURI []byte) []byte
- func Decimal(num []byte, prec int) []byte
- func Mediatype(b []byte) []byte
- func Number(num []byte, prec int) []byte
- type M
- func (m *M) Add(mimetype string, minifier Minifier)
- func (m *M) AddCmd(mimetype string, cmd *exec.Cmd)
- func (m *M) AddCmdRegexp(pattern *regexp.Regexp, cmd *exec.Cmd)
- func (m *M) AddFunc(mimetype string, minifier MinifierFunc)
- func (m *M) AddFuncRegexp(pattern *regexp.Regexp, minifier MinifierFunc)
- func (m *M) AddRegexp(pattern *regexp.Regexp, minifier Minifier)
- func (m *M) Bytes(mediatype string, v []byte) ([]byte, error)
- func (m *M) Match(mediatype string) (string, map[string]string, MinifierFunc)
- func (m *M) Middleware(next http.Handler) http.Handler
- func (m *M) Minify(mediatype string, w io.Writer, r io.Reader) error
- func (m *M) MinifyMimetype(mimetype []byte, w io.Writer, r io.Reader, params map[string]string) error
- func (m *M) Reader(mediatype string, r io.Reader) io.Reader
- func (m *M) ResponseWriter(w http.ResponseWriter, r *http.Request) *minifyResponseWriter
- func (m *M) String(mediatype string, v string) (string, error)
- func (m *M) Writer(mediatype string, w io.Writer) *minifyWriter
- type Minifier
- type MinifierFunc
Examples ¶
Constants ¶
const MaxInt = int(^uint(0) >> 1)
const MinInt = -MaxInt - 1
Variables ¶
var Epsilon = 0.00001
Epsilon is the closest number to zero that is not considered to be zero.
var ErrNotExist = errors.New("minifier does not exist for mimetype")
ErrNotExist is returned when no minifier exists for a given mimetype.
Functions ¶
func DataURI ¶
DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.
func Decimal ¶
Decimal minifies a given byte slice containing a number (see parse.Number) and removes superfluous characters. It does not parse or output exponents.
Types ¶
type M ¶
M holds a map of mimetype => function to allow recursive minifier calls of the minifier functions.
func (*M) AddCmd ¶
AddCmd adds a minify function to the mimetype => function map (unsafe for concurrent use) that executes a command to process the minification. It allows the use of external tools like ClosureCompiler, UglifyCSS, etc. for a specific mimetype.
func (*M) AddCmdRegexp ¶
AddCmdRegexp adds a minify function to the mimetype => function map (unsafe for concurrent use) that executes a command to process the minification. It allows the use of external tools like ClosureCompiler, UglifyCSS, etc. for a specific mimetype regular expression.
func (*M) AddFunc ¶
func (m *M) AddFunc(mimetype string, minifier MinifierFunc)
AddFunc adds a minify function to the mimetype => function map (unsafe for concurrent use).
func (*M) AddFuncRegexp ¶
func (m *M) AddFuncRegexp(pattern *regexp.Regexp, minifier MinifierFunc)
AddFuncRegexp adds a minify function to the mimetype => function map (unsafe for concurrent use).
func (*M) AddRegexp ¶
AddRegexp adds a minifier to the mimetype => function map (unsafe for concurrent use).
func (*M) Bytes ¶
Bytes minifies an array of bytes (safe for concurrent use). When an error occurs it return the original array and the error. It returns an error when no such mimetype exists (ErrNotExist) or any error occurred in the minifier function.
func (*M) Match ¶
Match returns the pattern and minifier that gets matched with the mediatype. It returns nil when no matching minifier exists. It has the same matching algorithm as Minify.
func (*M) Middleware ¶
Middleware provides a middleware function that minifies content on the fly by intercepting writes to http.ResponseWriter. http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ... Minification might be slower than just sending the original file! Caching is advised.
func (*M) Minify ¶
Minify minifies the content of a Reader and writes it to a Writer (safe for concurrent use). An error is returned when no such mimetype exists (ErrNotExist) or when an error occurred in the minifier function. Mediatype may take the form of 'text/plain', 'text/*', '*/*' or 'text/plain; charset=UTF-8; version=2.0'.
Example (Custom) ¶
m := New()
m.AddFunc("text/plain", func(m *M, w io.Writer, r io.Reader, _ map[string]string) error {
// remove all newlines and spaces
rb := bufio.NewReader(r)
for {
line, err := rb.ReadString('\n')
if err != nil && err != io.EOF {
return err
}
if _, errws := io.WriteString(w, strings.Replace(line, " ", "", -1)); errws != nil {
return errws
}
if err == io.EOF {
break
}
}
return nil
})
in := "Because my coffee was too cold, I heated it in the microwave."
out, err := m.String("text/plain", in)
if err != nil {
panic(err)
}
fmt.Println(out)
Output: Becausemycoffeewastoocold,Iheateditinthemicrowave.
func (*M) MinifyMimetype ¶
func (m *M) MinifyMimetype(mimetype []byte, w io.Writer, r io.Reader, params map[string]string) error
MinifyMimetype minifies the content of a Reader and writes it to a Writer (safe for concurrent use). It is a lower level version of Minify and requires the mediatype to be split up into mimetype and parameters. It is mostly used internally by minifiers because it is faster (no need to convert a byte-slice to string and vice versa).
func (*M) Reader ¶
Reader wraps a Reader interface and minifies the stream. Errors from the minifier are returned by the reader.
Example ¶
b := bytes.NewReader([]byte("input"))
m := New()
// add minfiers
r := m.Reader("mime/type", b)
if _, err := io.Copy(os.Stdout, r); err != nil {
if _, err := io.Copy(os.Stdout, b); err != nil {
panic(err)
}
}
func (*M) ResponseWriter ¶
func (m *M) ResponseWriter(w http.ResponseWriter, r *http.Request) *minifyResponseWriter
ResponseWriter minifies any writes to the http.ResponseWriter. http.ResponseWriter loses all functionality such as Pusher, Hijacker, Flusher, ... Minification might be slower than just sending the original file! Caching is advised.
func (*M) String ¶
String minifies a string (safe for concurrent use). When an error occurs it return the original string and the error. It returns an error when no such mimetype exists (ErrNotExist) or any error occurred in the minifier function.
func (*M) Writer ¶
Writer wraps a Writer interface and minifies the stream. Errors from the minifier are returned by Close on the writer. The writer must be closed explicitly.
Example ¶
m := New()
// add minfiers
w := m.Writer("mime/type", os.Stdout)
if _, err := w.Write([]byte("input")); err != nil {
panic(err)
}
if err := w.Close(); err != nil {
panic(err)
}
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
minify
command
|
|
|
Package css minifies CSS3 following the specifications at http://www.w3.org/TR/css-syntax-3/.
|
Package css minifies CSS3 following the specifications at http://www.w3.org/TR/css-syntax-3/. |
|
Package html minifies HTML5 following the specifications at http://www.w3.org/TR/html5/syntax.html.
|
Package html minifies HTML5 following the specifications at http://www.w3.org/TR/html5/syntax.html. |
|
Package js minifies ECMAScript5.1 following the specifications at http://www.ecma-international.org/ecma-262/5.1/.
|
Package js minifies ECMAScript5.1 following the specifications at http://www.ecma-international.org/ecma-262/5.1/. |
|
Package json minifies JSON following the specifications at http://json.org/.
|
Package json minifies JSON following the specifications at http://json.org/. |
|
Package parse contains a collection of parsers for various formats in its subpackages.
|
Package parse contains a collection of parsers for various formats in its subpackages. |
|
buffer
Package buffer contains buffer and wrapper types for byte slices.
|
Package buffer contains buffer and wrapper types for byte slices. |
|
css
Package css is a CSS3 lexer and parser following the specifications at http://www.w3.org/TR/css-syntax-3/.
|
Package css is a CSS3 lexer and parser following the specifications at http://www.w3.org/TR/css-syntax-3/. |
|
html
Package html is an HTML5 lexer following the specifications at http://www.w3.org/TR/html5/syntax.html.
|
Package html is an HTML5 lexer following the specifications at http://www.w3.org/TR/html5/syntax.html. |
|
js
Package js is an ECMAScript5.1 lexer following the specifications at http://www.ecma-international.org/ecma-262/5.1/.
|
Package js is an ECMAScript5.1 lexer following the specifications at http://www.ecma-international.org/ecma-262/5.1/. |
|
json
Package json is a JSON parser following the specifications at http://json.org/.
|
Package json is a JSON parser following the specifications at http://json.org/. |
|
xml
Package xml is an XML1.0 lexer following the specifications at http://www.w3.org/TR/xml/.
|
Package xml is an XML1.0 lexer following the specifications at http://www.w3.org/TR/xml/. |
|
Package pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
|
Package pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags. |
|
Package svg minifies SVG1.1 following the specifications at http://www.w3.org/TR/SVG11/.
|
Package svg minifies SVG1.1 following the specifications at http://www.w3.org/TR/SVG11/. |
|
Package try provides retry functionality.
|
Package try provides retry functionality. |
|
is
Package is is a mini testing helper.
|
Package is is a mini testing helper. |
|
Package xml minifies XML1.0 following the specifications at http://www.w3.org/TR/xml/.
|
Package xml minifies XML1.0 following the specifications at http://www.w3.org/TR/xml/. |