Repository navigation
Expand file tree
/
Copy pathtags.go
More file actions
94 lines (75 loc) · 2.1 KB
/
Copy pathtags.go
File metadata and controls
94 lines (75 loc) · 2.1 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
// Package tags provides and implements the tags plugin.
package tags
import (
"path/filepath"
"strings"
"sync"
"github.com/verless/verless/model"
"github.com/verless/verless/tree"
)
const (
// tagsDir is the target directory for all tag directories.
tagsDir string = "/tags"
)
// New creates a new tags plugin that uses templates from the given
// build path and outputs the tag directories to outputDir.
func New() *tags {
t := tags{
tags: make(map[string]*model.ListPage),
}
return &t
}
// tags is the actual tags plugin that maintains a map with all
// tags from all processed pages.
type tags struct {
tags map[string]*model.ListPage
tagsMutex sync.Mutex
}
// ProcessPage creates a new map entry for each tag in the processed
// page and adds the page to the entry's list page.
func (t *tags) ProcessPage(page *model.Page) error {
for _, tag := range page.Tags {
// Sanitizing the tags like "Making Coffee" to "making-coffee".
tag.Name = strings.Replace(tag.Name, " ", "-", -1)
tag.Name = strings.ToLower(tag.Name)
t.tagsMutex.Lock()
_, tagExists := t.tags[tag.Name]
if !tagExists {
t.createListPage(tag.Name)
}
t.tags[tag.Name].Pages = append(t.tags[tag.Name].Pages, page)
t.tagsMutex.Unlock()
}
return nil
}
// PreWrite registers each list page in the site model. Those list
// pages will be rendered by the writer.
func (t *tags) PreWrite(site *model.Site) error {
node := model.NewNode()
node.ListPage.Route = tagsDir
if err := tree.CreateNode(tagsDir, site.Root, node); err != nil {
return err
}
for tag, listPage := range t.tags {
path := filepath.ToSlash(filepath.Join(tagsDir, tag))
node := model.NewNode()
node.ListPage = *listPage
if err := tree.CreateNode(path, site.Root, node); err != nil {
return err
}
}
return nil
}
// PostWrite isn't needed by the tags plugin.
func (t *tags) PostWrite() error {
return nil
}
// createListPage initializes a new list page for a given key.
func (t *tags) createListPage(key string) {
t.tags[key] = &model.ListPage{
Pages: make([]*model.Page, 0),
Page: model.Page{
Route: tagsDir + "/" + key,
},
}
}