aboutsummaryrefslogtreecommitdiffhomepage
path: root/publisher
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2023-07-01 10:37:38 +0200
committerBjørn Erik Pedersen <[email protected]>2023-07-01 15:38:32 +0200
commit11ecea610617035745e620278a309d98091f527c (patch)
tree5678f3a089542b80316f3ee1ebdb7bc3aa9197fe /publisher
parentda98724bc8bd28532222fb379af7a3782e2a9480 (diff)
downloadhugo-11ecea610617035745e620278a309d98091f527c.tar.gz
hugo-11ecea610617035745e620278a309d98091f527c.zip
Make build.writeStats a struct
So you can do ```toml [build.writeStats] tags = true classes = true ids = false ``` Fixes #11191
Diffstat (limited to 'publisher')
-rw-r--r--publisher/htmlElementsCollector.go27
-rw-r--r--publisher/htmlElementsCollector_test.go25
-rw-r--r--publisher/publisher.go4
3 files changed, 46 insertions, 10 deletions
diff --git a/publisher/htmlElementsCollector.go b/publisher/htmlElementsCollector.go
index c3b88c4cc..080502352 100644
--- a/publisher/htmlElementsCollector.go
+++ b/publisher/htmlElementsCollector.go
@@ -24,6 +24,7 @@ import (
"golang.org/x/net/html"
+ "github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/helpers"
)
@@ -46,8 +47,9 @@ var (
}
)
-func newHTMLElementsCollector() *htmlElementsCollector {
+func newHTMLElementsCollector(conf config.WriteStats) *htmlElementsCollector {
return &htmlElementsCollector{
+ conf: conf,
elementSet: make(map[string]bool),
}
}
@@ -93,6 +95,8 @@ type htmlElement struct {
}
type htmlElementsCollector struct {
+ conf config.WriteStats
+
// Contains the raw HTML string. We will get the same element
// several times, and want to avoid costly reparsing when this
// is used for aggregated data only.
@@ -113,7 +117,9 @@ func (c *htmlElementsCollector) getHTMLElements() HTMLElements {
for _, el := range c.elements {
classes = append(classes, el.Classes...)
ids = append(ids, el.IDs...)
- tags = append(tags, el.Tag)
+ if c.conf.Tags {
+ tags = append(tags, el.Tag)
+ }
}
classes = helpers.UniqueStringsSorted(classes)
@@ -246,7 +252,7 @@ func (w *htmlElementsCollectorWriter) lexElementInside(resolve htmlCollectorStat
}
// Parse each collected element.
- el, err := parseHTMLElement(s)
+ el, err := w.parseHTMLElement(s)
if err != nil {
w.err = err
return resolve
@@ -363,7 +369,13 @@ func htmlLexToEndOfComment(w *htmlElementsCollectorWriter) htmlCollectorStateFun
return htmlLexToEndOfComment
}
-func parseHTMLElement(elStr string) (el htmlElement, err error) {
+func (w *htmlElementsCollectorWriter) parseHTMLElement(elStr string) (el htmlElement, err error) {
+ conf := w.collector.conf
+
+ if !conf.IDs && !conf.Classes {
+ // Nothing to do.
+ return
+ }
tagName := parseStartTag(elStr)
@@ -390,8 +402,13 @@ func parseHTMLElement(elStr string) (el htmlElement, err error) {
switch {
case strings.EqualFold(a.Key, "id"):
// There should be only one, but one never knows...
- el.IDs = append(el.IDs, a.Val)
+ if conf.IDs {
+ el.IDs = append(el.IDs, a.Val)
+ }
default:
+ if !conf.Classes {
+ continue
+ }
if classAttrRe.MatchString(a.Key) {
el.Classes = append(el.Classes, strings.Fields(a.Val)...)
} else {
diff --git a/publisher/htmlElementsCollector_test.go b/publisher/htmlElementsCollector_test.go
index 7aeda0daf..51b34a3d6 100644
--- a/publisher/htmlElementsCollector_test.go
+++ b/publisher/htmlElementsCollector_test.go
@@ -22,6 +22,7 @@ import (
"testing"
"time"
+ "github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/config/testconfig"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/minifiers"
@@ -136,7 +137,13 @@ func TestClassCollector(t *testing.T) {
} {
c.Run(fmt.Sprintf("%s--minify-%t", test.name, variant.minify), func(c *qt.C) {
- w := newHTMLElementsCollectorWriter(newHTMLElementsCollector())
+ w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
+ config.WriteStats{
+ Tags: true,
+ Classes: true,
+ IDs: true,
+ },
+ ))
if variant.minify {
if skipMinifyTest[test.name] {
c.Skip("skip minify test")
@@ -240,7 +247,13 @@ func BenchmarkElementsCollectorWriter(b *testing.B) {
</html>
`
for i := 0; i < b.N; i++ {
- w := newHTMLElementsCollectorWriter(newHTMLElementsCollector())
+ w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
+ config.WriteStats{
+ Tags: true,
+ Classes: true,
+ IDs: true,
+ },
+ ))
fmt.Fprint(w, benchHTML)
}
@@ -262,7 +275,13 @@ func BenchmarkElementsCollectorWriterPre(b *testing.B) {
<div class="foo"></div>
`
- w := newHTMLElementsCollectorWriter(newHTMLElementsCollector())
+ w := newHTMLElementsCollectorWriter(newHTMLElementsCollector(
+ config.WriteStats{
+ Tags: true,
+ Classes: true,
+ IDs: true,
+ },
+ ))
for i := 0; i < b.N; i++ {
fmt.Fprint(w, benchHTML)
diff --git a/publisher/publisher.go b/publisher/publisher.go
index 970c93e6c..37d8b36e4 100644
--- a/publisher/publisher.go
+++ b/publisher/publisher.go
@@ -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 {
- classCollector = newHTMLElementsCollector()
+ if rs.BuildConfig().WriteStats.Enabled() {
+ classCollector = newHTMLElementsCollector(rs.BuildConfig().WriteStats)
}
pub = DestinationPublisher{fs: fs, htmlElementsCollector: classCollector}
pub.min, err = minifiers.New(mediaTypes, outputFormats, cfg)