]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Rework the build.writeStats struct
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 2 Jul 2023 09:02:47 +0000 (11:02 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 2 Jul 2023 11:04:11 +0000 (13:04 +0200)
Mostly to make it easier to toggle on/off this feature from the env.

See #11191

config/commonConfig.go
hugolib/hugo_sites_build.go
hugolib/site_test.go
publisher/htmlElementsCollector.go
publisher/htmlElementsCollector_test.go
publisher/publisher.go

index 0374994654e7e7fda99496cdf818b39e983ae334..2c6497b34887c38fcc8a9560caacfc3d2739f9fc 100644 (file)
@@ -82,7 +82,7 @@ type LoadConfigResult struct {
 
 var defaultBuild = BuildConfig{
        UseResourceCacheWhen: "fallback",
-       WriteStats:           WriteStats{},
+       BuildStats:           BuildStats{},
 
        CacheBusters: []CacheBuster{
                {
@@ -112,7 +112,7 @@ type BuildConfig struct {
        // When enabled, will collect and write a hugo_stats.json with some build
        // related aggregated data (e.g. CSS class names).
        // Note that this was a bool <= v0.115.0.
-       WriteStats WriteStats
+       BuildStats BuildStats
 
        // Can be used to toggle off writing of the IntelliSense /assets/jsconfig.js
        // file.
@@ -122,15 +122,19 @@ type BuildConfig struct {
        CacheBusters []CacheBuster
 }
 
-// WriteStats configures what to write to the hugo_stats.json file.
-type WriteStats struct {
-       Tags    bool
-       Classes bool
-       IDs     bool
+// BuildStats configures if and what to write to the hugo_stats.json file.
+type BuildStats struct {
+       Enable         bool
+       DisableTags    bool
+       DisableClasses bool
+       DisableIDs     bool
 }
 
-func (w WriteStats) Enabled() bool {
-       return w.Tags || w.Classes || w.IDs
+func (w BuildStats) Enabled() bool {
+       if !w.Enable {
+               return false
+       }
+       return !w.DisableTags || !w.DisableClasses || !w.DisableIDs
 }
 
 func (b BuildConfig) clone() BuildConfig {
@@ -192,11 +196,7 @@ func DecodeBuildConfig(cfg Provider) BuildConfig {
        // writeStats was a bool <= v0.115.0.
        if writeStats, ok := m["writestats"]; ok {
                if bb, ok := writeStats.(bool); ok {
-                       m["writestats"] = WriteStats{
-                               Tags:    bb,
-                               Classes: bb,
-                               IDs:     bb,
-                       }
+                       m["buildstats"] = BuildStats{Enable: bb}
                }
        }
 
index 2c8ca0aae903c182eccdbc6f138c7a40263eb892..0a861d32e666607372d68deecba31a933cf84b91 100644 (file)
@@ -475,7 +475,7 @@ func (h *HugoSites) writeBuildStats() error {
        if h.ResourceSpec == nil {
                panic("h.ResourceSpec is nil")
        }
-       if !h.ResourceSpec.BuildConfig().WriteStats.Enabled() {
+       if !h.ResourceSpec.BuildConfig().BuildStats.Enabled() {
                return nil
        }
 
index 8265c06d07389e2033b5c56c38d0a169bb039a02..21029352f42143fe48b7748312836ee6d88c3efe 100644 (file)
@@ -1200,46 +1200,39 @@ writeStats = false
        b.AssertDestinationExists("hugo_stats.json", false)
 
        b = r(`
-[build.writeStats]
-tags = true
-classes = true
-ids = true
-       `)
+[build.buildStats]
+enable = true
+`)
 
        b.AssertFileContent("hugo_stats.json", "myclass", "div", "myid")
 
        b = r(`
-[build.writeStats]
-tags = true
-classes = true
-ids = false
+[build.buildStats]
+enable = true
+disableids = true
 `)
 
        b.AssertFileContent("hugo_stats.json", "myclass", "div", "! myid")
 
        b = r(`
-[build.writeStats]
-tags = true
-classes = false
-ids = true
+[build.buildStats]
+enable = true
+disableclasses = true
 `)
 
        b.AssertFileContent("hugo_stats.json", "! myclass", "div", "myid")
 
        b = r(`
-[build.writeStats]
-tags = false
-classes = true
-ids = true
+[build.buildStats]
+enable = true
+disabletags = true
        `)
 
        b.AssertFileContent("hugo_stats.json", "myclass", "! div", "myid")
 
        b = r(`
-[build.writeStats]
-tags = false
-classes = false
-ids = false
+[build.buildStats]
+enable = false
        `)
        b.AssertDestinationExists("hugo_stats.json", false)
 
index 0805023528ad229f351c94a220cb0ae9525148f5..c942c46e5543a11035b0252426de715b53eefb23 100644 (file)
@@ -47,7 +47,7 @@ var (
        }
 )
 
-func newHTMLElementsCollector(conf config.WriteStats) *htmlElementsCollector {
+func newHTMLElementsCollector(conf config.BuildStats) *htmlElementsCollector {
        return &htmlElementsCollector{
                conf:       conf,
                elementSet: make(map[string]bool),
@@ -95,7 +95,7 @@ type htmlElement struct {
 }
 
 type htmlElementsCollector struct {
-       conf config.WriteStats
+       conf config.BuildStats
 
        // Contains the raw HTML string. We will get the same element
        // several times, and want to avoid costly reparsing when this
@@ -117,7 +117,7 @@ func (c *htmlElementsCollector) getHTMLElements() HTMLElements {
        for _, el := range c.elements {
                classes = append(classes, el.Classes...)
                ids = append(ids, el.IDs...)
-               if c.conf.Tags {
+               if !c.conf.DisableTags {
                        tags = append(tags, el.Tag)
                }
        }
@@ -372,7 +372,7 @@ func htmlLexToEndOfComment(w *htmlElementsCollectorWriter) htmlCollectorStateFun
 func (w *htmlElementsCollectorWriter) parseHTMLElement(elStr string) (el htmlElement, err error) {
        conf := w.collector.conf
 
-       if !conf.IDs && !conf.Classes {
+       if conf.DisableTags && conf.DisableClasses {
                // Nothing to do.
                return
        }
@@ -402,11 +402,11 @@ func (w *htmlElementsCollectorWriter) parseHTMLElement(elStr string) (el htmlEle
                                switch {
                                case strings.EqualFold(a.Key, "id"):
                                        // There should be only one, but one never knows...
-                                       if conf.IDs {
+                                       if !conf.DisableIDs {
                                                el.IDs = append(el.IDs, a.Val)
                                        }
                                default:
-                                       if !conf.Classes {
+                                       if conf.DisableClasses {
                                                continue
                                        }
                                        if classAttrRe.MatchString(a.Key) {
index 51b34a3d629023b4166912c585213c55d6efce04..3047d5ca99ec12485300661d0f08a52bf4030e4a 100644 (file)
@@ -138,11 +138,7 @@ func TestClassCollector(t *testing.T) {
 
                        c.Run(fmt.Sprintf("%s--minify-%t", test.name, variant.minify), func(c *qt.C) {
                                w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
-                                       config.WriteStats{
-                                               Tags:    true,
-                                               Classes: true,
-                                               IDs:     true,
-                                       },
+                                       config.BuildStats{Enable: true},
                                ))
                                if variant.minify {
                                        if skipMinifyTest[test.name] {
@@ -248,11 +244,7 @@ func BenchmarkElementsCollectorWriter(b *testing.B) {
 `
        for i := 0; i < b.N; i++ {
                w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
-                       config.WriteStats{
-                               Tags:    true,
-                               Classes: true,
-                               IDs:     true,
-                       },
+                       config.BuildStats{Enable: true},
                ))
                fmt.Fprint(w, benchHTML)
 
@@ -276,11 +268,7 @@ func BenchmarkElementsCollectorWriterPre(b *testing.B) {
 
 `
        w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
-               config.WriteStats{
-                       Tags:    true,
-                       Classes: true,
-                       IDs:     true,
-               },
+               config.BuildStats{Enable: true},
        ))
        for i := 0; i < b.N; i++ {
                fmt.Fprint(w, benchHTML)
index 37d8b36e40833bf07fd1b274e65d30cd79f050c3..39274b2a98aba6b018e4bb9dd7d629e2d605b3c2 100644 (file)
@@ -81,8 +81,8 @@ func NewDestinationPublisher(rs *resources.Spec, outputFormats output.Formats, m
        fs := rs.BaseFs.PublishFs
        cfg := rs.Cfg
        var classCollector *htmlElementsCollector
-       if rs.BuildConfig().WriteStats.Enabled() {
-               classCollector = newHTMLElementsCollector(rs.BuildConfig().WriteStats)
+       if rs.BuildConfig().BuildStats.Enabled() {
+               classCollector = newHTMLElementsCollector(rs.BuildConfig().BuildStats)
        }
        pub = DestinationPublisher{fs: fs, htmlElementsCollector: classCollector}
        pub.min, err = minifiers.New(mediaTypes, outputFormats, cfg)