github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/minify/html.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package minify
     4  
     5  import (
     6  	"regexp"
     7  
     8  	"../protocol"
     9  
    10  	thtml "github.com/tdewolff/minify/html"
    11  	"github.com/tdewolff/parse/buffer"
    12  )
    13  
    14  var HTML html
    15  
    16  var htmlMinifier = regexp.MustCompile(`\r?\n|(<!--.*?-->)|(<!--[\w\W\n\s]+?-->)`)
    17  
    18  type html struct {
    19  	thtml.Minifier
    20  }
    21  
    22  // Minify replace file data with minify of them.
    23  func (html *html) Minify(data protocol.Codec) (err protocol.Error) {
    24  	var rawData = data.Marshal()
    25  	var minifiedData []byte
    26  	minifiedData, err = html.MinifyBytes(rawData)
    27  	if err != nil {
    28  		return
    29  	}
    30  	err = data.Unmarshal(minifiedData)
    31  	return
    32  }
    33  
    34  // MinifyBytes replace file data with minify of them.
    35  func (html *html) MinifyBytes(data []byte) (minifiedData []byte, err protocol.Error) {
    36  	// TODO::: have problem minify HTML >> https://github.com/tdewolff/minify/issues/318
    37  	if true {
    38  		minifiedData = htmlMinifier.ReplaceAll(data, []byte{})
    39  		return
    40  	}
    41  	var minifiedWriter = buffer.NewWriter(make([]byte, 0, len(data)))
    42  	var goErr = html.Minifier.Minify(minifier, minifiedWriter, buffer.NewReader(data), nil)
    43  	if goErr != nil {
    44  		// err =
    45  		return
    46  	}
    47  	minifiedData = minifiedWriter.Bytes()
    48  	return
    49  }