add unit test for normalizer rendering markdown
Kubernetes-commit: 9d2be187e550604879ad33d99379550b6fbebc3b
This commit is contained in:
parent
8402e4686d
commit
e0b132cd00
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||||
package templates
|
package templates
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -32,9 +33,10 @@ var _ blackfriday.Renderer = &ASCIIRenderer{}
|
||||||
// documents as plain text, well suited for human reading on terminals.
|
// documents as plain text, well suited for human reading on terminals.
|
||||||
type ASCIIRenderer struct {
|
type ASCIIRenderer struct {
|
||||||
Indentation string
|
Indentation string
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ASCIIRenderer) GetFlags() int { return 0 }
|
listItemCount uint
|
||||||
|
listLevel uint
|
||||||
|
}
|
||||||
|
|
||||||
// render markdown to text
|
// render markdown to text
|
||||||
func (r *ASCIIRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
|
func (r *ASCIIRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
|
||||||
|
@ -51,13 +53,12 @@ func (r *ASCIIRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
|
||||||
}
|
}
|
||||||
case blackfriday.HorizontalRule, blackfriday.Hardbreak:
|
case blackfriday.HorizontalRule, blackfriday.Hardbreak:
|
||||||
w.Write([]byte(linebreak + "----------" + linebreak))
|
w.Write([]byte(linebreak + "----------" + linebreak))
|
||||||
case blackfriday.Softbreak:
|
case blackfriday.Code, blackfriday.CodeBlock:
|
||||||
w.Write([]byte(linebreak))
|
|
||||||
case blackfriday.Code:
|
|
||||||
w.Write([]byte(linebreak))
|
w.Write([]byte(linebreak))
|
||||||
lines := []string{}
|
lines := []string{}
|
||||||
for _, line := range strings.Split(string(node.Literal), linebreak) {
|
for _, line := range strings.Split(string(node.Literal), linebreak) {
|
||||||
indented := r.Indentation + line
|
trimmed := strings.Trim(line, " \t")
|
||||||
|
indented := r.Indentation + trimmed
|
||||||
lines = append(lines, indented)
|
lines = append(lines, indented)
|
||||||
}
|
}
|
||||||
w.Write([]byte(strings.Join(lines, linebreak)))
|
w.Write([]byte(strings.Join(lines, linebreak)))
|
||||||
|
@ -66,18 +67,42 @@ func (r *ASCIIRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
|
||||||
case blackfriday.Link:
|
case blackfriday.Link:
|
||||||
w.Write([]byte(" "))
|
w.Write([]byte(" "))
|
||||||
w.Write(node.LinkData.Destination)
|
w.Write(node.LinkData.Destination)
|
||||||
case blackfriday.List:
|
|
||||||
w.Write([]byte(linebreak))
|
|
||||||
w.Write(node.Literal)
|
|
||||||
case blackfriday.Paragraph:
|
case blackfriday.Paragraph:
|
||||||
w.Write(node.Literal)
|
if r.listLevel == 0 {
|
||||||
w.Write([]byte(linebreak))
|
w.Write([]byte(linebreak))
|
||||||
|
}
|
||||||
|
case blackfriday.List:
|
||||||
|
if entering {
|
||||||
|
w.Write([]byte(linebreak))
|
||||||
|
r.listLevel++
|
||||||
|
} else {
|
||||||
|
r.listLevel--
|
||||||
|
r.listItemCount = 0
|
||||||
|
}
|
||||||
|
case blackfriday.Item:
|
||||||
|
if entering {
|
||||||
|
r.listItemCount++
|
||||||
|
for i := 0; uint(i) < r.listLevel; i++ {
|
||||||
|
w.Write([]byte(r.Indentation))
|
||||||
|
}
|
||||||
|
if node.ListFlags&blackfriday.ListTypeOrdered != 0 {
|
||||||
|
w.Write([]byte(fmt.Sprintf("%d. ", r.listItemCount)))
|
||||||
|
} else {
|
||||||
|
w.Write([]byte("* "))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
w.Write([]byte(linebreak))
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
w.Write(node.Literal)
|
normalText(w, node.Literal)
|
||||||
}
|
}
|
||||||
return blackfriday.GoToNext
|
return blackfriday.GoToNext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func normalText(w io.Writer, text []byte) {
|
||||||
|
w.Write([]byte(strings.Trim(string(text), " \n\t")))
|
||||||
|
}
|
||||||
|
|
||||||
// RenderHeader writes document preamble and TOC if requested.
|
// RenderHeader writes document preamble and TOC if requested.
|
||||||
func (r *ASCIIRenderer) RenderHeader(w io.Writer, ast *blackfriday.Node) {
|
func (r *ASCIIRenderer) RenderHeader(w io.Writer, ast *blackfriday.Node) {
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,7 @@ type normalizer struct {
|
||||||
|
|
||||||
func (s normalizer) markdown() normalizer {
|
func (s normalizer) markdown() normalizer {
|
||||||
bytes := []byte(s.string)
|
bytes := []byte(s.string)
|
||||||
formatted := blackfriday.Run(bytes, blackfriday.WithNoExtensions(), blackfriday.WithRenderer(&ASCIIRenderer{Indentation: Indentation}))
|
formatted := blackfriday.Run(bytes, blackfriday.WithExtensions(blackfriday.NoIntraEmphasis), blackfriday.WithRenderer(&ASCIIRenderer{Indentation: Indentation}))
|
||||||
s.string = string(formatted)
|
s.string = string(formatted)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
/*
|
||||||
|
Copyright 2022 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package templates
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLongDescMarkdown(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
desc string
|
||||||
|
in string
|
||||||
|
out string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "Empty input produces empty output",
|
||||||
|
in: "",
|
||||||
|
out: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "Single line text is preserved as is",
|
||||||
|
in: "Some text",
|
||||||
|
out: "Some text",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "Consecutive new lines are combined into a single paragraph",
|
||||||
|
in: "Line1\nLine2",
|
||||||
|
out: "Line1 Line2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "Two paragraphs",
|
||||||
|
in: "Line1\n\nLine2",
|
||||||
|
out: "Line1\n\n Line2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "Leading and trailing spaces are stripped (single line)",
|
||||||
|
in: "\t \nThe text line \n \t",
|
||||||
|
out: "The text line",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "Leading and trailing spaces are stripped (multi line)",
|
||||||
|
in: "\t \nLine1\nLine2 \n \t",
|
||||||
|
out: "Line1 Line2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "List Items with order",
|
||||||
|
in: "Title\n\n1. First item\n2. Second item\n\nSome text",
|
||||||
|
out: "Title\n\n 1. First item\n 2. Second item\n\n Some text",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "Multi lines without order",
|
||||||
|
in: "\t\t\t\t\tDescriptions.\n\n * Item.\n * Item2.",
|
||||||
|
out: "Descriptions.\n \n * Item.\n * Item2.",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
|
got := LongDesc(test.in)
|
||||||
|
if got != test.out {
|
||||||
|
t.Errorf("expected(%d):\n%s\n=====\ngot(%d):\n%s\n", len(test.out), test.out, len(got), got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue