123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package normalizer
- import (
- "strings"
- "github.com/MakeNowJust/heredoc"
- "github.com/russross/blackfriday"
- )
- const indentation = ` `
- func LongDesc(s string) string {
- if len(s) == 0 {
- return s
- }
- return normalizer{s}.Heredoc().Markdown().Trim().string
- }
- func Examples(s string) string {
- if len(s) == 0 {
- return s
- }
- return normalizer{s}.Trim().Indent().string
- }
- type normalizer struct {
- string
- }
- func (s normalizer) Markdown() normalizer {
- bytes := []byte(s.string)
- formatted := blackfriday.Markdown(bytes, &ASCIIRenderer{Indentation: indentation}, 0)
- s.string = string(formatted)
- return s
- }
- func (s normalizer) Heredoc() normalizer {
- s.string = heredoc.Doc(s.string)
- return s
- }
- func (s normalizer) Trim() normalizer {
- s.string = strings.TrimSpace(s.string)
- return s
- }
- func (s normalizer) Indent() normalizer {
- indentedLines := []string{}
- for _, line := range strings.Split(s.string, "\n") {
- trimmed := strings.TrimSpace(line)
- indented := indentation + trimmed
- indentedLines = append(indentedLines, indented)
- }
- s.string = strings.Join(indentedLines, "\n")
- return s
- }
|