teafolio/page_home.go

91 lines
2.7 KiB
Go
Raw Permalink Normal View History

2022-12-31 00:47:07 +00:00
package main
import (
"fmt"
"html"
"net/http"
"net/url"
"strings"
)
func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
this.reposMut.RLock()
defer this.reposMut.RUnlock()
if len(this.reposCache) == 0 {
// We haven't loaded the repositories from Gitea yet
this.Delay(w, r)
return
}
// Ready for template
this.Templatepage(w, r, "", "", func() {
fmt.Fprint(w, `
`+this.cfg.Template.HomepageHeaderHTML+`
<select id="sortorder" style="float:right;">
<option value="data-sort-al">Alphabetical</option>
<option value="data-sort-ls">Lifespan</option>
<option value="data-sort-ct" selected>Recent projects</option>
<option value="data-sort-mt">Recent updates</option>
</select>
<h2>Projects <small>(`+fmt.Sprintf("%d", len(this.reposCache))+`)</small></h2>
<table id="projtable-main" class="projtable">
`)
2022-12-31 01:18:47 +00:00
for repoIdx, repo := range this.reposCache {
2022-12-31 00:47:07 +00:00
pageHref := html.EscapeString(`/` + url.PathEscape(repo.Name))
normalisedDesc := repo.Description
normalisedDesc = strings.TrimRight(repo.Description, `.`)
if len(normalisedDesc) > 0 {
// Lowercase the first letter of the description, unless it starts with an acronym (all letters uppercase first word) or CamelCase word
firstWord := strings.SplitN(normalisedDesc, " ", 2)[0]
isAcronymOrCamelCase := len(firstWord) > 1 && (firstWord[1:] != strings.ToLower(firstWord[1:]))
if !(isAcronymOrCamelCase || firstWord == `Go`) {
normalisedDesc = strings.ToLower(normalisedDesc[0:1]) + normalisedDesc[1:]
}
// Add leading `<COMMA><SPACE>` to separate from the repo title
normalisedDesc = `, ` + normalisedDesc
}
rowClass := ""
2022-12-31 01:06:43 +00:00
for _, topic := range repo.Topics {
2022-12-31 00:47:07 +00:00
rowClass += `taggedWith-` + topic + ` `
}
fmt.Fprint(w, `
<tr
class="`+html.EscapeString(rowClass)+`"
data-sort-al="`+fmt.Sprintf("-%d", this.reposAlphabeticalOrder[repo.Name])+`"
2022-12-31 01:06:43 +00:00
data-sort-ls="`+fmt.Sprintf("%.0f", repo.NewestCommit.Sub(repo.GiteaCreated).Seconds())+`"
2022-12-31 01:18:47 +00:00
data-sort-ct="`+fmt.Sprintf("-%d", repoIdx)+`"
2022-12-31 01:06:43 +00:00
data-sort-mt="`+fmt.Sprintf("%d", repo.NewestCommit.Unix())+`"
2022-12-31 00:47:07 +00:00
>
<td>
<a href="`+pageHref+`"><img class="homeimage" loading="lazy" src="`+html.EscapeString(`/:banner/`+url.PathEscape(repo.Name))+`"></div></a>
</td>
<td>
<strong>`+html.EscapeString(repo.Name)+`</strong>`+html.EscapeString(normalisedDesc)+`
<a href="`+pageHref+`" class="article-read-more">more...</a>
<br>
<small>
`)
2022-12-31 01:06:43 +00:00
for _, topic := range repo.Topics {
2022-12-31 00:47:07 +00:00
fmt.Fprint(w, `<a class="tag tag-link" data-tag="`+html.EscapeString(topic)+`">`+html.EscapeString(topic)+`</a> `)
}
fmt.Fprint(w, `
</small>
</td>
</tr>
`)
}
fmt.Fprint(w, `
</table>
`)
})
}