/
markup.go
240 lines (211 loc) · 4.67 KB
/
markup.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"bytes"
"io"
"regexp"
"strings"
Q "github.com/PuerkitoBio/goquery"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"github.com/kr/text"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
var INDENT = 3
type Paragraph struct {
data []byte
indent int
cols int
prefix string
}
// Buffer adds data to the internal paragraph buffer.
func (p *Paragraph) Buffer(b []byte) {
p.data = append(p.data, b...)
}
var (
nl = []byte{'\n'}
sp = []byte{' '}
)
// makePad makes a whitespace pad that is l bytes long.
func makePad(l int) []byte {
ret := make([]byte, l)
for i := 0; i < l; i++ {
ret[i] = ' '
}
return ret
}
var spaceRE = regexp.MustCompile(`[[:space:]]+`)
// specify replaces arbitrary strings of whitespace with
// a single ' ' character. Also strips off leading and trailing
// whitespace.
func spacify(s string) string {
v := spaceRE.Split(s, -1)
if len(v) > 0 && v[0] == "" {
v = v[1:]
}
l := len(v)
if l > 0 && v[l-1] == "" {
v = v[0:(l - 1)]
}
return strings.Join(v, " ")
}
// Output a paragraph to the io.Writer, applying the proper
// formatting.
func (p Paragraph) Output(out io.Writer) {
s := []byte(spacify(string(p.data)))
if len(s) == 0 {
out.Write(nl)
return
}
indent := p.indent * INDENT
width := p.cols - indent - len(p.prefix)
wrapped := text.WrapBytes(s, width)
lines := bytes.Split(wrapped, nl)
gutter := makePad(indent)
pad := makePad(len(p.prefix))
for i, line := range lines {
out.Write(gutter)
if i == 0 {
out.Write([]byte(p.prefix))
} else {
out.Write(pad)
}
out.Write(line)
out.Write(nl)
}
}
type Renderer struct {
libkb.Contextified
indent int
cols int
out io.Writer
paragraph *Paragraph
}
func NewRenderer(g *libkb.GlobalContext, out io.Writer) *Renderer {
width, _ := g.UI.GetTerminalUI().TerminalSize()
if width == 0 {
width = 80
}
return &Renderer{Contextified: libkb.NewContextified(g), indent: 0, out: out, cols: width}
}
func (r *Renderer) RenderNodes(nodes []*html.Node) {
for _, n := range nodes {
r.RenderNode(n)
}
}
func (r *Renderer) Buffer(d []byte) {
if r.paragraph == nil {
d = bytes.TrimSpace(d)
if len(d) > 0 {
r.G().Log.Warning("floating data in Markup is ignored: %v", d)
}
} else {
r.paragraph.Buffer(d)
}
}
func (r *Renderer) NewParagraph(prefix string) *Paragraph {
r.FlushParagraph()
r.paragraph = &Paragraph{indent: r.indent, cols: r.cols, prefix: prefix}
return r.paragraph
}
func (r *Renderer) FlushParagraph() {
if r.paragraph != nil {
r.paragraph.Output(r.out)
r.paragraph = nil
}
}
func GetNodeAttrVal(node *html.Node, which string) *string {
for _, attr := range node.Attr {
if attr.Key == which {
return &attr.Val
}
}
return nil
}
func (r *Renderer) RenderNode(node *html.Node) {
if node.Type == html.TextNode {
r.Buffer([]byte(node.Data))
return
}
var cp *CodePair
switch node.DataAtom {
case atom.Ul:
r.indent++
case atom.Li:
var bullet string
if bp := GetNodeAttrVal(node, "bullet"); bp != nil {
bullet = *bp
} else {
bullet = "• "
}
r.NewParagraph(bullet)
case atom.P:
r.NewParagraph("")
case atom.Strong:
cp = &CpBold
case atom.Em:
cp = &CpItalic
default:
if node.Data == "url" {
cp = &CpUnderline
} else if node.Data == "color" {
if c := GetNodeAttrVal(node, "name"); c != nil {
cp = GetColorCode(*c)
}
}
}
if cp != nil {
r.Buffer(cp.OpenBytes())
}
if node.FirstChild != nil {
for c := node.FirstChild; c != nil; c = c.NextSibling {
r.RenderNode(c)
}
}
if node.DataAtom == atom.Ul {
r.indent--
}
if node.DataAtom == atom.Li || node.DataAtom == atom.P {
r.FlushParagraph()
}
if cp != nil {
r.Buffer(cp.CloseBytes())
}
}
func getWriter(g *libkb.GlobalContext, w io.Writer) io.Writer {
if w == nil {
w = g.UI.GetTerminalUI().OutputWriter()
}
return w
}
func Render(g *libkb.GlobalContext, w io.Writer, m *libkb.Markup) {
if m == nil {
return
}
w = getWriter(g, w)
doc, err := Q.NewDocumentFromReader(m.ToReader())
if err != nil {
g.UI.GetTerminalUI().Printf("Cannot render markup: %s\n", err)
return
}
renderer := NewRenderer(g, w)
renderer.RenderNodes(doc.Nodes)
}
func RenderText(g *libkb.GlobalContext, w io.Writer, txt keybase1.Text) {
w = getWriter(g, w)
if !txt.Markup {
w.Write([]byte(txt.Data))
} else {
Render(g, w, libkb.NewMarkup(txt.Data))
}
}
// BoolString maps a bool to true and false strings (like "yes"
// and "no"). For example: BoolString(b, "yes", "no").
func BoolString(b bool, t, f string) string {
if b {
return t
}
return f
}