-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
categoryctl.go
143 lines (128 loc) · 4.88 KB
/
categoryctl.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
// Pipe - A small and beautiful blogging platform written in golang.
// Copyright (C) 2017-2018, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package controller
import (
"html/template"
"math"
"net/http"
"strconv"
"strings"
"github.com/b3log/pipe/i18n"
"github.com/b3log/pipe/model"
"github.com/b3log/pipe/service"
"github.com/b3log/pipe/util"
"github.com/gin-gonic/gin"
"github.com/vinta/pangu"
)
func showCategoriesAction(c *gin.Context) {
dataModel := getDataModel(c)
blogID := getBlogID(c)
locale := getLocale(c)
categoryModels := service.Category.GetCategories(math.MaxInt8, blogID)
var themeCategories []*model.ThemeCategory
for _, categoryModel := range categoryModels {
var themeTags []*model.ThemeTag
tagStrs := strings.Split(categoryModel.Tags, ",")
for _, tagTitle := range tagStrs {
themeTag := &model.ThemeTag{
Title: tagTitle,
URL: getBlogURL(c) + util.PathTags + "/" + tagTitle,
}
themeTags = append(themeTags, themeTag)
}
themeCategory := &model.ThemeCategory{
Title: categoryModel.Title,
URL: getBlogURL(c) + util.PathCategories + categoryModel.Path,
Description: categoryModel.Description,
Tags: themeTags,
ArticleCount: 8, // TODO: category article count
}
themeCategories = append(themeCategories, themeCategory)
}
dataModel["Categories"] = themeCategories
dataModel["Title"] = i18n.GetMessage(locale, "categories") + " - " + dataModel["Title"].(string)
c.HTML(http.StatusOK, getTheme(c)+"/categories.html", dataModel)
}
func showCategoryArticlesArticlesAction(c *gin.Context) {
dataModel := getDataModel(c)
blogID := getBlogID(c)
locale := getLocale(c)
session := util.GetSession(c)
page := util.GetPage(c)
categoryPath := strings.SplitAfter(c.Request.URL.Path, util.PathCategories)[1]
categoryModel := service.Category.GetCategoryByPath(categoryPath, blogID)
if nil == categoryModel {
notFound(c)
return
}
articleListStyleSetting := service.Setting.GetSetting(model.SettingCategoryPreference, model.SettingNamePreferenceArticleListStyle, blogID)
articleModels, pagination := service.Article.GetCategoryArticles(categoryModel.ID, page, blogID)
var articles []*model.ThemeArticle
for _, articleModel := range articleModels {
var themeTags []*model.ThemeTag
tagStrs := strings.Split(articleModel.Tags, ",")
for _, tagStr := range tagStrs {
themeTag := &model.ThemeTag{
Title: tagStr,
URL: getBlogURL(c) + util.PathTags + "/" + tagStr,
}
themeTags = append(themeTags, themeTag)
}
authorModel := service.User.GetUser(articleModel.AuthorID)
if nil == authorModel {
logger.Errorf("not found author of article [id=%d, authorID=%d]", articleModel.ID, articleModel.AuthorID)
continue
}
author := &model.ThemeAuthor{
Name: authorModel.Name,
URL: getBlogURL(c) + util.PathAuthors + "/" + authorModel.Name,
AvatarURL: authorModel.AvatarURL,
}
mdResult := util.Markdown(articleModel.Content)
abstract := template.HTML("")
thumbnailURL := mdResult.ThumbURL
if strconv.Itoa(model.SettingPreferenceArticleListStyleValueTitleAbstract) == articleListStyleSetting.Value {
abstract = template.HTML(mdResult.AbstractText)
}
if strconv.Itoa(model.SettingPreferenceArticleListStyleValueTitleContent) == articleListStyleSetting.Value {
abstract = template.HTML(mdResult.ContentHTML)
thumbnailURL = ""
}
article := &model.ThemeArticle{
ID: articleModel.ID,
Abstract: abstract,
Author: author,
CreatedAt: articleModel.CreatedAt.Format("2006-01-02"),
Title: pangu.SpacingText(articleModel.Title),
Tags: themeTags,
URL: getBlogURL(c) + articleModel.Path,
Topped: articleModel.Topped,
ViewCount: articleModel.ViewCount,
CommentCount: articleModel.CommentCount,
ThumbnailURL: thumbnailURL,
Editable: session.UID == authorModel.ID,
}
articles = append(articles, article)
}
dataModel["Articles"] = articles
dataModel["Pagination"] = pagination
dataModel["Category"] = &model.ThemeCategory{
Title: categoryModel.Title,
ArticleCount: pagination.RecordCount,
}
dataModel["Title"] = categoryModel.Title + " - " + i18n.GetMessage(locale, "categories") + " - " + dataModel["Title"].(string)
c.HTML(http.StatusOK, getTheme(c)+"/category-articles.html", dataModel)
}