package minify
import "github.com/tdewolff/minify"
Package minify relates MIME type to minifiers. Several minifiers are provided in the subpackages.
Index ¶
- Variables
- func ContentType(b []byte) []byte
- func DataURI(m *M, dataURI []byte) []byte
- func Number(num []byte) []byte
- type M
- func New() *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) 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) String(mediatype string, v string) (string, error)
- func (m *M) Writer(mediatype string, w io.Writer) io.WriteCloser
- type Minifier
Examples ¶
Variables ¶
var Epsilon = 0.00001
Epsilon is the closest number to zero that is not considered to be zero.
ErrNotExist is returned when no minifier exists for a given mimetype.
Functions ¶
func ContentType ¶
ContentType minifies a given mediatype by removing all whitespace.
func DataURI ¶
DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.
func Number ¶
Number minifies a given byte slice containing a number (see parse.Number) and removes superfluous characters.
Types ¶
type M ¶
M holds a map of mimetype => function to allow recursive minifier calls of the minifier functions.
func New ¶
func New() *M
New returns a new M.
func (*M) Add ¶
Add adds a minifier to the mimetype => function map (unsafe for concurrent use).
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 ¶
AddFunc adds a minify function to the mimetype => function map (unsafe for concurrent use).
func (*M) AddFuncRegexp ¶
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) 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'.
Code:
Output: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.
}
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.
Code:
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) 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 the writer.
The writer must be closed explicitly.
Code:
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)
}
}
type Minifier ¶
Minifier is the interface for minifiers. The *M parameter is used for minifying embedded resources, such as JS within HTML.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd | |
cmd/minify | |
css | Package css minifies CSS3 following the specifications at http://www.w3.org/TR/css-syntax-3/. |
html | Package html minifies HTML5 following the specifications at http://www.w3.org/TR/html5/syntax.html. |
js | Package js minifies ECMAScript5.1 following the specifications at http://www.ecma-international.org/ecma-262/5.1/. |
json | Package json minifies JSON following the specifications at http://json.org/. |
svg | Package svg minifies SVG1.1 following the specifications at http://www.w3.org/TR/SVG11/. |
xml | Package xml minifies XML1.0 following the specifications at http://www.w3.org/TR/xml/. |
- Version
- v2.0.0+incompatible
- Published
- Apr 21, 2016
- Platform
- darwin/amd64
- Imports
- 11 packages
- Last checked
- now –
Tools for package owners.