]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
markup/asciidocext: Support boolean document attributes
authorJoe Mooring <joe.mooring@veriphor.com>
Thu, 13 Nov 2025 16:23:12 +0000 (08:23 -0800)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 13 Nov 2025 18:04:21 +0000 (19:04 +0100)
Closes #14138

markup/asciidocext/asciidocext_config/config.go
markup/asciidocext/convert_test.go
markup/asciidocext/internal/converter.go

index 4c38fbbbf7b22d3bb8c4c22bb5ab93b7e1d819b1..720e71286f55fed086dcf559512b44776619bc9c 100644 (file)
@@ -19,7 +19,7 @@ var (
        Default = Config{
                Backend:              "html5",
                Extensions:           []string{},
-               Attributes:           map[string]string{},
+               Attributes:           map[string]any{},
                NoHeaderOrFooter:     true,
                SafeMode:             "unsafe",
                SectionNumbers:       false,
@@ -67,7 +67,7 @@ var (
 type Config struct {
        Backend              string
        Extensions           []string
-       Attributes           map[string]string
+       Attributes           map[string]any
        NoHeaderOrFooter     bool
        SafeMode             string
        SectionNumbers       bool
index e93cab00bb8b1e3d90a31f0cc95fe262699da280..bfcd4ae8fd3b08381b4d6d023e3a29d327c5463f 100644 (file)
@@ -97,7 +97,7 @@ func TestAsciidoctorDisallowedArgs(t *testing.T) {
        mconf := markup_config.Default
        mconf.AsciidocExt.Backend = "disallowed-backend"
        mconf.AsciidocExt.Extensions = []string{"./disallowed-extension"}
-       mconf.AsciidocExt.Attributes = map[string]string{"outdir": "disallowed-attribute"}
+       mconf.AsciidocExt.Attributes = map[string]any{"outdir": "disallowed-attribute"}
        mconf.AsciidocExt.SafeMode = "disallowed-safemode"
        mconf.AsciidocExt.FailureLevel = "disallowed-failurelevel"
 
@@ -267,6 +267,8 @@ trace = false
 [markup.asciidocext.attributes]
 my-base-url = "https://gohugo.io/"
 my-attribute-name = "my value"
+my-attribute-true = true
+my-attribute-false = false
 `)
        conf := testconfig.GetTestConfig(nil, cfg)
        p, err := asciidocext.Provider.New(
@@ -286,15 +288,21 @@ my-attribute-name = "my value"
        expectedValues := map[string]bool{
                "my-base-url=https://gohugo.io/": true,
                "my-attribute-name=my value":     true,
+               "my-attribute-true":              true,
+               "'!my-attribute-false'":          true,
        }
 
        args := ac.ParseArgs(converter.DocumentContext{})
-       c.Assert(len(args), qt.Equals, 5)
+       c.Assert(len(args), qt.Equals, 9)
        c.Assert(args[0], qt.Equals, "-a")
        c.Assert(expectedValues[args[1]], qt.Equals, true)
        c.Assert(args[2], qt.Equals, "-a")
        c.Assert(expectedValues[args[3]], qt.Equals, true)
-       c.Assert(args[4], qt.Equals, "--no-header-footer")
+       c.Assert(args[4], qt.Equals, "-a")
+       c.Assert(expectedValues[args[5]], qt.Equals, true)
+       c.Assert(args[6], qt.Equals, "-a")
+       c.Assert(expectedValues[args[7]], qt.Equals, true)
+       c.Assert(args[8], qt.Equals, "--no-header-footer")
 }
 
 func getProvider(c *qt.C, mConfStr string) converter.Provider {
index f6ae4136ea396eea31a1c31428f83510529a4c68..47e4066410f064741b8d52bd641bee2882c3dc10 100644 (file)
@@ -11,6 +11,7 @@ import (
        "github.com/gohugoio/hugo/markup/converter"
        "github.com/gohugoio/hugo/markup/internal"
        "github.com/gohugoio/hugo/markup/tableofcontents"
+       "github.com/spf13/cast"
        "golang.org/x/net/html"
 )
 
@@ -89,7 +90,18 @@ func (a *AsciidocConverter) ParseArgs(ctx converter.DocumentContext) []string {
                        continue
                }
 
-               args = append(args, "-a", attributeKey+"="+attributeValue)
+               // To set a document attribute to true: -a attributeKey
+               // To set a document attribute to false: -a '!attributeKey'
+               // For other types: -a attributeKey=attributeValue
+               if b, ok := attributeValue.(bool); ok {
+                       arg := attributeKey
+                       if !b {
+                               arg = "'!" + attributeKey + "'"
+                       }
+                       args = append(args, "-a", arg)
+               } else {
+                       args = append(args, "-a", attributeKey+"="+cast.ToString(attributeValue))
+               }
        }
 
        if cfg.WorkingFolderCurrent {