reprogl

Форк
0
/
extensions.go 
270 строк · 5.9 Кб
1
package views
2

3
import (
4
	"fmt"
5
	"html/template"
6
	"math/rand"
7
	"strings"
8
	"time"
9

10
	"xelbot.com/reprogl/container"
11
	"xelbot.com/reprogl/models"
12
	"xelbot.com/reprogl/utils"
13
	"xelbot.com/reprogl/utils/hashid"
14
	"xelbot.com/reprogl/views/style"
15
)
16

17
const RegionalIndicatorOffset = 127397
18

19
func rawHTML(s string) template.HTML {
20
	return template.HTML(s)
21
}
22

23
func urlGenerator(routeName string, pairs ...string) string {
24
	return container.GenerateURL(routeName, pairs...)
25
}
26

27
func absUrlGenerator(routeName string, pairs ...string) string {
28
	return container.GenerateAbsoluteURL(routeName, pairs...)
29
}
30

31
func tags(tl models.TagList) template.HTML {
32
	var s string
33
	if len(tl) > 0 {
34
		s = "Теги: "
35
		links := make([]string, len(tl))
36
		for i, t := range tl {
37
			links[i] = fmt.Sprintf(
38
				"<a href=\"%s\">%s</a>",
39
				urlGenerator("tag-first", "slug", t.Slug),
40
				t.Name,
41
			)
42
		}
43

44
		s += strings.Join(links, ", ")
45
	}
46

47
	return template.HTML(s)
48
}
49

50
func nl2br(s string) string {
51
	return strings.Replace(s, "\n", "<br/>", -1)
52
}
53

54
func authorBio() string {
55
	emoij := []rune{
56
		rune(0x1F41C), // ant
57
		rune(0x1FAB0), // fly
58
		rune(0x1F41D), // bee
59
		rune(0x1F980), // crab
60
		rune(0x1F997), // cricket
61
		rune(0x1F577), // spider
62
		rune(0x1F982), // scorpion
63
		rune(0x1F990), // shrimp
64
	}
65

66
	return container.GetConfig().Author.Bio + " " + string(emoij[rand.Intn(len(emoij))])
67
}
68

69
func authorDataPart(item string) (str string) {
70
	author := container.GetConfig().Author
71

72
	switch item {
73
	case "name":
74
		str = author.FullName
75
	case "github":
76
		str = fmt.Sprintf("https://github.com/%s", author.GithubUser)
77
	case "telegram":
78
		str = fmt.Sprintf("https://t.me/%s/", author.TelegramChannel)
79
	case "mastodon":
80
		str = author.MastodonLink
81
	case "gitverse":
82
		str = fmt.Sprintf("https://gitverse.ru/%s", author.GitVerseUser)
83
	default:
84
		str = "N/A"
85
	}
86

87
	return
88
}
89

90
func authorLocation() template.HTML {
91
	location := container.GetConfig().AuthorLocationRu
92
	s := make([]string, 0, 3)
93

94
	if location.City != "" {
95
		s = append(s, "<span class=\"locality\">"+location.City+"</span>")
96
	}
97
	if location.Region != "" {
98
		s = append(s, "<span class=\"region\">"+location.Region+"</span>")
99
	}
100
	if location.Country != "" {
101
		s = append(s, "<span class=\"country-name\">"+location.Country+"</span>")
102
	}
103

104
	return template.HTML(strings.Join(s, ", "))
105
}
106

107
func authorJob() template.HTML {
108
	jobs := container.GetConfig().Jobs
109
	job := jobs.Last()
110
	s := fmt.Sprintf(
111
		"<span class=\"title\">%s</span> в <a class=\"org\" href=\"%s\">%s</a>",
112
		job.Title,
113
		job.Link,
114
		job.Company,
115
	)
116

117
	return template.HTML(s)
118
}
119

120
func authorAvatar() string {
121
	return models.AvatarLink(1, hashid.Male|hashid.User, 200)
122
}
123

124
func renderESI(routeName string, pairs ...string) template.HTML {
125
	s := fmt.Sprintf(
126
		"<esi:include src=\"%s\" onerror=\"continue\"/>",
127
		urlGenerator(routeName, pairs...),
128
	)
129

130
	return template.HTML(s)
131
}
132

133
func subString(input string, length int) (str string) {
134
	symbols := []rune(input)
135

136
	if len(symbols) >= length {
137
		str = string(symbols[:length-3]) + "..."
138
	} else {
139
		str = input
140
	}
141

142
	return
143
}
144

