-
Notifications
You must be signed in to change notification settings - Fork 15
/
inline_formats.go
170 lines (134 loc) · 2.96 KB
/
inline_formats.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
package quill
import (
"io"
"strconv"
)
// bold
type boldFormat struct{}
func (*boldFormat) Fmt() *Format {
return &Format{
Val: "strong",
Place: Tag,
}
}
func (*boldFormat) HasFormat(o *Op) bool {
return o.HasAttr("bold")
}
// italic
type italicFormat struct{}
func (*italicFormat) Fmt() *Format {
return &Format{
Val: "em",
Place: Tag,
}
}
func (*italicFormat) HasFormat(o *Op) bool {
return o.HasAttr("italic")
}
// underline
type underlineFormat struct{}
func (*underlineFormat) Fmt() *Format {
return &Format{
Val: "u",
Place: Tag,
}
}
func (*underlineFormat) HasFormat(o *Op) bool {
return o.HasAttr("underline")
}
// text color
type colorFormat struct {
c string
}
func (cf *colorFormat) Fmt() *Format {
return &Format{
Val: "color:" + cf.c + ";",
Place: Style,
}
}
func (cf *colorFormat) HasFormat(o *Op) bool {
return o.Attrs["color"] == cf.c
}
// link
type linkFormat struct {
href string
}
func (*linkFormat) Fmt() *Format { return new(Format) } // Only a wrapper.
func (lf *linkFormat) HasFormat(*Op) bool {
return false // Only a wrapper.
}
func (lf *linkFormat) Wrap() (string, string) {
return `<a href=` + strconv.Quote(lf.href) + ` target="_blank">`, "</a>"
}
func (lf *linkFormat) Open(_ []*Format, _ *Op) bool {
return true // This format will only appear when there is a "link" attribute set.
}
func (lf *linkFormat) Close(_ []*Format, o *Op, _ bool) bool {
return o.Attrs["link"] != lf.href
}
// image
type imageFormat struct {
src, alt string
}
func (*imageFormat) Fmt() *Format { return nil } // The body contains the entire element.
func (imf *imageFormat) HasFormat(o *Op) bool {
return o.Type == "image" && o.Data == imf.src
}
// imageFormat implements the FormatWriter interface.
func (imf *imageFormat) Write(buf io.Writer) {
io.WriteString(buf, "<img src=")
io.WriteString(buf, strconv.Quote(imf.src))
if imf.alt != "" {
io.WriteString(buf, " alt=")
io.WriteString(buf, strconv.Quote(imf.alt))
}
buf.Write([]byte{'>'})
}
// strikethrough
type strikeFormat struct{}
func (*strikeFormat) Fmt() *Format {
return &Format{
Val: "s",
Place: Tag,
}
}
func (*strikeFormat) HasFormat(o *Op) bool {
return o.HasAttr("strike")
}
// background
type bkgFormat struct {
c string
}
func (bf *bkgFormat) Fmt() *Format {
return &Format{
Val: "background-color:" + bf.c + ";",
Place: Style,
}
}
func (bf *bkgFormat) HasFormat(o *Op) bool {
return o.Attrs["background"] == bf.c
}
// sizeFormat is used for inline strings of named sizes such as "huge" or "small".
type sizeFormat string
func (sf sizeFormat) Fmt() *Format {
return &Format{
Val: "ql-size-" + string(sf),
Place: Class,
}
}
func (sf sizeFormat) HasFormat(o *Op) bool {
return o.Attrs["size"] == string(sf)
}
// script (sup and sub)
type scriptFormat struct {
t string
}
func (sf *scriptFormat) Fmt() *Format {
return &Format{
Val: sf.t,
Place: Tag,
}
}
func (*scriptFormat) HasFormat(o *Op) bool {
return o.HasAttr("script")
}