jtf.go (1809B)
1 package main 2 3 import ( 4 "fmt" 5 "go/doc/comment" 6 "strings" 7 ) 8 9 // https://jira.atlassian.com/secure/WikiRendererHelpAction.jspa?section=all 10 11 func toJTF(content string) string { 12 var p comment.Parser 13 doc := p.Parse(content) 14 buf := &strings.Builder{} 15 for _, block := range doc.Content { 16 switch v := block.(type) { 17 case *comment.Heading: 18 fmt.Fprintf(buf, "h3. %s\n", render(v.Text)) 19 case *comment.Paragraph: 20 fmt.Fprintln(buf, render(v.Text)) 21 case *comment.Code: 22 fmt.Fprintf(buf, "{noformat}%s{noformat}\n", v.Text) 23 case *comment.List: 24 fmt.Fprintln(buf, renderList(v)) 25 } 26 fmt.Fprintln(buf) 27 } 28 return strings.TrimSpace(buf.String()) 29 } 30 31 func renderList(list *comment.List) string { 32 buf := &strings.Builder{} 33 prefix := "*" 34 for _, it := range list.Items { 35 if it.Number != "" { 36 prefix = "#" 37 } 38 for _, block := range it.Content { 39 // the block is known to be a paragraph 40 s := render(block.(*comment.Paragraph).Text) 41 fmt.Fprintln(buf, prefix, s) 42 } 43 } 44 return buf.String() 45 } 46 47 func render(text []comment.Text) string { 48 buf := &strings.Builder{} 49 for _, txt := range text { 50 switch v := txt.(type) { 51 case comment.Plain: 52 s := strings.ReplaceAll(string(v), "\n", " ") 53 if strings.HasPrefix(s, "> ") { 54 s = strings.ReplaceAll(s, "> ", "") 55 fmt.Fprintf(buf, "{quote}%s{quote}", s) 56 } else { 57 buf.WriteString(s) 58 } 59 case comment.Italic: 60 fmt.Fprintf(buf, "*%s*", v) 61 case *comment.Link: 62 if v.Auto { 63 fmt.Fprintf(buf, "[%s]", v.URL) 64 } else { 65 title := render(v.Text) 66 fmt.Fprintf(buf, "[%s|%s]", title, v.URL) 67 } 68 case *comment.DocLink: 69 // we're not actually printing godoc, so treat 70 // any accidental DocLink as plain text. 71 buf.WriteString(render(v.Text)) 72 default: 73 fmt.Fprintf(buf, "%v", v) 74 } 75 } 76 return buf.String() 77 }