URI: 
       config.go - hugo - [fork] hugo port for 9front
  HTML git clone https://git.drkhsh.at/hugo.git
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
       config.go (3007B)
       ---
            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/spf13/cast"
           19 
           20         "github.com/mitchellh/mapstructure"
           21         "github.com/tdewolff/minify/v2/css"
           22         "github.com/tdewolff/minify/v2/html"
           23         "github.com/tdewolff/minify/v2/js"
           24         "github.com/tdewolff/minify/v2/json"
           25         "github.com/tdewolff/minify/v2/svg"
           26         "github.com/tdewolff/minify/v2/xml"
           27 )
           28 
           29 var defaultTdewolffConfig = TdewolffConfig{
           30         HTML: html.Minifier{
           31                 KeepDocumentTags:    true,
           32                 KeepSpecialComments: true,
           33                 KeepEndTags:         true,
           34                 KeepDefaultAttrVals: true,
           35                 KeepWhitespace:      false,
           36         },
           37         CSS: css.Minifier{
           38                 Precision: 0,
           39                 KeepCSS2:  true,
           40         },
           41         JS: js.Minifier{
           42                 Version: 2022,
           43         },
           44         JSON: json.Minifier{},
           45         SVG: svg.Minifier{
           46                 KeepComments: false,
           47                 Precision:    0,
           48         },
           49         XML: xml.Minifier{
           50                 KeepWhitespace: false,
           51         },
           52 }
           53 
           54 type TdewolffConfig struct {
           55         HTML html.Minifier
           56         CSS  css.Minifier
           57         JS   js.Minifier
           58         JSON json.Minifier
           59         SVG  svg.Minifier
           60         XML  xml.Minifier
           61 }
           62 
           63 type MinifyConfig struct {
           64         // Whether to minify the published output (the HTML written to /public).
           65         MinifyOutput bool
           66 
           67         DisableHTML bool
           68         DisableCSS  bool
           69         DisableJS   bool
           70         DisableJSON bool
           71         DisableSVG  bool
           72         DisableXML  bool
           73 
           74         Tdewolff TdewolffConfig
           75 }
           76 
           77 var defaultConfig = MinifyConfig{
           78         Tdewolff: defaultTdewolffConfig,
           79 }
           80 
           81 func DecodeConfig(v any) (conf MinifyConfig, err error) {
           82         conf = defaultConfig
           83 
           84         if v == nil {
           85                 return
           86         }
           87 
           88         m := maps.ToStringMap(v)
           89 
           90         // Handle upstream renames.
           91         if td, found := m["tdewolff"]; found {
           92                 tdm := maps.ToStringMap(td)
           93 
           94                 for _, key := range []string{"css", "svg"} {
           95                         if v, found := tdm[key]; found {
           96                                 vm := maps.ToStringMap(v)
           97                                 ko := "decimal"
           98                                 kn := "precision"
           99                                 if vv, found := vm[ko]; found {
          100                                         if _, found = vm[kn]; !found {
          101                                                 vvi := cast.ToInt(vv)
          102                                                 if vvi > 0 {
          103                                                         vm[kn] = vvi
          104                                                 }
          105                                         }
          106                                         delete(vm, ko)
          107                                 }
          108                         }
          109                 }
          110 
          111                 // keepConditionalComments was renamed to keepSpecialComments
          112                 if v, found := tdm["html"]; found {
          113                         vm := maps.ToStringMap(v)
          114                         ko := "keepconditionalcomments"
          115                         kn := "keepspecialcomments"
          116                         if vv, found := vm[ko]; found {
          117                                 // Set keepspecialcomments, if not already set
          118                                 if _, found := vm[kn]; !found {
          119                                         vm[kn] = cast.ToBool(vv)
          120                                 }
          121                                 // Remove the old key to prevent deprecation warnings
          122                                 delete(vm, ko)
          123                         }
          124                 }
          125 
          126         }
          127 
          128         err = mapstructure.WeakDecode(m, &conf)
          129 
          130         if err != nil {
          131                 return
          132         }
          133 
          134         return
          135 }