Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix(template/render): fix template footer / time

kumfo46bff9e4f9aa
diff --git a/internal/base/server/http.go b/internal/base/server/http.go
index 17e4eec3..22729570 100644
--- a/internal/base/server/http.go
+++ b/internal/base/server/http.go
@@ -5,6 +5,7 @@ import (
"io/fs"
"math"
"os"
+ "regexp"
"strconv"
"strings"
"time"
@@ -65,20 +66,41 @@ func NewHTTPServer(debug bool,
answerRouter.RegisterAnswerCmsAPIRouter(cmsauthV1)
funcMap := template.FuncMap{
+ "replaceHTMLTag": func(src string, tags ...string) string {
+ p := `(?U)<(\d+)>.+</(\d+)>`
+
+ re := regexp.MustCompile(p)
+ ms := re.FindAllStringSubmatch(src, -1)
+ for _, mi := range ms {
+ if mi[1] == mi[2] {
+ i, err := strconv.Atoi(mi[1])
+ if err != nil || len(tags) < i {
+ break
+ }
+
+ src = strings.ReplaceAll(src, mi[0], tags[i-1])
+ }
+ }
+
+ return src
+ },
+ "join": func(sep string, elems ...string) string {
+ return strings.Join(elems, sep)
+ },
"templateHTML": func(data string) template.HTML {
return template.HTML(data)
},
"translator": func(la i18n.Language, data string, params ...interface{}) string {
- // todo
- /*if len(params) > 0 && len(params)%2 == 0 {
+ trans := translator.GlobalTrans.Tr(la, data)
+
+ if len(params) > 0 && len(params)%2 == 0 {
for i := 0; i < len(params); i += 2 {
k := converter.InterfaceToString(params[i])
v := converter.InterfaceToString(params[i+1])
- data = strings.ReplaceAll(data, "{{ "+k+" }}", v)
+ trans = strings.ReplaceAll(trans, "{{ "+k+" }}", v)
}
- }*/
+ }
- trans := translator.GlobalTrans.Tr(la, data)
return trans
},
"timeFormatISO": func(tz string, timestamp int64) string {
@@ -116,7 +138,7 @@ func NewHTTPServer(debug bool,
}
if between >= 3600 && between < 3600*24 {
- h := math.Floor(float64(between / 60))
+ h := math.Floor(float64(between / 3600))
trans = translator.GlobalTrans.Tr(la, "ui.dates.x_hours_ago")
return strings.ReplaceAll(trans, "{{count}}", strconv.FormatFloat(h, 'f', 0, 64))
}
diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go
index 52ba1b16..b4a43d98 100644
--- a/internal/controller/template_controller.go
+++ b/internal/controller/template_controller.go
@@ -3,6 +3,7 @@ package controller
import (
"encoding/json"
"fmt"
+ "github.com/segmentfault/pacman/i18n"
"html/template"
"net/http"
"regexp"
@@ -78,7 +79,9 @@ func (tc *TemplateController) SiteInfo(ctx *gin.Context) *schema.TemplateSiteInf
// Index question list
func (tc *TemplateController) Index(ctx *gin.Context) {
- req := &schema.QuestionSearch{}
+ req := &schema.QuestionSearch{
+ Order: "newest",
+ }
if handler.BindAndCheck(ctx, req) {
tc.Page404(ctx)
return
@@ -101,7 +104,9 @@ func (tc *TemplateController) Index(ctx *gin.Context) {
}
func (tc *TemplateController) QuestionList(ctx *gin.Context) {
- req := &schema.QuestionSearch{}
+ req := &schema.QuestionSearch{
+ Order: "newest",
+ }
if handler.BindAndCheck(ctx, req) {
tc.Page404(ctx)
return
@@ -307,14 +312,14 @@ func (tc *TemplateController) Page404(ctx *gin.Context) {
func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteInfo *schema.TemplateSiteInfoResp, data gin.H) {
data["siteinfo"] = siteInfo
- data["scriptPath"] = tc.scriptPath
+ data["scriptPath"] = "" //tc.scriptPath
data["cssPath"] = tc.cssPath
data["keywords"] = siteInfo.Keywords
if siteInfo.Description == "" {
siteInfo.Description = siteInfo.General.Description
}
data["description"] = siteInfo.Description
- data["language"] = handler.GetLang(ctx)
+ data["language"] = i18n.LanguageChinese //handler.GetLang(ctx)
data["timezone"] = siteInfo.Interface.TimeZone
ctx.HTML(code, tpl, data)
diff --git a/pkg/converter/str.go b/pkg/converter/str.go
index 7d4374cc..04415fe4 100644
--- a/pkg/converter/str.go
+++ b/pkg/converter/str.go
@@ -2,6 +2,7 @@ package converter
import (
"fmt"
+ "github.com/segmentfault/pacman/log"
"strconv"
)
@@ -25,6 +26,29 @@ func IntToString(data int64) string {
return fmt.Sprintf("%d", data)
}
+// InterfaceToString converts data to string
+// It will be used in template render
func InterfaceToString(data interface{}) string {
- return fmt.Sprintf("%d", data)
+ switch t := data.(type) {
+ case int:
+ i := data.(int)
+ return strconv.Itoa(i)
+ case int8:
+ i := data.(int8)
+ return strconv.Itoa(int(i))
+ case int16:
+ i := data.(int16)
+ return strconv.Itoa(int(i))
+ case int32:
+ i := data.(int32)
+ return string(i)
+ case int64:
+ i := data.(int64)
+ return strconv.FormatInt(i, 10)
+ case string:
+ return data.(string)
+ default:
+ log.Warn("can't convert type:", t)
+ }
+ return ""
}
diff --git a/ui/template/footer.html b/ui/template/footer.html
index 45839a21..f83ed4f5 100644
--- a/ui/template/footer.html
+++ b/ui/template/footer.html
@@ -1,12 +1,11 @@
{{define "footer"}}
</div>
-
<footer class="bg-light py-3">
<div class="container">
<p class="text-center mb-0 fs-14 text-secondary">
- Built on <a href="https://answer.dev/" target="_blank"> Answer </a>-
- the open-source software that power Q&amp;A communities.<br />Made
- with love © {{.siteinfo.Year}} {{.siteinfo.General.Name}}.
+ {{$cc := (join " " .siteinfo.Year .siteinfo.General.Name) -}}
+ {{- $ft := translator $.language "ui.footer.build_on" "cc" $cc -}}
+ {{templateHTML (replaceHTMLTag $ft "<a href=\"https://answer.dev/\" target=\"_blank\"> Answer </a>")}}
</p>
</div>
</footer>
diff --git a/ui/template/question.html b/ui/template/question.html
index 8724d28e..8a32fc1d 100644
--- a/ui/template/question.html
+++ b/ui/template/question.html
@@ -21,10 +21,17 @@
class="fw-bold" title="Reputation">{{.UserInfo.Rank}}</span>
</div>
•
+ {{if eq .CreateTime .UpdateTime}}
<time class="text-secondary ms-1"
datetime="{{timeFormatISO $.timezone .CreateTime}}"
title="{{translatorTimeFormatLongDate $.language $.timezone .CreateTime}}">{{translator $.language "ui.question.asked"}} {{translatorTimeFormat $.language $.timezone .CreateTime}}
</time>
+ {{else if gt .UpdateTime 0}}
+ <time class="text-secondary ms-1"
+ datetime="{{timeFormatISO $.timezone .UpdateTime}}"
+ title="{{translatorTimeFormatLongDate $.language $.timezone .UpdateTime}}">{{translator $.language "ui.question.modified"}} {{translatorTimeFormat $.language $.timezone .UpdateTime}}
+ </time>
+ {{end}}
</div>
<div class="ms-0 ms-md-3 mt-2 mt-md-0">
<span><i class="br bi-hand-thumbs-up-fill"></i><em
diff --git a/ui/template/tag-detail.html b/ui/template/tag-detail.html
index eedc3a89..c4150948 100644
--- a/ui/template/tag-detail.html
+++ b/ui/template/tag-detail.html
@@ -12,8 +12,9 @@
</div>
<div>
<div class="mb-3 d-flex flex-wrap justify-content-between">
- <h5 class="fs-5 text-nowrap mb-3 mb-md-0">{{ .questionCount }}
- {{translator ($.language) ("ui.question.questions")}}</h5>
+ <h5 class="fs-5 text-nowrap mb-3 mb-md-0">
+ {{translator ($.language) "ui.question.x_questions" "count" .questionCount}}
+ </h5>
</div>
<div class="border-top border-bottom-0 list-group list-group-flush">
{{range .questionList}}