-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnode.go
377 lines (325 loc) · 8.72 KB
/
node.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2017 Atelier Disko. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
var (
// Basenames matching this pattern are considered configuration files.
NodeMetaRegexp = regexp.MustCompile(`(?i)^(index|meta)\.(json|ya?ml)$`)
// Basenames matching the pattern will be ignored when searching
// for downloadable files in the node's directory.
IgnoreDownloadsRegexp = regexp.MustCompile(`(?i)^(.*\.(js|css|md|markdown|html?|json|ya?ml)|\..*|dsk.*)$`)
// Basenames matching this pattern are considered documents.
NodeDocsRegexp = regexp.MustCompile(`(?i)^.*\.(md|markdown|html?)$`)
// Characters that are ignored when looking up an URL,
// i.e. "foo/bar baz" and "foo/barbaz" are than equal.
NodeLookupURLIgnoreChars = regexp.MustCompile(`[\s\-_]+`)
// Patterns for extracting order number and title from a node's
// path/URL segment in the form of 06_Foo. As well as for
// "slugging" the URL/path segment.
NodePathTitleRegexp = regexp.MustCompile(`^0?(\d+)[_,-]+(.*)$`)
NodePathInvalidCharsRegexp = regexp.MustCompile(`[^A-Za-z0-9-_]`)
NodePathMultipleDashRegexp = regexp.MustCompile(`-+`)
)
// Constructs a new node using its path in the filesystem. Returns a
// node instance even if uncritical errors happened. This is to not
// interrupt tree creation in many cases. Tree creation must fail once
// a bridging node cannot be constructed.
func NewNode(path string, root string) *Node {
n := &Node{
root: root,
path: path,
Children: make([]*Node, 0),
}
if err := n.loadMeta(); err != nil {
log.Print(err)
}
return n
}
// Node represents a directory inside the design definitions tree.
type Node struct {
// Absolute path to the design defintions tree root.
root string
// Absolute path to the node's directory.
path string
// The parent node. If this is the root node, left unset.
Parent *Node
// A list of children nodes.
Children []*Node
// Meta data as parsed from the node configuration file.
meta NodeMeta
}
// Loads node meta data from the first config file found. Config files
// are optional.
func (n *Node) loadMeta() error {
files, err := ioutil.ReadDir(n.path)
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() {
continue
}
if !NodeMetaRegexp.MatchString(f.Name()) {
continue
}
m, err := NewNodeMeta(filepath.Join(n.path, f.Name()))
if err != nil {
return err
}
n.meta = m
return nil
}
// No node configuration found.
return nil
}
// Returns the normalized URL path fragment, that can be used to
// address this node i.e Input/Password.
func (n Node) URL() string {
if n.root == n.path {
return ""
}
return normalizeNodeURL(strings.TrimPrefix(n.path, n.root+"/"))
}
// Returns the unnormalized URL path fragment.
func (n Node) UnnormalizedURL() string {
if n.root == n.path {
return ""
}
return strings.TrimPrefix(n.path, n.root+"/")
}
// Returns the normalized and lower cased lookup URL for this node.
func (n Node) LookupURL() string {
return NodeLookupURLIgnoreChars.ReplaceAllString(
strings.ToLower(n.URL()),
"",
)
}
// An order number, as a hint for outside sorting mechanisms.
func (n Node) Order() uint64 {
return orderNumber(filepath.Base(n.path))
}
// The node's computed title with any ordering numbers stripped off, usually for display purposes.
func (n Node) Title() string {
if n.root == n.path {
return ""
}
return removeOrderNumber(filepath.Base(n.path))
}
// Returns the full description of the node.
func (n Node) Description() string {
return n.meta.Description
}
// Returns a list of related nodes.
func (n Node) Related(get NodeGetter) []*Node {
nodes := make([]*Node, 0, len(n.meta.Related))
for _, r := range n.meta.Related {
ok, node, err := get(r)
if err != nil {
log.Printf("Skipping related in %s: %s", n.URL(), err)
continue
}
if !ok {
log.Printf("Skipping related in %s: %s not found in tree", n.URL(), r)
continue
}
nodes = append(nodes, node)
}
return nodes
}
// Returns an alphabetically sorted list of tags.
func (n Node) Tags() []string {
if n.meta.Tags == nil {
return make([]string, 0)
}
tags := n.meta.Tags
sort.Strings(tags)
return tags
}
// Returns a list of keywords terms.
func (n Node) Keywords() []string {
if n.meta.Keywords == nil {
return make([]string, 0)
}
return n.meta.Keywords
}
// Returns a list of node authors; wil use the given authors
// database to lookup information.
func (n Node) Authors(as *Authors) []*Author {
r := make([]*Author, 0)
if n.meta.Authors == nil {
return r
}
for _, email := range n.meta.Authors {
ok, author, _ := as.Get(email)
if !ok {
author = &Author{email, ""}
}
r = append(r, author)
}
return r
}
// Finds the most recently edited file in the node directory and
// returns its modified timestamp.
func (n Node) Modified() time.Time {
var modified time.Time
files, err := ioutil.ReadDir(n.path)
if err != nil {
return modified
}
for _, f := range files {
if f.IsDir() {
continue
}
if f.ModTime().After(modified) {
modified = f.ModTime()
}
}
return modified
}
func (n Node) Version() string {
return n.meta.Version
}
// Returns a node asset, given its basename.
func (n Node) Asset(name string) (*NodeAsset, error) {
path := filepath.Join(n.path, name)
f, err := os.Stat(path)
if os.IsNotExist(err) || err != nil {
return nil, err
}
if f.IsDir() {
return nil, fmt.Errorf("Accessing directory as asset: %s", path)
}
return &NodeAsset{
path: filepath.Join(n.path, f.Name()),
Name: f.Name(),
URL: filepath.Join(n.URL(), f.Name()),
}, nil
}
// Returns a list of downloadable files, this may include Sketch files
// or other binary assets. JavaScript and Stylesheets and DSK control
// files are excluded.
func (n Node) Downloads() ([]*NodeAsset, error) {
downloads := make([]*NodeAsset, 0)
files, err := ioutil.ReadDir(n.path)
if err != nil {
return downloads, err
}
for _, f := range files {
if f.IsDir() {
continue
}
if IgnoreDownloadsRegexp.MatchString(f.Name()) {
continue
}
downloads = append(downloads, &NodeAsset{
path: filepath.Join(n.path, f.Name()),
Name: f.Name(),
URL: filepath.Join(n.URL(), f.Name()),
})
}
return downloads, nil
}
// Returns a slice of documents for this node.
//
// The provided tree URL prefix will be used to resolve and make
// relative links inside the document absolute. This is usually
// something like: /api/v1/tree
func (n Node) Docs() ([]*NodeDoc, error) {
docs := make([]*NodeDoc, 0)
files, err := ioutil.ReadDir(n.path)
if err != nil {
return docs, err
}
for _, f := range files {
if f.IsDir() {
continue
}
if !NodeDocsRegexp.MatchString(f.Name()) {
continue
}
docs = append(docs, &NodeDoc{
path: filepath.Join(n.path, f.Name()),
})
}
return docs, nil
}
// Returns a list of crumbs. The last element is the current active
// one. Does not include a root node.
func (n Node) Crumbs(get NodeGetter) []*Node {
nodes := make([]*Node, 0)
parts := strings.Split(strings.TrimSuffix(n.URL(), "/"), "/")
for index, _ := range parts {
url := strings.Join(parts[:index+1], "/")
ok, node, err := get(url)
if err != nil {
log.Printf("Skipping crumb in %s: %s", n.URL(), err)
continue
}
if !ok {
log.Printf("Skipping crumb in %s: %s not found in tree", n.URL(), url)
continue
}
nodes = append(nodes, node)
}
return nodes
}
// "Prettifies" (and normalizes) given relative node URL. Idempotent
// function. Removes any order numbers, as well as leading and
// trailing slashes.
//
// /foo/bar/ -> foo/bar
// foo/02_bar -> foo/bar
func normalizeNodeURL(url string) string {
var normalized []string
for _, p := range strings.Split(url, "/") {
if p == "/" {
continue
}
p = removeOrderNumber(p)
p = NodePathInvalidCharsRegexp.ReplaceAllString(p, "-")
p = NodePathMultipleDashRegexp.ReplaceAllString(p, "-")
p = strings.Trim(p, "-")
normalized = append(normalized, p)
}
return strings.Join(normalized, "/")
}
func lookupNodeURL(url string) string {
return NodeLookupURLIgnoreChars.ReplaceAllString(
strings.ToLower(normalizeNodeURL(url)),
"",
)
}
// Finds an order number embedded into given path/URL segment and
// returns it. If none is found, returns 0.
func orderNumber(segment string) uint64 {
s := NodePathTitleRegexp.FindStringSubmatch(segment)
if len(s) > 2 {
parsed, _ := strconv.ParseUint(s[0], 10, 64)
return parsed
}
return 0
}
// Removes order numbers from path/URL segment, if present.
func removeOrderNumber(segment string) string {
s := NodePathTitleRegexp.FindStringSubmatch(segment)
if len(s) == 0 {
return segment
}
if len(s) > 2 {
return s[2]
}
return s[1]
}