quote.go (861B)
1 package quote 2 3 import ( 4 "strings" 5 ) 6 7 type Quote struct { 8 Author string `json:"author"` 9 Comment string `json:"comment"` 10 Source string `json:"source"` 11 Tags map[string]struct{} `json:"tags"` 12 Text string `json:"text"` 13 } 14 15 func (q *Quote) Render() string { 16 var str strings.Builder 17 18 str.WriteString(q.Text) 19 20 if q.Author != "" || q.Source != "" { 21 str.WriteString("\n\n") 22 } 23 if q.Author != "" { 24 str.WriteString(q.Author) 25 } 26 if q.Author != "" && q.Source != "" { 27 str.WriteString(", ") 28 } 29 if q.Source != "" { 30 str.WriteString(q.Source) 31 } 32 if q.Comment != "" { 33 str.WriteString("\n\n") 34 str.WriteString(q.Comment) 35 } 36 if len(q.Tags) > 0 { 37 str.WriteString("\nTags: ") 38 var tags string 39 for tag := range q.Tags { 40 tags += tag + " " 41 } 42 str.WriteString(tags) 43 } 44 45 return str.String() 46 }