add CommentConverterFunc to simplify injection of custom converters
This commit is contained in:
@@ -19,6 +19,14 @@ type CommentConverter interface {
|
||||
Convert(text string) string
|
||||
}
|
||||
|
||||
// CommentConverterFunc functional struct implementing CommentConverter
|
||||
type CommentConverterFunc func(text string) string
|
||||
|
||||
// Convert calls func for given text
|
||||
func (f CommentConverterFunc) Convert(text string) string {
|
||||
return f(text)
|
||||
}
|
||||
|
||||
// NewCommentFormatter makes CommentFormatter
|
||||
func NewCommentFormatter(converters ...CommentConverter) *CommentFormatter {
|
||||
return &CommentFormatter{converters: converters}
|
||||
|
||||
@@ -33,6 +33,12 @@ func TestFormatter_FormatTextNoConvertor(t *testing.T) {
|
||||
assert.Equal(t, "<p>12345</p>\n", f.FormatText("12345"))
|
||||
}
|
||||
|
||||
func TestFormatter_FormatTextConvertorFunc(t *testing.T) {
|
||||
fn := CommentConverterFunc(func(text string) string { return "zz!" + text })
|
||||
f := NewCommentFormatter(fn)
|
||||
assert.Equal(t, "zz!<p>12345</p>\n", f.FormatText("12345"))
|
||||
}
|
||||
|
||||
func TestFormatter_FormatComment(t *testing.T) {
|
||||
comment := Comment{
|
||||
Text: "blah\n\nxyz",
|
||||
|
||||
Reference in New Issue
Block a user