hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

config.go (3011B)

    1 // Copyright 2019 The Hugo Authors. All rights reserved.
    2 //
    3 // Licensed under the Apache License, Version 2.0 (the "License");
    4 // you may not use this file except in compliance with the License.
    5 // You may obtain a copy of the License at
    6 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 package minifiers
   15 
   16 import (
   17 	"github.com/gohugoio/hugo/common/maps"
   18 	"github.com/gohugoio/hugo/config"
   19 	"github.com/gohugoio/hugo/docshelper"
   20 	"github.com/gohugoio/hugo/parser"
   21 	"github.com/spf13/cast"
   22 
   23 	"github.com/mitchellh/mapstructure"
   24 	"github.com/tdewolff/minify/v2/css"
   25 	"github.com/tdewolff/minify/v2/html"
   26 	"github.com/tdewolff/minify/v2/js"
   27 	"github.com/tdewolff/minify/v2/json"
   28 	"github.com/tdewolff/minify/v2/svg"
   29 	"github.com/tdewolff/minify/v2/xml"
   30 )
   31 
   32 var defaultTdewolffConfig = tdewolffConfig{
   33 	HTML: html.Minifier{
   34 		KeepDocumentTags:        true,
   35 		KeepConditionalComments: true,
   36 		KeepEndTags:             true,
   37 		KeepDefaultAttrVals:     true,
   38 		KeepWhitespace:          false,
   39 	},
   40 	CSS: css.Minifier{
   41 		Precision: 0,
   42 		KeepCSS2:  true,
   43 	},
   44 	JS:   js.Minifier{},
   45 	JSON: json.Minifier{},
   46 	SVG: svg.Minifier{
   47 		KeepComments: false,
   48 		Precision:    0,
   49 	},
   50 	XML: xml.Minifier{
   51 		KeepWhitespace: false,
   52 	},
   53 }
   54 
   55 type tdewolffConfig struct {
   56 	HTML html.Minifier
   57 	CSS  css.Minifier
   58 	JS   js.Minifier
   59 	JSON json.Minifier
   60 	SVG  svg.Minifier
   61 	XML  xml.Minifier
   62 }
   63 
   64 type minifyConfig struct {
   65 	// Whether to minify the published output (the HTML written to /public).
   66 	MinifyOutput bool
   67 
   68 	DisableHTML bool
   69 	DisableCSS  bool
   70 	DisableJS   bool
   71 	DisableJSON bool
   72 	DisableSVG  bool
   73 	DisableXML  bool
   74 
   75 	Tdewolff tdewolffConfig
   76 }
   77 
   78 var defaultConfig = minifyConfig{
   79 	Tdewolff: defaultTdewolffConfig,
   80 }
   81 
   82 func decodeConfig(cfg config.Provider) (conf minifyConfig, err error) {
   83 	conf = defaultConfig
   84 
   85 	// May be set by CLI.
   86 	conf.MinifyOutput = cfg.GetBool("minifyOutput")
   87 
   88 	v := cfg.Get("minify")
   89 	if v == nil {
   90 		return
   91 	}
   92 
   93 	// Legacy.
   94 	if b, ok := v.(bool); ok {
   95 		conf.MinifyOutput = b
   96 		return
   97 	}
   98 
   99 	m := maps.ToStringMap(v)
  100 
  101 	// Handle upstream renames.
  102 	if td, found := m["tdewolff"]; found {
  103 		tdm := maps.ToStringMap(td)
  104 		for _, key := range []string{"css", "svg"} {
  105 			if v, found := tdm[key]; found {
  106 				vm := maps.ToStringMap(v)
  107 				if vv, found := vm["decimal"]; found {
  108 					vvi := cast.ToInt(vv)
  109 					if vvi > 0 {
  110 						vm["precision"] = vvi
  111 					}
  112 				}
  113 			}
  114 		}
  115 	}
  116 
  117 	err = mapstructure.WeakDecode(m, &conf)
  118 
  119 	if err != nil {
  120 		return
  121 	}
  122 
  123 	return
  124 }
  125 
  126 func init() {
  127 	docsProvider := func() docshelper.DocProvider {
  128 		return docshelper.DocProvider{"config": map[string]any{"minify": parser.LowerCaseCamelJSONMarshaller{Value: defaultConfig}}}
  129 	}
  130 	docshelper.AddDocProviderFunc(docsProvider)
  131 }