1
0

don't use delimiters for top level choice items

This commit is contained in:
Arpad Ryszka 2025-11-22 17:10:39 +01:00
parent 0ddb884813
commit cb1b9806c3
8 changed files with 90 additions and 1 deletions

View File

@ -153,7 +153,13 @@ func htmlSequence(s SyntaxItem) string {
}
func htmlChoice(s SyntaxItem) string {
ss := htmlSyntaxItems(s.choice)
items := make([]SyntaxItem, len(s.choice))
copy(items, s.choice)
for i := range items {
items[i].topLevel = s.topLevel
}
ss := htmlSyntaxItems(items)
if s.topLevel {
return strings.Join(ss, "\n")
}

View File

@ -1562,6 +1562,26 @@ lines.</p>
}
})
t.Run("top level choice with sequence item", func(t *testing.T) {
doc := textfmt.Doc(
textfmt.Syntax(
textfmt.Choice(
textfmt.Symbol("foo"),
textfmt.Sequence(textfmt.Symbol("bar"), textfmt.Symbol("baz")),
),
),
)
var b bytes.Buffer
if err := textfmt.HTMLFragment(&b, doc); err != nil {
t.Fatal(err)
}
if b.String() != "<pre>\nfoo\nbar baz\n</pre>\n" {
t.Fatal(b.String())
}
})
t.Run("choice", func(t *testing.T) {
doc := textfmt.Doc(
textfmt.Syntax(

View File

@ -293,6 +293,7 @@ func renderMDChoice(w io.Writer, s SyntaxItem) {
}
item.delimited = false
item.topLevel = s.topLevel
renderMDSyntaxItem(w, item)
}

View File

@ -1706,6 +1706,26 @@ textfmt supports the following entries:
}
})
t.Run("top level choice with sequence item", func(t *testing.T) {
doc := textfmt.Doc(
textfmt.Syntax(
textfmt.Choice(
textfmt.Symbol("foo"),
textfmt.Sequence(textfmt.Symbol("bar"), textfmt.Symbol("baz")),
),
),
)
var b bytes.Buffer
if err := textfmt.Markdown(&b, doc); err != nil {
t.Fatal(err)
}
if b.String() != "```\nfoo\nbar baz\n```\n" {
t.Fatal(b.String())
}
})
t.Run("choice", func(t *testing.T) {
doc := textfmt.Doc(
textfmt.Syntax(

View File

@ -456,6 +456,7 @@ func renderRoffChoice(w io.Writer, s SyntaxItem) {
}
item.delimited = false
item.topLevel = s.topLevel
renderRoffSyntaxItem(w, item)
}

View File

@ -2637,6 +2637,26 @@ and silver birch\~\~\~\~ | their canopies creating\~ | and shadow on the
}
})
t.Run("top level choice with sequence item", func(t *testing.T) {
doc := textfmt.Doc(
textfmt.Syntax(
textfmt.Choice(
textfmt.Symbol("foo"),
textfmt.Sequence(textfmt.Symbol("bar"), textfmt.Symbol("baz")),
),
),
)
var b bytes.Buffer
if err := textfmt.Runoff(&b, doc); err != nil {
t.Fatal(err)
}
if b.String() != ".nf\nfoo\nbar baz\n.fi\n" {
t.Fatal(b.String())
}
})
t.Run("choice", func(t *testing.T) {
doc := textfmt.Doc(
textfmt.Syntax(

View File

@ -347,6 +347,7 @@ func renderTTYChoice(w io.Writer, s SyntaxItem) {
}
item.delimited = false
item.topLevel = s.topLevel
renderTTYSyntaxItem(w, item)
}

View File

@ -2276,6 +2276,26 @@ and silver birch | their canopies creating | and shadow on the
}
})
t.Run("top level choice with sequence item", func(t *testing.T) {
doc := textfmt.Doc(
textfmt.Syntax(
textfmt.Choice(
textfmt.Symbol("foo"),
textfmt.Sequence(textfmt.Symbol("bar"), textfmt.Symbol("baz")),
),
),
)
var b bytes.Buffer
if err := textfmt.Teletype(&b, doc); err != nil {
t.Fatal(err)
}
if b.String() != "foo\nbar baz\n" {
t.Fatal(b.String())
}
})
t.Run("choice", func(t *testing.T) {
doc := textfmt.Doc(
textfmt.Syntax(