-
Notifications
You must be signed in to change notification settings - Fork 76
/
description.go
196 lines (156 loc) · 5.07 KB
/
description.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"bytes"
"context"
"os/exec"
"strings"
"github.com/russross/blackfriday"
)
func reformatForControl(raw string) string {
// Reformat the wrapped description to conform to Debian’s control format.
for strings.Contains(raw, "\n\n") {
raw = strings.Replace(raw, "\n\n", "\n.\n", -1)
}
return strings.Replace(raw, "\n", "\n ", -1)
}
// getDescriptionForGopkg reads from README.md (or equivalent) from GitHub,
// intended for the extended description in debian/control.
func getLongDescriptionForGopkg(gopkg string) (string, error) {
owner, repo, err := findGitHubRepo(gopkg)
if err != nil {
return "", err
}
rr, _, err := gitHub.Repositories.GetReadme(context.TODO(), owner, repo, nil)
if err != nil {
return "", err
}
content, err := rr.GetContent()
if err != nil {
return "", err
}
// Supported filename suffixes are from
// https://github.com/github/markup/blob/master/README.md
// NOTE(stapelberg): Ideally, we’d use https://github.com/github/markup
// itself to render to HTML, then convert HTML to plaintext. That sounds
// fairly involved, but it’d be the most correct solution to the problem at
// hand. Our current code just knows markdown, which is good enough since
// most (Go?) projects in fact use markdown for their README files.
if !strings.HasSuffix(rr.GetName(), "md") &&
!strings.HasSuffix(rr.GetName(), "markdown") &&
!strings.HasSuffix(rr.GetName(), "mdown") &&
!strings.HasSuffix(rr.GetName(), "mkdn") {
return reformatForControl(strings.TrimSpace(string(content))), nil
}
output := blackfriday.Markdown([]byte(content), &TextRenderer{}, 0)
// Shell out to fmt(1) to line-wrap the output.
cmd := exec.Command("fmt")
cmd.Stdin = bytes.NewBuffer(output)
out, err := cmd.Output()
if err != nil {
return "", err
}
return reformatForControl(strings.TrimSpace(string(out))), nil
}
type TextRenderer struct {
}
func (options *TextRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) {
out.Write(text)
}
func (options *TextRenderer) BlockQuote(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *TextRenderer) BlockHtml(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *TextRenderer) Header(out *bytes.Buffer, text func() bool, level int, id string) {
text()
}
func (options *TextRenderer) HRule(out *bytes.Buffer) {
out.WriteString("--------------------------------------------------------------------------------\n")
}
func (options *TextRenderer) List(out *bytes.Buffer, text func() bool, flags int) {
text()
}
func (options *TextRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
out.WriteString("• ")
out.Write(text)
}
func (options *TextRenderer) Paragraph(out *bytes.Buffer, text func() bool) {
out.WriteString("\n")
text()
out.WriteString("\n")
}
func (options *TextRenderer) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
out.Write(header)
out.Write(body)
}
func (options *TextRenderer) TableRow(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *TextRenderer) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {
out.Write(text)
}
func (options *TextRenderer) TableCell(out *bytes.Buffer, text []byte, flags int) {
out.Write(text)
}
func (options *TextRenderer) Footnotes(out *bytes.Buffer, text func() bool) {
text()
}
func (options *TextRenderer) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
out.WriteString("[")
out.Write(name)
out.WriteString("]")
out.Write(text)
}
func (options *TextRenderer) TitleBlock(out *bytes.Buffer, text []byte) {
}
// Span-level callbacks
func (options *TextRenderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
out.Write(link)
}
func (options *TextRenderer) CodeSpan(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *TextRenderer) DoubleEmphasis(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *TextRenderer) Emphasis(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *TextRenderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
out.Write(alt)
}
func (options *TextRenderer) LineBreak(out *bytes.Buffer) {
out.WriteString("\n")
}
func (options *TextRenderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
out.Write(content)
out.WriteString(" (")
out.Write(link)
out.WriteString(")")
}
func (options *TextRenderer) RawHtmlTag(out *bytes.Buffer, tag []byte) {
}
func (options *TextRenderer) TripleEmphasis(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *TextRenderer) StrikeThrough(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *TextRenderer) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
}
// Low-level callbacks
func (options *TextRenderer) Entity(out *bytes.Buffer, entity []byte) {
out.Write(entity)
}
func (options *TextRenderer) NormalText(out *bytes.Buffer, text []byte) {
out.Write(text)
}
// Header and footer
func (options *TextRenderer) DocumentHeader(out *bytes.Buffer) {
}
func (options *TextRenderer) DocumentFooter(out *bytes.Buffer) {
}
func (options *TextRenderer) GetFlags() int {
return 0
}