markup/asciidocext: Support boolean document attributes

Closes #14138
This commit is contained in:
Joe Mooring
2025-11-13 08:23:12 -08:00
committed by Bjørn Erik Pedersen
parent 9289aa43aa
commit bca171b691
3 changed files with 26 additions and 6 deletions
@@ -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
+11 -3
View 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 {
+13 -1
View 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 {