minify – github.com/tdewolff/minify Index | Files | Directories

package minify

import "github.com/tdewolff/minify"

Package minify relates MIME type to minifiers. Several minifiers are provided in the subpackages.

Index

Variables

var Epsilon = 0.00001

Epsilon is the closest number to zero that is not considered to be zero.

var ErrNotExist = errors.New("minify function does not exist for mediatype")

ErrNotExist is returned when no minifier exists for a given mediatype.

Functions

func Bytes

func Bytes(m Minifier, mediatype string, v []byte) ([]byte, error)

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 mediatype exists (ErrNotExist) or any error occurred in the minifier function.

func ContentType

func ContentType(b []byte) []byte

ContentType minifies a given mediatype by removing all whitespace.

func DataURI

func DataURI(m Minifier, dataURI []byte) []byte

DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.

func Number

func Number(num []byte) []byte

Number minifies a given byte slice containing a number (see parse.Number) and removes superfluous characters.

func String

func String(m Minifier, mediatype string, v string) (string, error)

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 mediatype exists (ErrNotExist) or any error occurred in the minifier function.

Types

type Func

type Func func(m Minifier, mediatype string, w io.Writer, r io.Reader) error

Func is the function interface for minifiers. The Minifier parameter is used for embedded resources, such as JS within HTML. The mediatype string is for wildcard minifiers so they know what they minify and for parameter passing (charset for example).

type Minifier

type Minifier interface {
	Minify(mediatype string, w io.Writer, r io.Reader) error
}

Minifier is the interface which all minifier functions accept as first parameter. It's used to extract parameter values of the mediatype and to recursively call other minifier functions.

type Minify

type Minify struct {
	// contains filtered or unexported fields
}

Minify holds a map of mediatype => function to allow recursive minifier calls of the minifier functions.

func New

func New() *Minify

New returns a new Minify.

func (*Minify) AddCmd

func (m *Minify) AddCmd(mediatype string, cmd *exec.Cmd)

AddCmd adds a minify function to the mediatype => 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 mediatype. Be aware that running external tools will slow down minification a lot!

func (*Minify) AddCmdRegexp

func (m *Minify) AddCmdRegexp(mediatype *regexp.Regexp, cmd *exec.Cmd)

AddCmdRegexp adds a minify function to the mediatype => 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 mediatype regular expression. Be aware that running external tools will slow down minification a lot!

func (*Minify) AddFunc

func (m *Minify) AddFunc(mediatype string, minifyFunc Func)

AddFunc adds a minify function to the mediatype => function map (unsafe for concurrent use). It allows one to implement a custom minifier for a specific mediatype.

func (*Minify) AddFuncRegexp

func (m *Minify) AddFuncRegexp(mediatype *regexp.Regexp, minifyFunc Func)

AddFuncRegexp adds a minify function to the mediatype => function map (unsafe for concurrent use). It allows one to implement a custom minifier for a specific mediatype regular expression.

func (Minify) Minify

func (m Minify) Minify(mediatype string, w io.Writer, r io.Reader) error

Minify minifies the content of a Reader and writes it to a Writer (safe for concurrent use). An error is returned when no such mediatype 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'.

Source Files

common.go minify.go

Directories

PathSynopsis
cmd
cmd/minify
cssPackage css minifies CSS3 following the specifications at http://www.w3.org/TR/css-syntax-3/.
htmlPackage html minifies HTML5 following the specifications at http://www.w3.org/TR/html5/syntax.html.
jsPackage js minifies ECMAScript5.1 following the specifications at http://www.ecma-international.org/ecma-262/5.1/.
jsonPackage json minifies JSON following the specifications at http://json.org/.
svgPackage svg minifies SVG1.1 following the specifications at http://www.w3.org/TR/SVG11/.
xmlPackage xml minifies XML1.0 following the specifications at http://www.w3.org/TR/xml/.
Version
v1.1.0 (latest)
Published
Nov 2, 2015
Platform
linux/amd64
Imports
9 packages
Last checked
5 hours ago

Tools for package owners.