145
func timeTag(t time.Time) template.HTML {
146
	var s = "<time class=\"post-date\" datetime=\"" +
147
		t.Format(time.RFC3339) + "\">" +
148
		t.Format("2 ") +
149
		utils.RuMonthName(t.Month(), true) +
150
		t.Format(" 2006, 15:04:05.000") +
151
		"</time>"
152

153
	return template.HTML(s)
154
}
155

156
func commentsCountString(cnt int) (str string) {
157
	modulo := cnt % 10
158
	if modulo == 1 {
159
		str = fmt.Sprintf("%d комментарий", cnt)
160
	}
161

162
	if modulo > 1 && modulo < 5 {
163
		str = fmt.Sprintf("%d комментария", cnt)
164
	}
165

166
	if modulo > 4 || modulo == 0 {
167
		str = fmt.Sprintf("%d комментариев", cnt)
168
	}
169

170
	modulo100 := cnt % 100
171
	if modulo100 >= 11 && modulo100 <= 14 {
172
		str = fmt.Sprintf("%d комментариев", cnt)
173
	}
174

175
	return
176
}
177

178
func timesCountString(cnt int) (str string) {
179
	modulo := cnt % 10
180
	if modulo == 1 {
181
		str = fmt.Sprintf("%d раз", cnt)
182
	}
183

184
	if modulo > 1 && modulo < 5 {
185
		str = fmt.Sprintf("%d раза", cnt)
186
	}
187

188
	if modulo > 4 || modulo == 0 {
189
		str = fmt.Sprintf("%d раз", cnt)
190
	}
191

192
	modulo100 := cnt % 100
193
	if modulo100 >= 12 && modulo100 <= 14 {
194
		str = fmt.Sprintf("%d раз", cnt)
195
	}
196

197
	return
198
}
199

200
func flagCounterImage(fullSize bool) func() template.HTML {
201
	var (
202
		url  string
203
		w, h int
204
	)
205

206
	w = 162
207
	h = 82
208
	cdn := container.GetConfig().CDNBaseURL
209
	if container.IsDevMode() {
210
		url = cdn + "/images/flagcounter.png"
211
		if !fullSize {
212
			url = cdn + "/images/flagcounter_mini.png"
213
			w = 160
214
			h = 20
215
		}
216
	} else {
217
		url = "//s05.flagcounter.com/count2/D9g3/bg_23222D/txt_FFFFFF/border_FFFFFF/columns_2/maxflags_4/viewers_3/labels_0/pageviews_1/flags_0/percent_1/"
218
		if !fullSize {
219
			url = "//s05.flagcounter.com/mini/D9g3/bg_23222D/txt_FFFFFF/border_23222D/flags_0/"
220
			w = 160
221
			h = 20
222
		}
223
	}
224

225
	return func() template.HTML {
226
		return template.HTML(
227
			fmt.Sprintf("<img src=\"%s\" alt=\"Free counters!\" width=\"%d\" height=\"%d\">", url, w, h),
228
		)
229
	}
230
}
231

232
func emojiFlag(countryCode string) string {
233
	if countryCode == "-" {
234
		// https://apps.timwhitlock.info/unicode/inspect?s=%F0%9F%8F%B4%E2%80%8D%E2%98%A0%EF%B8%8F
235
		return string([]rune{'\U0001F3F4', '\u200D', '\u2620', '\uFE0F'})
236
	}
237

238
	if len(countryCode) != 2 {
239
		return countryCode
240
	}
241

242
	countryCode = strings.ToUpper(countryCode)
243

244
	resultBytes := make([]rune, 0, 2)
245
	for _, b := range []byte(countryCode) {
246
		resultBytes = append(resultBytes, rune(RegionalIndicatorOffset+int(b)))
247
	}
248

249
	return string(resultBytes)
250
}
251

252
func articleStyles(article *models.Article, acceptAvif, acceptWebp bool) template.HTML {
253
	return template.HTML(style.GenerateArticleStyles(article, acceptAvif, acceptWebp))
254
}
255

256
func statisticsStyles() template.HTML {
257
	return template.HTML(style.GenerateStatisticsStyles())
258
}
259

260
func indexStyles() template.HTML {
261
	return template.HTML(style.GenerateIndexStyles())
262
}
263

264
func infoStyles() template.HTML {
265
	return template.HTML(style.GenerateInfoStyles())
266
}
267

268
func profileStyles() template.HTML {
269
	return template.HTML(style.GenerateProfileStyles())
270
}
271

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.