summaryrefslogtreecommitdiffstats
path: root/selector.go
blob: ff1e7c15637eb6c405bc8ee413c2603615b1c4ff (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package cnm

import (
	"errors"
	"net/url"
	"reflect"
	"strconv"
	"strings"
)

// Select selects content from the document based on an extended CNM selector.
//
// See ContNet draft/cnm-selector for more information about content selectors.
//
// This function may create copies of the document and all container content
// blocks, but includes non-container blocks untouched in the copy.
func (doc *Document) Select(selector string) (*Document, error) {
	deep := true
	if len(selector) > 0 && selector[0] == '!' {
		deep = false
		selector = selector[1:]
	}

	if len(selector) == 0 {
		return duplicateBlock(doc, true, deep).(*Document), nil
	}

	indices, block, err := doc.SelectBlock(selector)
	if err != nil || block == nil {
		return nil, err
	}

	d := NewDocument()
	if block := selectContent(doc.Content, indices, deep); block != nil {
		d.Content = block.(*ContentBlock)
	}

	return d, nil
}

// SelectIndex selects a content block based on an index path.
//
// For example, indices=[]int{1, 4, 2} selects
// doc.Content.Children()[1].Children()[4].Children(2) if it exists, otherwise
// returns nil. Note that this is *not* the same as a CNM index selector.
func (doc *Document) SelectIndex(indices ...int) (block Block) {
	return SelectIndex(doc.Content, indices...)
}

// SelectBlock selects section blocks from the document with a selector query.
//
// Returns the block index path within content blocks, the target block and, if
// the query was invalid, an error. All are nil when no block was found with a
// valid selector.
//
// See ContNet draft/cnm-selector for more information.
func (doc *Document) SelectBlock(selector string) (indices []int, block Block, err error) {
	if len(selector) == 0 {
		block = doc
		indices = []int{}
		return
	}

	switch selector[0] {
	case '/':
		indices, block, err = doc.selectPath(selector[1:])
	case '#':
		indices, block, err = doc.selectTitle(selector[1:])
	case '$':
		indices, block, err = doc.selectSectionIndex(selector[1:])
	default:
		err = errors.New("cnm-go: invalid selector")
	}

	return
}

func (doc *Document) selectPath(selector string) (indices []int, block Block, err error) {
	ss := []string{}
	if selector != "" {
		ss = strings.Split(selector, "/")
		for i := range ss {
			ss[i], err = url.PathUnescape(ss[i])
			if err != nil {
				return
			}
		}
	}
	indices, block = selectSearch(doc.Content, ss, false)
	return
}

func (doc *Document) selectTitle(selector string) (indices []int, block Block, err error) {
	if strings.ContainsRune(selector, '/') {
		err = errors.New("cnm-go: invalid title selector")
		return
	}
	selector, err = url.PathUnescape(selector)
	if err != nil {
		return
	}
	ss := []string{}
	if selector != "" {
		ss = []string{selector}
	}
	indices, block = selectSearch(doc.Content, ss, true)
	return
}

func (doc *Document) selectSectionIndex(selector string) (indices []int, block Block, err error) {
	secs := []int{}
	if selector != "" {
		for _, s := range strings.Split(selector, ".") {
			var n uint64
			n, err = strconv.ParseUint(s, 10, 31)
			if err != nil {
				return
			}
			secs = append(secs, int(n))
		}
	}
	indices, block = selectSectionIndex(doc.Content, secs)
	return
}

func selectSearch(block Block, titles []string, any bool) ([]int, Block) {
	if len(titles) == 0 {
		return []int{}, block
	}

	if bl, ok := block.(*SectionBlock); ok && bl.Title() != "" {
		if len(titles) == 1 && bl.Title() == titles[0] {
			return []int{}, bl
		}
		if any || bl.Title() == titles[0] {
			if !any {
				titles = titles[1:]
			}
			for i, c := range bl.Children() {
				if p, b := selectSearch(c, titles, any); b != nil {
					return append([]int{i}, p...), b
				}
			}
		}
	} else if bl, ok := block.(ContainerBlock); ok {
		for i, c := range bl.Children() {
			if p, b := selectSearch(c, titles, any); b != nil {
				return append([]int{i}, p...), b
			}
		}
	}

	return nil, nil
}

func selectSectionIndex(block Block, secs []int) ([]int, Block) {
	if len(secs) == 0 {
		return []int{}, block
	}
	if bl, ok := block.(ContainerBlock); ok {
		for i, c := range bl.Children() {
			if secs[0] < 1 {
				return nil, nil
			}
			if cb, ok2 := c.(*SectionBlock); ok2 && cb.Title() != "" {
				secs[0]-- // starts with 1 for first section
				if secs[0] == 0 {
					if len(secs) == 1 {
						return []int{i}, cb
					}
					p, b := selectSectionIndex(cb, secs[1:])
					if b == nil {
						return nil, nil
					}
					return append([]int{i}, p...), b
				}
			} else if _, ok2 := c.(ContainerBlock); ok2 {
				p, b := selectSectionIndex(c, secs)
				if b != nil {
					return append([]int{i}, p...), b
				}
			}
		}
	}
	return nil, nil
}

func selectContent(block Block, indices []int, deep bool) Block {
	if indices == nil {
		return nil
	}
	if len(indices) == 0 {
		return duplicateBlock(block, true, deep)
	}
	if bl, ok := block.(ContainerBlock); ok {
		ch := bl.Children()
		if len(ch) <= indices[0] {
			return nil
		}
		b := duplicateBlock(block, false, true).(ContainerBlock)
		c := selectContent(ch[indices[0]], indices[1:], deep)
		if c != nil {
			b.AppendChild(c)
		}
		return b
	}
	return nil
}

func duplicateBlock(block Block, deep, sections bool) Block {
	if bl, ok := block.(*Document); ok {
		doc := &Document{
			Title: bl.Title,
			Links: bl.Links,
			Site:  bl.Site,
		}
		if deep {
			doc.Content = duplicateBlock(bl.Content, deep, sections).(*ContentBlock)
		}
		return doc
	}
	if _, ok := block.(ContainerBlock); !ok {
		return block
	}

	var cb ContainerBlock

	switch bl := block.(type) {
	case *ContentBlock:
		cb = NewContentBlock(bl.Name(), bl.Args()...)
	case *HeaderBlock:
		cb = NewHeaderBlock()
	case *ListBlock:
		cb = NewListBlock(bl.Ordered())
	case *RowBlock:
		cb = NewRowBlock()
	case *SectionBlock:
		cb = NewSectionBlock(bl.Title())
	case *TableBlock:
		cb = NewTableBlock()
	default: // XXX
		//return nil
		val := reflect.New(reflect.TypeOf(bl).Elem())
		cb = val.Interface().(ContainerBlock)
	}

	if cb != nil && deep {
		for _, ch := range block.(ContainerBlock).Children() {
			sb, isSection := ch.(*SectionBlock)
			cb.AppendChild(duplicateBlock(ch, sections || (!isSection || sb.Title() == ""), sections))
		}
	}

	return cb
}

// SelectIndex selects a child block based on an index path.
//
// Note that this is *not* the same as a CNM section index selector.
func SelectIndex(block Block, indices ...int) Block {
	if indices == nil {
		return nil
	}
	for len(indices) > 0 {
		if bl, ok := block.(ContainerBlock); ok {
			ch := bl.Children()
			if len(ch) <= indices[0] {
				return nil
			}
			block = ch[indices[0]]
			indices = indices[1:]
		} else {
			return nil
		}
	}
	return block
}