-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockobj.go
More file actions
199 lines (171 loc) · 3.85 KB
/
blockobj.go
File metadata and controls
199 lines (171 loc) · 3.85 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
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
package foxmarks
import (
"fmt"
"strconv"
"strings"
)
// BlockObject is an interface for all kinds of block objects.
// In Markdown, all formatting is based around strings -
// so strings are the primitive in Markdown.
type BlockObject struct {
Type BlockObjectType
Initialized bool // indicates if a block object is currently being initalized or not
Content string // the content of a block object, in string format. This is the raw string parsed by a constructor.
Inlines []*InlineObject // whatever inline objects a block object may have
Blocks []*BlockObject // some blocks can contain more blocks - if so, then a blockobject will contain these blocks.
}
type BlockObjectType int
const (
Generic BlockObjectType = iota // generics are placeholders
Paragraph
Header1
Header2
Header3
Header4
Header5
Header6
ThematicBreak
List
ListItem
CodeBlock
)
func (b *BlockObject) appendContent(i interface{}) error {
switch v := i.(type) {
case rune:
b.Content = b.Content + string(v)
case string:
b.Content = b.Content + v
default:
return fmt.Errorf("invalid type to append to BlockObject: %T", v)
}
return nil
}
func (b *BlockObject) appendObject(i interface{}) error {
switch o := i.(type) {
case *BlockObject:
b.Blocks = append(b.Blocks, o)
case *InlineObject:
b.Inlines = append(b.Inlines, o)
default:
return fmt.Errorf("invalid object to append to BlockObject: %T", o)
}
return nil
}
func NewBlockObject(t BlockObjectType, i bool) *BlockObject {
return &BlockObject{
Type: t,
Initialized: i,
}
}
func NewGeneric() *BlockObject {
return &BlockObject{
Type: Generic,
Initialized: false,
}
}
func NewParagraph() *BlockObject {
return &BlockObject{
Type: Paragraph,
Initialized: true,
}
}
func NewHeader() *BlockObject {
return NewGeneric()
}
func NewThematicBreak() *BlockObject {
return &BlockObject{
Type: ThematicBreak,
Initialized: false,
}
}
// listOffsets is a way to encode how many 'spaces' a list needs
// before it is considered a part of a list structure
//
// pre indicates how many prefix spaces there are
// op indicates the length of the operator
// suf indicates how many suffix spaces there are
//
// for example:
//
// [* ] would have listOffsets pre: 0, op: 2, suf: 1
// [ - ] would have listOffsets pre: 1, op: 2, suf: 3
// [ 1. ] would have liftOffsets pre: 2, op: 3, suf: 2
//
// if a ListItem immediately precedes a newline char,
// there can only be precursor spaces equivalent to:
// - for a new item: pre
// - to continue the previous item: pre + op + suf
// relevant to the root/first item in the list
//
// otherwise, it automatically closes the entire list
//
// list examples:
// ```
// * [pre 0, op 2]
// * [pre 0, op 2]
// * [pre 0, op 2]
// ```
//
// ```
// * [pre 0, op 2, suf 0]
// * [pre 2, op 2, suf 0]
// * [pre 4, op 2, suf 0]
// ```
type ListOrder int
const (
Unordered ListOrder = 0
Ordered ListOrder = 1
)
type ListAttribs struct {
Pre int
Op string
Order ListOrder
}
// listOffsets are encoded in the List's content as fieldless CSV
func (b *BlockObject) GetListAttrib() ListAttribs {
if b.Type != List {
return ListAttribs{}
}
var a ListAttribs
v := strings.Split(b.Content,",")
a.Pre, _ = strconv.Atoi(v[0])
a.Op = v[1]
switch v[2] {
case "u":
a.Order = Unordered
case "o":
a.Order = Ordered
}
return a
}
func NewList(p int, o string, r ListOrder) *BlockObject {
l := NewBlockObject(List, false)
l.Content = strings.Join(
[]string{
strconv.Itoa(p),
o,
func () string {
switch r {
case Unordered:
return "u"
case Ordered:
return "o"
}
return ""
}(),
},
",",
)
return l
}
func NewListItem() *BlockObject {
return NewBlockObject(ListItem, false)
}
var headerLevels map[int]BlockObjectType = map[int]BlockObjectType{
1: Header1,
2: Header2,
3: Header3,
4: Header4,
5: Header5,
6: Header6,
}