Skip to content

Commit ad2cee6

Browse files
committedNov 19, 2020
added format_commnt helper
1 parent 5766639 commit ad2cee6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎render/render.go

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func Render(template string, def parser.Definition, params map[string]interface{
2222
ctx.Set("def", def)
2323
ctx.Set("params", params)
2424
ctx.Set("json", toJSONHelper)
25+
ctx.Set("formatComment", formatComment)
2526
ctx.Set("format_comment_text", formatCommentText)
2627
ctx.Set("format_comment_html", formatCommentHTML)
2728
ctx.Set("format_tags", formatTags)
@@ -40,6 +41,12 @@ func toJSONHelper(v interface{}) (template.HTML, error) {
4041
return template.HTML(b), nil
4142
}
4243

44+
func formatComment(s string, linePrefix string) template.HTML {
45+
var buf bytes.Buffer
46+
doc.ToText(&buf, s, linePrefix, linePrefix, 80)
47+
return template.HTML(buf.String())
48+
}
49+
4350
func formatCommentText(s string) template.HTML {
4451
var buf bytes.Buffer
4552
doc.ToText(&buf, s, "// ", "", 80)

‎render/render_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,19 @@ func TestFormatCommentText(t *testing.T) {
108108
actual = strings.TrimSpace(string(formatCommentText(`What happens if I use "quotes"?`)))
109109
is.Equal(actual, `// What happens if I use "quotes"?`)
110110

111+
actual = strings.TrimSpace(string(formatCommentText("What about\nnew lines?")))
112+
is.Equal(actual, `// What about new lines?`)
113+
114+
}
115+
116+
func TestFormatComment(t *testing.T) {
117+
is := is.New(t)
118+
119+
longComment := `This is a long comment that will end up spanning
120+
multiple lines so we get to test the indent string option
121+
in formatComment.`
122+
actual := strings.TrimSpace(string(formatComment(longComment, "\t\t")))
123+
is.Equal(actual, `This is a long comment that will end up spanning multiple lines so we get to
124+
test the indent string option in formatComment.`)
125+
111126
}

0 commit comments

Comments
 (0)
Please sign in to comment.