-
Notifications
You must be signed in to change notification settings - Fork 0
/
page-method-setters.go
157 lines (135 loc) · 4.72 KB
/
page-method-setters.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
// Copyright (c) 2023 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package page
import (
"database/sql"
"fmt"
"strings"
"time"
"github.com/gofrs/uuid"
"github.com/go-corelibs/x-text/language"
sha "github.com/go-corelibs/shasum"
"github.com/go-enjin/be/pkg/globals"
"github.com/go-enjin/be/pkg/pages"
"github.com/go-enjin/be/types/page/matter"
)
func (p *CPage) SetType(pageType string) {
p.fields.Type = pageType
p.fields.PageMatter.Matter.SetSpecific("type", pageType)
p.fields.Context.SetSpecific("Type", pageType)
}
func (p *CPage) SetFormat(filepath string) (path string) {
if format, match := p.fields.Formats.MatchFormat(filepath); format != nil {
p.fields.Format = match
path = strings.TrimSuffix(filepath, "."+match)
} else {
p.fields.Format = "tmpl"
path = strings.TrimSuffix(filepath, ".tmpl")
}
p.fields.Context.SetSpecific("Format", p.fields.Format)
return
}
func (p *CPage) SetSlugUrl(filepath string) {
p.fields.Url, p.fields.Path, p.fields.Section, p.fields.Slug = pages.GetUrlPathSectionSlug(filepath)
if p.fields.Slug == globals.DirectoryIndexPageName {
if trimmed := strings.TrimSuffix(p.fields.Url, "/"+p.fields.Slug); trimmed == "" {
p.fields.Url = "/"
} else {
p.fields.Url = trimmed
}
}
p.fields.Context.SetSpecific("Url", p.fields.Url)
p.fields.Context.SetSpecific("Path", p.fields.Path)
p.fields.Context.SetSpecific("Section", p.fields.Section)
p.fields.Context.SetSpecific("Slug", p.fields.Slug)
}
func (p *CPage) SetTitle(title string) {
p.fields.Title = title
p.fields.Context.SetSpecific("Title", title)
}
func (p *CPage) SetDescription(description string) {
p.fields.Description = description
p.fields.Context.SetSpecific("Description", description)
}
func (p *CPage) SetLayout(layoutName string) {
p.fields.Layout = layoutName
p.fields.Context.SetSpecific("Layout", layoutName)
}
func (p *CPage) SetArchetype(archetype string) {
p.fields.Archetype = archetype
p.fields.Context.SetSpecific("Archetype", archetype)
}
func (p *CPage) SetPermalink(value string) (err error) {
if value == "" {
p.fields.Context.SetSpecific("LongLink", p.fields.Url)
p.fields.Context.SetSpecific("ShortLink", p.fields.Url)
p.fields.Context.SetSpecific("Permalink", "")
p.fields.Context.SetSpecific("Permalinked", false)
p.fields.Context.SetSpecific("PermalinkUrl", p.fields.Url)
p.fields.Context.SetSpecific("PermalinkSha", "")
p.fields.Context.SetSpecific("PermalinkLongUrl", p.fields.Url)
return
}
if id, e := uuid.FromString(value); e != nil {
err = fmt.Errorf("error parsing permalink id: %v - %v", value, e)
return
} else if sum, ee := sha.BriefSum(id.Bytes()); ee != nil {
err = fmt.Errorf("error getting permalink sha: %v - %v", id, ee)
return
} else {
p.fields.Permalink = id
p.fields.PermalinkSha = sum
p.fields.Context.SetSpecific("LongLink", "/"+p.fields.Permalink.String())
p.fields.Context.SetSpecific("ShortLink", "/"+p.fields.PermalinkSha)
p.fields.Context.SetSpecific("Permalink", id)
p.fields.Context.SetSpecific("Permalinked", true)
p.fields.Context.SetSpecific("PermalinkSha", sum)
if p.fields.Url == "/" {
p.fields.Context.SetSpecific("PermalinkUrl", p.fields.Url+p.fields.PermalinkSha)
} else {
p.fields.Context.SetSpecific("PermalinkUrl", p.fields.Url+"-"+p.fields.PermalinkSha)
}
}
return
}
func (p *CPage) SetLanguage(tag language.Tag) {
p.fields.LanguageTag = tag
p.fields.Language = p.LanguageTag().String()
p.fields.Context.Set("Language", p.fields.Language)
}
func (p *CPage) SetTranslates(url string) {
p.fields.Translates = url
p.fields.Context.SetSpecific("Translates", url)
}
func (p *CPage) SetContent(content string) {
p.fields.Content = content
}
func (p *CPage) SetFrontMatter(frontMatter string) {
p.fields.FrontMatter = frontMatter
}
func (p *CPage) SetFrontMatterType(frontMatterType matter.FrontMatterType) {
p.fields.FrontMatterType = frontMatterType
}
func (p *CPage) SetCreatedAt(at time.Time) {
p.fields.CreatedAt = at
p.fields.Context.SetSpecific("Created", at)
}
func (p *CPage) SetUpdatedAt(at time.Time) {
p.fields.UpdatedAt = at
p.fields.Context.SetSpecific("Updated", at)
}
func (p *CPage) SetDeletedAt(at sql.NullTime) {
p.fields.DeletedAt = at
p.fields.Context.SetSpecific("Deleted", at)
}