本文整理汇总了Golang中github.com/tdewolff/minify.M类的典型用法代码示例。如果您正苦于以下问题:Golang M类的具体用法?Golang M怎么用?Golang M使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了M类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Minify
// Minify minifies HTML data, it reads from r and writes to w.
func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
var rawTagHash html.Hash
var rawTagTraits traits
var rawTagMediatype []byte
omitSpace := true // if true the next leading space is omitted
defaultScriptType := jsMimeBytes
defaultScriptParams := map[string]string(nil)
defaultStyleType := cssMimeBytes
defaultStyleParams := map[string]string(nil)
defaultInlineStyleParams := map[string]string{"inline": "1"}
attrMinifyBuffer := buffer.NewWriter(make([]byte, 0, 64))
attrByteBuffer := make([]byte, 0, 64)
attrTokenBuffer := make([]*Token, 0, maxAttrLookup)
l := html.NewLexer(r)
tb := NewTokenBuffer(l)
for {
t := *tb.Shift()
SWITCH:
switch t.TokenType {
case html.ErrorToken:
if l.Err() == io.EOF {
return nil
}
return l.Err()
case html.DoctypeToken:
if _, err := w.Write([]byte("<!doctype html>")); err != nil {
return err
}
case html.CommentToken:
// TODO: ensure that nested comments are handled properly (lexer doesn't handle this!)
var comment []byte
if bytes.HasPrefix(t.Data, []byte("[if")) {
comment = append(append([]byte("<!--"), t.Data...), []byte("-->")...)
} else if bytes.HasSuffix(t.Data, []byte("--")) {
// only occurs when mixed up with conditional comments
comment = append(append([]byte("<!"), t.Data...), '>')
}
if _, err := w.Write(comment); err != nil {
return err
}
case html.TextToken:
// CSS and JS minifiers for inline code
if rawTagHash != 0 {
if rawTagHash == html.Style || rawTagHash == html.Script || rawTagHash == html.Iframe || rawTagHash == html.Svg || rawTagHash == html.Math {
var mimetype []byte
var params map[string]string
if rawTagHash == html.Iframe {
mimetype = htmlMimeBytes
} else if rawTagHash == html.Svg {
mimetype = svgMimeBytes
} else if rawTagHash == html.Math {
mimetype = mathMimeBytes
} else if len(rawTagMediatype) > 0 {
mimetype, params = parse.Mediatype(rawTagMediatype)
} else if rawTagHash == html.Script {
mimetype = defaultScriptType
params = defaultScriptParams
} else if rawTagHash == html.Style {
mimetype = defaultStyleType
params = defaultStyleParams
}
// TODO: really necessary?
// ignore CDATA because that only has meaning in XML
if trimmedData := parse.TrimWhitespace(t.Data); len(trimmedData) > 12 && bytes.Equal(trimmedData[:9], cdataBytes) && bytes.Equal(trimmedData[len(trimmedData)-3:], cdataEndBytes) {
t.Data = trimmedData[9 : len(trimmedData)-3]
}
if err := m.MinifyMimetype(mimetype, w, buffer.NewReader(t.Data), params); err != nil {
if _, err := w.Write(t.Data); err != nil {
return err
}
}
} else if _, err := w.Write(t.Data); err != nil {
return err
}
if rawTagTraits&nonPhrasingTag == 0 && rawTagHash != html.Script {
omitSpace = len(t.Data) > 0 && t.Data[len(t.Data)-1] == ' '
}
} else {
t.Data = parse.ReplaceMultipleWhitespace(t.Data)
if !o.KeepWhitespace {
// whitespace removal; trim left
if omitSpace && t.Data[0] == ' ' {
t.Data = t.Data[1:]
}
// whitespace removal; trim right
omitSpace = false
if len(t.Data) == 0 {
omitSpace = true
} else if t.Data[len(t.Data)-1] == ' ' {
omitSpace = true
i := 0
for {
next := tb.Peek(i)
// trim if EOF, text token with leading whitespace or block token
if next.TokenType == html.ErrorToken {
t.Data = t.Data[:len(t.Data)-1]
//.........这里部分代码省略.........
示例2: Minify
// Minify minifies SVG data, it reads from r and writes to w.
func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
var tag svg.Hash
defaultStyleType := cssMimeBytes
defaultStyleParams := map[string]string(nil)
defaultInlineStyleParams := map[string]string{"inline": "1"}
p := NewPathData(o)
minifyBuffer := buffer.NewWriter(make([]byte, 0, 64))
attrByteBuffer := make([]byte, 0, 64)
gStack := make([]bool, 0)
l := xml.NewLexer(r)
tb := NewTokenBuffer(l)
for {
t := *tb.Shift()
SWITCH:
switch t.TokenType {
case xml.ErrorToken:
if l.Err() == io.EOF {
return nil
}
return l.Err()
case xml.DOCTYPEToken:
if len(t.Text) > 0 && t.Text[len(t.Text)-1] == ']' {
if _, err := w.Write(t.Data); err != nil {
return err
}
}
case xml.TextToken:
t.Data = parse.ReplaceMultipleWhitespace(parse.TrimWhitespace(t.Data))
if tag == svg.Style && len(t.Data) > 0 {
if err := m.MinifyMimetype(defaultStyleType, w, buffer.NewReader(t.Data), defaultStyleParams); err != nil {
if err != minify.ErrNotExist {
return err
} else if _, err := w.Write(t.Data); err != nil {
return err
}
}
} else if _, err := w.Write(t.Data); err != nil {
return err
}
case xml.CDATAToken:
if tag == svg.Style {
minifyBuffer.Reset()
if err := m.MinifyMimetype(defaultStyleType, minifyBuffer, buffer.NewReader(t.Text), defaultStyleParams); err == nil {
t.Data = append(t.Data[:9], minifyBuffer.Bytes()...)
t.Text = t.Data[9:]
t.Data = append(t.Data, cdataEndBytes...)
} else if err != minify.ErrNotExist {
return err
}
}
var useText bool
if t.Text, useText = xml.EscapeCDATAVal(&attrByteBuffer, t.Text); useText {
t.Text = parse.ReplaceMultipleWhitespace(parse.TrimWhitespace(t.Text))
if _, err := w.Write(t.Text); err != nil {
return err
}
} else if _, err := w.Write(t.Data); err != nil {
return err
}
case xml.StartTagPIToken:
for {
if t := *tb.Shift(); t.TokenType == xml.StartTagClosePIToken || t.TokenType == xml.ErrorToken {
break
}
}
case xml.StartTagToken:
tag = t.Hash
if containerTagMap[tag] { // skip empty containers
i := 0
for {
next := tb.Peek(i)
i++
if next.TokenType == xml.EndTagToken && next.Hash == tag || next.TokenType == xml.StartTagCloseVoidToken || next.TokenType == xml.ErrorToken {
for j := 0; j < i; j++ {
tb.Shift()
}
break SWITCH
} else if next.TokenType != xml.AttributeToken && next.TokenType != xml.StartTagCloseToken {
break
}
}
if tag == svg.G {
if tb.Peek(0).TokenType == xml.StartTagCloseToken {
gStack = append(gStack, false)
tb.Shift()
break
}
gStack = append(gStack, true)
}
} else if tag == svg.Metadata {
skipTag(tb, tag)
break
} else if tag == svg.Line {
o.shortenLine(tb, &t, p)
} else if tag == svg.Rect && !o.shortenRect(tb, &t, p) {
skipTag(tb, tag)
break
//.........这里部分代码省略.........
示例3: Minify
// Minify minifies SVG data, it reads from r and writes to w.
func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
var tag svg.Hash
defaultStyleType := "text/css"
defaultInlineStyleType := "text/css;inline=1"
attrMinifyBuffer := buffer.NewWriter(make([]byte, 0, 64))
attrByteBuffer := make([]byte, 0, 64)
l := xml.NewLexer(r)
tb := NewTokenBuffer(l)
for {
t := *tb.Shift()
if t.TokenType == xml.CDATAToken {
var useText bool
if t.Data, useText = xml.EscapeCDATAVal(&attrByteBuffer, t.Data); useText {
t.TokenType = xml.TextToken
}
}
SWITCH:
switch t.TokenType {
case xml.ErrorToken:
if l.Err() == io.EOF {
return nil
}
return l.Err()
case xml.TextToken:
t.Data = parse.ReplaceMultipleWhitespace(parse.TrimWhitespace(t.Data))
if tag == svg.Style && len(t.Data) > 0 {
if err := m.Minify(defaultStyleType, w, buffer.NewReader(t.Data)); err != nil {
if err == minify.ErrNotExist { // no minifier, write the original
if _, err := w.Write(t.Data); err != nil {
return err
}
} else {
return err
}
}
} else if _, err := w.Write(t.Data); err != nil {
return err
}
case xml.CDATAToken:
if _, err := w.Write(cdataStartBytes); err != nil {
return err
}
t.Data = parse.ReplaceMultipleWhitespace(parse.TrimWhitespace(t.Data))
if tag == svg.Style && len(t.Data) > 0 {
if err := m.Minify(defaultStyleType, w, buffer.NewReader(t.Data)); err != nil {
if err == minify.ErrNotExist { // no minifier, write the original
if _, err := w.Write(t.Data); err != nil {
return err
}
} else {
return err
}
}
} else if _, err := w.Write(t.Data); err != nil {
return err
}
if _, err := w.Write(cdataEndBytes); err != nil {
return err
}
case xml.StartTagPIToken:
for {
if t := *tb.Shift(); t.TokenType == xml.StartTagClosePIToken || t.TokenType == xml.ErrorToken {
break
}
}
case xml.StartTagToken:
tag = t.Hash
if containerTagMap[t.Hash] { // skip empty containers
i := 0
for {
next := tb.Peek(i)
i++
if next.TokenType == xml.EndTagToken && next.Hash == t.Hash || next.TokenType == xml.StartTagCloseVoidToken || next.TokenType == xml.ErrorToken {
for j := 0; j < i; j++ {
tb.Shift()
}
break SWITCH
} else if next.TokenType != xml.AttributeToken && next.TokenType != xml.StartTagCloseToken {
break
}
}
} else if t.Hash == svg.Metadata {
for {
if next := *tb.Shift(); (next.TokenType == xml.EndTagToken || next.TokenType == xml.StartTagCloseVoidToken) && next.Hash == t.Hash || next.TokenType == xml.ErrorToken {
break
}
}
break
} else if t.Hash == svg.Line || t.Hash == svg.Rect {
// TODO: shape2path also for polygon and polyline
// x1, y1, x2, y2 float64 := 0, 0, 0, 0
// valid := true
// i := 0
// for {
// next := tb.Peek(i)
// i++
// if next.TokenType != xml.AttributeToken {
//.........这里部分代码省略.........