Платформа ЦРНП "Мирокод" для разработки проектов https://git.mirocod.ru
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

93 lines
2.4 KiB

package repo
import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/routers/web/user"
"code.gitea.io/gitea/services/trust_props"
"fmt"
"net/http"
"regexp"
"strings"
)
const (
tplResources base.TplName = "repo/resources/list"
)
func Resources(ctx *context.Context) {
ctx.Data["PageIsResources"] = true
repo := ctx.Data["Repository"]
var err error
ctx.Data["RenderedResources"], ctx.Data["TransformedTrustProps"], err = GetRenderedTrustPropsWithSearchLinks(ctx, repo, "Resources")
if err != nil {
ctx.ServerError("Render", err)
return
}
ctx.HTML(http.StatusOK, tplResources)
}
func ToggleResources(ctx *context.Context) {
if !ctx.IsSigned {
ctx.Error(http.StatusForbidden)
return
}
toggledContent, err := trust_props.SaveRepoAndRenderContent(ctx, "resources")
if err != nil {
ctx.ServerError("ToggleCompetences", err)
return
}
responseBody := map[string]interface{}{
"content": toggledContent,
}
ctx.JSON(
http.StatusOK,
responseBody,
)
}
func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{}, fieldName string) (string, string, error) {
trustProps := user.GetTextField(repo, fieldName)
var regExp *regexp.Regexp
var err error
regExp, err = regexp.Compile(`- \[ \] (.+)`)
if err != nil {
return "", "", err
}
trustPropNamesMatches := regExp.FindAllStringSubmatch(trustProps, -1)
var transformedTrustProps string
if trustPropNamesMatches == nil {
transformedTrustProps = trustProps
} else {
transformedTrustProps = strings.Clone(trustProps)
var trustPropName string
var searchQS string
for _, curTrustPropNameMatches := range trustPropNamesMatches {
trustPropName = curTrustPropNameMatches[1]
searchQS = strings.ReplaceAll(trustPropName, " ", "+")
trustPropSubstitutionPattern := fmt.Sprintf(
`- [ ] %s [найти](/explore/%s?tab=&q=%s)`,
trustPropName,
strings.ToLower(fieldName),
searchQS)
transformedTrustProps = strings.Replace(transformedTrustProps, curTrustPropNameMatches[0], trustPropSubstitutionPattern, -1)
}
}
var renderedTrustPropsWithSafeURLs string
renderedTrustPropsWithSafeURLs, err = user.GetRenderedTextFieldByValue(ctx, repo, transformedTrustProps)
renderedTrustPropsWithTargetBlank := strings.ReplaceAll(renderedTrustPropsWithSafeURLs, "<a", "<a target='blank'")
return renderedTrustPropsWithTargetBlank, transformedTrustProps, err
}