Платформа ЦРНП "Мирокод" для разработки проектов
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.
68 lines
1.7 KiB
68 lines
1.7 KiB
package repo |
|
|
|
import ( |
|
"code.gitea.io/gitea/modules/base" |
|
"code.gitea.io/gitea/modules/context" |
|
"code.gitea.io/gitea/routers/web/user" |
|
"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"], err = GetRenderedResourcesWithSearchLinks(ctx, repo, "Resources") |
|
|
|
if err != nil { |
|
ctx.ServerError("Render", err) |
|
return |
|
} |
|
|
|
ctx.HTML(http.StatusOK, tplResources) |
|
} |
|
|
|
func GetRenderedResourcesWithSearchLinks(ctx *context.Context, repo interface{}, fieldName string) (string, error) { |
|
resources := user.GetTextField(repo, fieldName) |
|
|
|
var regExp *regexp.Regexp |
|
var err error |
|
|
|
regExp, err = regexp.Compile(`- \[ \] (.+)`) |
|
if err != nil { |
|
return "", err |
|
} |
|
|
|
resourceNamesMatches := regExp.FindAllStringSubmatch(resources, -1) |
|
if resourceNamesMatches == nil { |
|
return "", err |
|
} |
|
|
|
var resourcesWithSafeURLs = strings.Clone(resources) |
|
var resourceName string |
|
var searchQS string |
|
|
|
for _, matches := range resourceNamesMatches { |
|
resourceName = matches[1] |
|
searchQS = strings.ReplaceAll(resourceName, " ", "+") |
|
resourceSubstitutionPattern := fmt.Sprintf( |
|
`- \[ \] %s [найти](/explore/%s?tab=&q=%s)`, |
|
resourceName, |
|
strings.ToLower(fieldName), |
|
searchQS) |
|
|
|
resourcesWithSafeURLs = strings.Replace(resourcesWithSafeURLs, resourceName, resourceSubstitutionPattern, -1) |
|
} |
|
|
|
var renderedResourcesWithSafeURLs string |
|
renderedResourcesWithSafeURLs, err = user.GetRenderedTextFieldByValue(ctx, repo, resourcesWithSafeURLs) |
|
|
|
return renderedResourcesWithSafeURLs, err |
|
}
|
|
|