Skip to content

Commit

Permalink
fix: escape asterix in adoc (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad committed Feb 28, 2024
1 parent 74c953d commit c62fbcd
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
23 changes: 23 additions & 0 deletions renderer/asciidoctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (adr *AsciidoctorRenderer) ToFuncMap() template.FuncMap {
"ShouldRenderType": adr.ShouldRenderType,
"TypeID": adr.TypeID,
"RenderFieldDoc": adr.RenderFieldDoc,
"RenderValidation": adr.RenderValidation,
}
}

Expand Down Expand Up @@ -157,3 +158,25 @@ func (adr *AsciidoctorRenderer) RenderFieldDoc(text string) string {
// See: https://docs.asciidoctor.org/asciidoc/latest/blocks/hard-line-breaks
return strings.Join(lines, " +\n\n")
}

func (adr *AsciidoctorRenderer) RenderValidation(text string) string {
return escapeFirstAsterixInEachPair(text)
}

// escapeFirstAsterixInEachPair escapes the first asterix in each pair of
// asterixes in text. E.g. "*a*b*c*" -> "\*a*b\*c*" and "*a*b*" -> "\*a*b*".
func escapeFirstAsterixInEachPair(text string) string {
index := -1
for i := 0; i < len(text); i++ {
if text[i] == '*' {
if index >= 0 {
text = text[:index] + "\\" + text[index:]
index = -1
i++
} else {
index = i
}
}
}
return text
}
15 changes: 15 additions & 0 deletions renderer/asciidoctor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package renderer

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_escapeFirstAsterixInEachPair(t *testing.T) {
assert.Equal(t, "[a-z]", escapeFirstAsterixInEachPair("[a-z]"))
assert.Equal(t, "[a-z]*", escapeFirstAsterixInEachPair("[a-z]*"))
assert.Equal(t, `0\*[a-z]*`, escapeFirstAsterixInEachPair(`0*[a-z]*`))
assert.Equal(t, `0\*[a-z]*[a-z]*[0-9]`, escapeFirstAsterixInEachPair(`0*[a-z]*[a-z]*[0-9]`))
assert.Equal(t, `0\*[a-z]*[a-z]\*[0-9]*`, escapeFirstAsterixInEachPair(`0*[a-z]*[a-z]*[0-9]*`))
}
14 changes: 7 additions & 7 deletions renderer/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,6 @@ func (m *MarkdownRenderer) RenderExternalLink(link, text string) string {
return fmt.Sprintf("[%s](%s)", text, link)
}

func (m *MarkdownRenderer) RenderDefault(text string) string {
return strings.NewReplacer(
"{", "\\{",
"}", "\\}",
).Replace(text)
}

func (m *MarkdownRenderer) RenderGVLink(gv types.GroupVersionDetails) string {
return m.RenderLocalLink(gv.GroupVersionString())
}
Expand All @@ -158,3 +151,10 @@ func (m *MarkdownRenderer) RenderFieldDoc(text string) string {
// Replace newlines with 2 line breaks so that they don't break the Markdown table formatting.
return strings.ReplaceAll(out, "\n", "<br /><br />")
}

func (m *MarkdownRenderer) RenderDefault(text string) string {
return strings.NewReplacer(
"{", "\\{",
"}", "\\}",
).Replace(text)
}
2 changes: 1 addition & 1 deletion templates/asciidoctor/type.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
{{ end -}}

{{ range $type.Members -}}
| *`{{ .Name }}`* __{{ asciidocRenderType .Type }}__ | {{ template "type_members" . }} | {{ .Default }} | {{ range .Validation -}} {{ . }} +
| *`{{ .Name }}`* __{{ asciidocRenderType .Type }}__ | {{ template "type_members" . }} | {{ .Default }} | {{ range .Validation -}} {{ asciidocRenderValidation . }} +
{{ end }}
{{ end -}}
|===
Expand Down
3 changes: 2 additions & 1 deletion test/api/v1/guestbook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ type PositiveInt int
type GuestbookEntry struct {
// Name of the guest (pipe | should be escaped)
// +kubebuilder:validation:MaxLength=80
// +kubebuilder:validation:Pattern=`0*[a-z0-9]*[a-z]*[0-9]`
Name string `json:"name,omitempty"`
// Time of entry
Time metav1.Time `json:"time,omitempty"`
// Comment by guest. This can be a multi-line comment.
//
// Just like this one.
// +kubebuilder:validation:Pattern=`[a-z0-9]`
// +kubebuilder:validation:Pattern=`0*[a-z0-9]*[a-z]*[0-9]*`
Comment string `json:"comment,omitempty"`
// Rating provided by the guest
Rating Rating `json:"rating,omitempty"`
Expand Down
3 changes: 2 additions & 1 deletion test/expected.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,12 @@ GuestbookEntry defines an entry in a guest book.
|===
| Field | Description | Default | Validation
| *`name`* __string__ | Name of the guest (pipe \| should be escaped) | | MaxLength: 80 +
Pattern: `0\*[a-z0-9]*[a-z]*[0-9]` +

| *`time`* __link:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#time-v1-meta[$$Time$$]__ | Time of entry | |
| *`comment`* __string__ | Comment by guest. This can be a multi-line comment. +

Just like this one. | | Pattern: `[a-z0-9]` +
Just like this one. | | Pattern: `0\*[a-z0-9]*[a-z]\*[0-9]*` +

| *`rating`* __xref:{anchor_prefix}-github-com-elastic-crd-ref-docs-api-v1-rating[$$Rating$$]__ | Rating provided by the guest | | Maximum: 5 +
Minimum: 1 +
Expand Down
4 changes: 2 additions & 2 deletions test/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ _Appears in:_

| Field | Description | Default | Validation |
| --- | --- | --- | --- |
| `name` _string_ | Name of the guest (pipe \| should be escaped) | | MaxLength: 80 <br /> |
| `name` _string_ | Name of the guest (pipe \| should be escaped) | | MaxLength: 80 <br />Pattern: `0*[a-z0-9]*[a-z]*[0-9]` <br /> |
| `time` _[Time](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#time-v1-meta)_ | Time of entry | | |
| `comment` _string_ | Comment by guest. This can be a multi-line comment. <br /><br /> Just like this one. | | Pattern: `[a-z0-9]` <br /> |
| `comment` _string_ | Comment by guest. This can be a multi-line comment. <br /><br /> Just like this one. | | Pattern: `0*[a-z0-9]*[a-z]*[0-9]*` <br /> |
| `rating` _[Rating](#rating)_ | Rating provided by the guest | | Maximum: 5 <br />Minimum: 1 <br /> |


Expand Down

0 comments on commit c62fbcd

Please sign in to comment.