Skip to content

Commit bca171b

Browse files
jmooringbep
authored andcommitted
markup/asciidocext: Support boolean document attributes
Closes #14138
1 parent 9289aa4 commit bca171b

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

‎markup/asciidocext/asciidocext_config/config.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
Default = Config{
2020
Backend: "html5",
2121
Extensions: []string{},
22-
Attributes: map[string]string{},
22+
Attributes: map[string]any{},
2323
NoHeaderOrFooter: true,
2424
SafeMode: "unsafe",
2525
SectionNumbers: false,
@@ -67,7 +67,7 @@ var (
6767
type Config struct {
6868
Backend string
6969
Extensions []string
70-
Attributes map[string]string
70+
Attributes map[string]any
7171
NoHeaderOrFooter bool
7272
SafeMode string
7373
SectionNumbers bool

‎markup/asciidocext/convert_test.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestAsciidoctorDisallowedArgs(t *testing.T) {
9797
mconf := markup_config.Default
9898
mconf.AsciidocExt.Backend = "disallowed-backend"
9999
mconf.AsciidocExt.Extensions = []string{"./disallowed-extension"}
100-
mconf.AsciidocExt.Attributes = map[string]string{"outdir": "disallowed-attribute"}
100+
mconf.AsciidocExt.Attributes = map[string]any{"outdir": "disallowed-attribute"}
101101
mconf.AsciidocExt.SafeMode = "disallowed-safemode"
102102
mconf.AsciidocExt.FailureLevel = "disallowed-failurelevel"
103103

@@ -267,6 +267,8 @@ trace = false
267267
[markup.asciidocext.attributes]
268268
my-base-url = "https://gohugo.io/"
269269
my-attribute-name = "my value"
270+
my-attribute-true = true
271+
my-attribute-false = false
270272
`)
271273
conf := testconfig.GetTestConfig(nil, cfg)
272274
p, err := asciidocext.Provider.New(
@@ -286,15 +288,21 @@ my-attribute-name = "my value"
286288
expectedValues := map[string]bool{
287289
"my-base-url=https://gohugo.io/": true,
288290
"my-attribute-name=my value": true,
291+
"my-attribute-true": true,
292+
"'!my-attribute-false'": true,
289293
}
290294

291295
args := ac.ParseArgs(converter.DocumentContext{})
292-
c.Assert(len(args), qt.Equals, 5)
296+
c.Assert(len(args), qt.Equals, 9)
293297
c.Assert(args[0], qt.Equals, "-a")
294298
c.Assert(expectedValues[args[1]], qt.Equals, true)
295299
c.Assert(args[2], qt.Equals, "-a")
296300
c.Assert(expectedValues[args[3]], qt.Equals, true)
297-
c.Assert(args[4], qt.Equals, "--no-header-footer")
301+
c.Assert(args[4], qt.Equals, "-a")
302+
c.Assert(expectedValues[args[5]], qt.Equals, true)
303+
c.Assert(args[6], qt.Equals, "-a")
304+
c.Assert(expectedValues[args[7]], qt.Equals, true)
305+
c.Assert(args[8], qt.Equals, "--no-header-footer")
298306
}
299307

300308
func getProvider(c *qt.C, mConfStr string) converter.Provider {

‎markup/asciidocext/internal/converter.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/gohugoio/hugo/markup/converter"
1212
"github.com/gohugoio/hugo/markup/internal"
1313
"github.com/gohugoio/hugo/markup/tableofcontents"
14+
"github.com/spf13/cast"
1415
"golang.org/x/net/html"
1516
)
1617

@@ -89,7 +90,18 @@ func (a *AsciidocConverter) ParseArgs(ctx converter.DocumentContext) []string {
8990
continue
9091
}
9192

92-
args = append(args, "-a", attributeKey+"="+attributeValue)
93+
// To set a document attribute to true: -a attributeKey
94+
// To set a document attribute to false: -a '!attributeKey'
95+
// For other types: -a attributeKey=attributeValue
96+
if b, ok := attributeValue.(bool); ok {
97+
arg := attributeKey
98+
if !b {
99+
arg = "'!" + attributeKey + "'"
100+
}
101+
args = append(args, "-a", arg)
102+
} else {
103+
args = append(args, "-a", attributeKey+"="+cast.ToString(attributeValue))
104+
}
93105
}
94106

95107
if cfg.WorkingFolderCurrent {

0 commit comments

Comments
 (0)