generated from bmf-san/gobel-client-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
211 lines (189 loc) · 6.8 KB
/
post.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
package presenter
import (
"bytes"
"html/template"
"net/http"
"os"
"github.com/bmf-san/bmf-tech-client/app/model"
)
// PostIndex is a data for index template.
type PostIndex struct {
Posts *model.Posts
Pagination *Pagination
}
// PostIndexBySearch is a data for index template by search.
type PostIndexBySearch struct {
Keyword string
Posts *model.Posts
Pagination *Pagination
}
// PostIndexByCategory is a data for index template by category.
type PostIndexByCategory struct {
CategoryName string
Posts *model.Posts
Pagination *Pagination
}
// PostIndexByTag is a data for index template by tag.
type PostIndexByTag struct {
TagName string
Posts *model.Posts
Pagination *Pagination
}
// PostShow is a data for show template.
type PostShow struct {
Post *model.Post
TwitterShareURL string
FacebookShareURL string
HatenaShareURL string
SupportURL string
}
// ExecutePostIndex responses a index template.
func (pt *Presenter) ExecutePostIndex(buf *bytes.Buffer, r *http.Request, p *PostIndex) (*bytes.Buffer, error) {
fm := template.FuncMap{
"year": pt.year,
"striptags": pt.StripTags,
"summary": pt.Summary,
"isAd": pt.IsAd,
}
u := os.Getenv("BASE_URL") + "/posts"
m := &model.Meta{
Title: "bmf-tech.com - 記事一覧",
Canonical: u,
Description: "bmf-tech.comの記事一覧ページです。",
OGTitle: "bmf-tech.com - 記事一覧",
OGDescription: "bmf-tech.comの記事一覧ページです。",
OGURL: u,
OGType: "article",
OGImage: "",
OGSiteName: "bmf-tech",
OGLocale: "ja_JP",
TwitterCard: "summary",
TwitterSite: "@bmf_san",
NoIndex: false,
}
tpl := template.Must(template.New("base").Funcs(fm).ParseFS(pt.templates, "templates/layout/base.tpl", "templates/partial/meta.tpl", "templates/post/index.tpl", "templates/partial/pagination.tpl", "templates/partial/posts.tpl", "templates/partial/search.tpl"))
if err := tpl.ExecuteTemplate(buf, "base", map[string]interface{}{"Meta": m, "Posts": p}); err != nil {
return nil, err
}
return buf, nil
}
// ExecutePostIndexByKeyword responses a index template by keyword.
func (pt *Presenter) ExecutePostIndexByKeyword(buf *bytes.Buffer, r *http.Request, p *PostIndexBySearch) (*bytes.Buffer, error) {
fm := template.FuncMap{
"year": pt.year,
"striptags": pt.StripTags,
"summary": pt.Summary,
"isAd": pt.IsAd,
}
u := os.Getenv("BASE_URL") + "/posts"
m := &model.Meta{
Title: "bmf-tech.com - 検索記事一覧",
Canonical: u,
Description: "検索記事一覧:" + p.Keyword,
OGTitle: "検索記事一覧:" + p.Keyword,
OGDescription: "検索記事一覧:" + p.Keyword,
OGURL: u,
OGType: "article",
OGImage: "",
OGSiteName: "bmf-tech",
OGLocale: "ja_JP",
TwitterCard: "summary",
TwitterSite: "@bmf_san",
NoIndex: false,
}
tpl := template.Must(template.New("base").Funcs(fm).ParseFS(pt.templates, "templates/layout/base.tpl", "templates/partial/meta.tpl", "templates/post/search.tpl", "templates/partial/pagination.tpl", "templates/partial/posts.tpl", "templates/partial/search.tpl"))
if err := tpl.ExecuteTemplate(buf, "base", map[string]interface{}{"Meta": m, "Posts": p}); err != nil {
return nil, err
}
return buf, nil
}
// ExecutePostIndexByCategory responses a index template by category.
func (pt *Presenter) ExecutePostIndexByCategory(buf *bytes.Buffer, r *http.Request, p *PostIndexByCategory) (*bytes.Buffer, error) {
fm := template.FuncMap{
"year": pt.year,
"striptags": pt.StripTags,
"summary": pt.Summary,
"isAd": pt.IsAd,
}
u := os.Getenv("BASE_URL") + "/posts/categories/" + p.CategoryName
m := &model.Meta{
Title: "bmf-tech.com - カテゴリ別記事一覧",
Canonical: u,
Description: "カテゴリ別記事一覧:" + p.CategoryName,
OGTitle: "カテゴリ別記事一覧:" + p.CategoryName,
OGDescription: "カテゴリ別記事一覧:" + p.CategoryName,
OGURL: u,
OGType: "article",
OGImage: "",
OGSiteName: "bmf-tech",
OGLocale: "ja_JP",
TwitterCard: "summary",
TwitterSite: "@bmf_san",
NoIndex: false,
}
tpl := template.Must(template.New("base").Funcs(fm).ParseFS(pt.templates, "templates/layout/base.tpl", "templates/partial/meta.tpl", "templates/post/category.tpl", "templates/partial/pagination.tpl", "templates/partial/posts.tpl"))
if err := tpl.ExecuteTemplate(buf, "base", map[string]interface{}{"Meta": m, "Posts": p}); err != nil {
return nil, err
}
return buf, nil
}
// ExecutePostIndexByTag responses a index template by tag.
func (pt *Presenter) ExecutePostIndexByTag(buf *bytes.Buffer, r *http.Request, p *PostIndexByTag) (*bytes.Buffer, error) {
fm := template.FuncMap{
"year": pt.year,
"striptags": pt.StripTags,
"summary": pt.Summary,
"isAd": pt.IsAd,
}
u := os.Getenv("BASE_URL") + "/posts/tags/" + p.TagName
m := &model.Meta{
Title: "bmf-tech.com - タグ別記事一覧",
Canonical: u,
Description: "タグ別記事一覧:" + p.TagName,
OGTitle: "タグ別記事一覧:" + p.TagName,
OGDescription: "タグ別記事一覧:" + p.TagName,
OGURL: u,
OGType: "article",
OGImage: "",
OGSiteName: "bmf-tech",
OGLocale: "ja_JP",
TwitterCard: "summary",
TwitterSite: "@bmf_san",
NoIndex: false,
}
tpl := template.Must(template.New("base").Funcs(fm).ParseFS(pt.templates, "templates/layout/base.tpl", "templates/partial/meta.tpl", "templates/post/tag.tpl", "templates/partial/pagination.tpl", "templates/partial/posts.tpl"))
if err := tpl.ExecuteTemplate(buf, "base", map[string]interface{}{"Meta": m, "Posts": p}); err != nil {
return nil, err
}
return buf, nil
}
// ExecutePostShow responses a show template by tag.
func (pt *Presenter) ExecutePostShow(buf *bytes.Buffer, r *http.Request, p *PostShow) (*bytes.Buffer, error) {
fm := template.FuncMap{
"year": pt.year,
"unescape": pt.Unescape,
"isAd": pt.IsAd,
}
s := pt.Summary(pt.StripTags(p.Post.HTMLBody))
u := os.Getenv("BASE_URL") + "/posts/" + p.Post.Title
m := &model.Meta{
Title: "bmf-tech.com - " + p.Post.Title,
Canonical: u,
Description: s,
OGTitle: p.Post.Title,
OGDescription: s,
OGURL: u,
OGType: "article",
OGImage: "",
OGSiteName: "bmf-tech",
OGLocale: "ja_JP",
TwitterCard: "summary",
TwitterSite: "@bmf_san",
NoIndex: false,
}
tpl := template.Must(template.New("base").Funcs(fm).ParseFS(pt.templates, "templates/layout/base.tpl", "templates/partial/meta.tpl", "templates/post/show.tpl"))
if err := tpl.ExecuteTemplate(buf, "base", map[string]interface{}{"Meta": m, "Post": p}); err != nil {
return nil, err
}
return buf, nil
}