Browse Source

Реализован вывод ресурсов со ссылкой на поиск лиц с данным ресурсом #65

pull/120/head
Artur Galyamov 2 years ago
parent
commit
c1c130ae34
  1. 2
      routers/web/explore/user.go
  2. 2
      routers/web/repo/competence.go
  3. 27
      routers/web/repo/resource.go
  4. 22
      routers/web/user/profile.go

2
routers/web/explore/user.go

@ -92,7 +92,7 @@ func RenderUserSearch(ctx *context.Context, opts *user_model.SearchUserOptions,
contentFieldName = "Description"
}
for _, curUser := range users {
renderedContent[curUser.ID], err = user.GetRenderedTextField(ctx, curUser, contentFieldName)
renderedContent[curUser.ID], err = user.GetRenderedTextFieldByName(ctx, curUser, contentFieldName)
if err != nil {
ctx.ServerError("RenderContent", err)
return

2
routers/web/repo/competence.go

@ -16,7 +16,7 @@ func Competences(ctx *context.Context) {
repo := ctx.Data["Repository"]
var err error
ctx.Data["RenderedCompetences"], err = user.GetRenderedTextField(ctx, repo, "Competences")
ctx.Data["RenderedCompetences"], err = user.GetRenderedTextFieldByName(ctx, repo, "Competences")
if err != nil {
ctx.ServerError("Render", err)

27
routers/web/repo/resource.go

@ -4,7 +4,9 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/routers/web/user"
"errors"
"net/http"
"regexp"
)
const (
@ -16,7 +18,7 @@ func Resources(ctx *context.Context) {
repo := ctx.Data["Repository"]
var err error
ctx.Data["RenderedResources"], err = user.GetRenderedTextField(ctx, repo, "Resources")
ctx.Data["RenderedResources"], err = getRenderedResourcesWithSearchLinks(ctx, repo)
if err != nil {
ctx.ServerError("Render", err)
@ -25,3 +27,26 @@ func Resources(ctx *context.Context) {
ctx.HTML(http.StatusOK, tplResources)
}
func getRenderedResourcesWithSearchLinks(ctx *context.Context, repo interface{}) (string, error) {
resources := user.GetTextField(repo, "Resources")
var regExp *regexp.Regexp
var err error
regExp, err = regexp.Compile(`- \[ \] (.+)`)
if err != nil {
return "", err
}
resourcesWithLinks := regExp.ReplaceAll([]byte(resources), []byte(`- \[ \] $1 [найти](/explore/resources?tab=&q=$1)`))
if resourcesWithLinks == nil {
return "", errors.New("not found matches in resources")
}
var renderedResourcesWithLinks string
renderedResourcesWithLinks, err = user.GetRenderedTextFieldByValue(ctx, repo, string(resourcesWithLinks))
return renderedResourcesWithLinks, err
}

22
routers/web/user/profile.go

@ -164,7 +164,7 @@ func Profile(ctx *context.Context) {
content string
)
content, renderErr = GetRenderedTextField(ctx, ctxUser, "Description")
content, renderErr = GetRenderedTextFieldByName(ctx, ctxUser, "Description")
if renderErr == nil {
ctx.Data["RenderedDescription"] = content
} else {
@ -172,7 +172,7 @@ func Profile(ctx *context.Context) {
return
}
content, renderErr = GetRenderedTextField(ctx, ctxUser, "Competences")
content, renderErr = GetRenderedTextFieldByName(ctx, ctxUser, "Competences")
if renderErr == nil {
ctx.Data["RenderedCompetences"] = content
} else {
@ -180,7 +180,7 @@ func Profile(ctx *context.Context) {
return
}
content, renderErr = GetRenderedTextField(ctx, ctxUser, "Resources")
content, renderErr = GetRenderedTextFieldByName(ctx, ctxUser, "Resources")
if renderErr == nil {
ctx.Data["RenderedResources"] = content
} else {
@ -188,7 +188,7 @@ func Profile(ctx *context.Context) {
return
}
content, renderErr = GetRenderedTextField(ctx, ctxUser, "Interests")
content, renderErr = GetRenderedTextFieldByName(ctx, ctxUser, "Interests")
if renderErr == nil {
ctx.Data["RenderedInterests"] = content
} else {
@ -402,16 +402,24 @@ func Action(ctx *context.Context) {
ctx.RedirectToFirst(ctx.FormString("redirect_to"), u.HomeLink())
}
func getTextField(obj interface{}, fieldName string) string {
func GetTextField(obj interface{}, fieldName string) string {
reflectedObj := reflect.ValueOf(obj)
dynamicField := reflect.Indirect(reflectedObj).FieldByName(fieldName)
return dynamicField.String()
}
func GetRenderedTextField(ctx *context.Context, obj interface{}, fieldName string) (string, error) {
func GetRenderedTextFieldByName(ctx *context.Context, obj interface{}, fieldName string) (string, error) {
var err error = nil
var content string
fieldVal := getTextField(obj, fieldName)
fieldVal := GetTextField(obj, fieldName)
content, err = GetRenderedTextFieldByValue(ctx, obj, fieldVal)
return content, err
}
func GetRenderedTextFieldByValue(ctx *context.Context, obj interface{}, fieldVal string) (string, error) {
var content string
var err error
if len(fieldVal) != 0 {
content, err = markdown.RenderString(&markup.RenderContext{
URLPrefix: ctx.Repo.RepoLink,

Loading…
Cancel
Save