-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline.go
84 lines (72 loc) · 1.31 KB
/
inline.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
package ftml
import (
"fmt"
"strings"
"unicode/utf8"
)
type InlineStyle uint8
const (
StyleNone InlineStyle = iota
StyleBold
StyleItalic
StyleHighlight
StyleUnderline
StyleStrike
StyleLink
StyleCode
)
func (s InlineStyle) String() string {
switch s {
case StyleNone:
return "text"
case StyleBold:
return "bold"
case StyleItalic:
return "italic"
case StyleUnderline:
return "underline"
case StyleStrike:
return "striked"
case StyleHighlight:
return "highlight"
case StyleLink:
return "link"
case StyleCode:
return "code"
}
panic("Unknown Inline Style")
}
type Span struct {
Style InlineStyle
Text string
LinkTarget string
Children []Span
}
func (s *Span) EndsWithLineBreak() bool {
if l := len(s.Children); l > 0 {
return s.Children[l-1].EndsWithLineBreak()
}
return len(s.Text) > 0 && s.Text[len(s.Text)-1] == '\n'
}
func (s *Span) Width() int {
l := utf8.RuneCountInString(s.Text)
for _, i := range s.Children {
l += i.Width()
}
return l
}
func (s *Span) String() string {
b := &strings.Builder{}
if len(s.Children) > 0 {
b.WriteString(fmt.Sprintf("[%s:", s.Style))
for _, i := range s.Children {
b.WriteString(i.String())
}
b.WriteString("]")
} else {
b.WriteString("‘")
b.WriteString(s.Text)
b.WriteString("’")
}
return b.String()
}