-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelements.go
More file actions
173 lines (159 loc) · 3.59 KB
/
elements.go
File metadata and controls
173 lines (159 loc) · 3.59 KB
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
package parser
// Element is the interface that all elements implement. It represents an
// abstraction of the different gemtext line types.
//
// Type() returns the type of the element. It can be compared with
// [ElementTypes]:
//
// func example(e p.Element) {
// if e.Type() == p.ElementTypes.Text() {
// // Do something
// }
// }
//
// Text() returns any text associated with the element. For [Text], [Heading],
// [List], and [Quote], it returns the main text of the element. For [Link], it
// returns the user-friendly link name. For [PreformatToggle], it returns the
// alt text.
//
// URL() returns a URL associated with the element. Only [Link] actually returns
// a URL, other elements will return an empty string.
//
// Level() returns the heading level associated with the element. Only [Heading]
// actually returns a heading level, other elements will return
// [HeadingLevels].NotHeading(). It can be compared with [HeadingLevels]:
//
// func example(h p.Heading) {
// if h.Level() == p.HeadingLevels.Heading() {
// // Do something
// }
// }
type Element interface {
Type() elementType
Text() string
URL() string
Level() headingLevel
}
var defaultValues = struct {
url string
level headingLevel
}{
"",
HeadingLevels.NotHeading(),
}
// Text represents a text line.
type Text struct {
text string
}
func NewText(text string) Text {
return Text{text: text}
}
func (t Text) Type() elementType {
return ElementTypes.text
}
func (t Text) Text() string {
return t.text
}
func (t Text) URL() string {
return defaultValues.url
}
func (t Text) Level() headingLevel {
return defaultValues.level
}
// Link represents a link line.
type Link struct {
text string
url string
}
func NewLink(text string, url string) Link {
return Link{text: text, url: url}
}
func (l Link) Type() elementType {
return ElementTypes.link
}
func (l Link) Text() string {
return l.text
}
func (l Link) URL() string {
return l.url
}
func (l Link) Level() headingLevel {
return defaultValues.level
}
// Heading represents a heading line.
type Heading struct {
text string
level headingLevel
}
func NewHeading(text string, level headingLevel) Heading {
return Heading{text: text, level: level}
}
func (h Heading) Type() elementType {
return ElementTypes.heading
}
func (h Heading) Text() string {
return h.text
}
func (h Heading) URL() string {
return defaultValues.url
}
func (h Heading) Level() headingLevel {
return h.level
}
// List represents a list item.
type List struct {
text string
}
func NewList(text string) List {
return List{text: text}
}
func (l List) Type() elementType {
return ElementTypes.list
}
func (l List) Text() string {
return l.text
}
func (l List) URL() string {
return defaultValues.url
}
func (l List) Level() headingLevel {
return defaultValues.level
}
// Quote represents a quote line.
type Quote struct {
text string
}
func NewQuote(text string) Quote {
return Quote{text: text}
}
func (q Quote) Type() elementType {
return ElementTypes.quote
}
func (q Quote) Text() string {
return q.text
}
func (q Quote) URL() string {
return defaultValues.url
}
func (q Quote) Level() headingLevel {
return defaultValues.level
}
// PreformatToggle represents a preformat toggle line.
type PreformatToggle struct {
text string
}
func NewPreformatToggle(text string) PreformatToggle {
return PreformatToggle{text: text}
}
func (pt PreformatToggle) Type() elementType {
return ElementTypes.preformatToggle
}
func (pt PreformatToggle) Text() string {
return pt.text
}
func (pt PreformatToggle) URL() string {
return defaultValues.url
}
func (pt PreformatToggle) Level() headingLevel {
return defaultValues.level
}