言語バー
このコミットが含まれているのは:
コミット
05b9b1006d
234
pages/repo.go
234
pages/repo.go
@ -1,129 +1,139 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitler.moe/suwako/gitlin/utils"
|
||||
"github.com/gocolly/colly"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gitler.moe/suwako/gitlin/utils"
|
||||
"github.com/gocolly/colly"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type Languages struct {
|
||||
Name string
|
||||
Percent string
|
||||
Color string
|
||||
}
|
||||
|
||||
type Repo struct {
|
||||
Fullname string
|
||||
Description string
|
||||
Parent string
|
||||
Stars string
|
||||
Forks string
|
||||
CommitsBehind string
|
||||
Watchers string
|
||||
Language []string
|
||||
License string
|
||||
DefaultBranch string
|
||||
Readme string
|
||||
Link string
|
||||
Tags []string
|
||||
Branch []string
|
||||
Fullname string
|
||||
Description string
|
||||
Parent string
|
||||
Stars string
|
||||
Forks string
|
||||
CommitsBehind string
|
||||
Watchers string
|
||||
Language []Languages
|
||||
License string
|
||||
DefaultBranch string
|
||||
Readme string
|
||||
Link string
|
||||
Tags []string
|
||||
Branch []string
|
||||
}
|
||||
|
||||
type RepoFiles struct {
|
||||
Name string
|
||||
Path string
|
||||
Type string
|
||||
Fullname string
|
||||
DefaultBranch string
|
||||
Name string
|
||||
Path string
|
||||
Type string
|
||||
Fullname string
|
||||
DefaultBranch string
|
||||
}
|
||||
|
||||
func HandleRepo(c *fiber.Ctx) error {
|
||||
var repoArray []Repo
|
||||
var repoFilesArray []RepoFiles
|
||||
var readmeOutput string
|
||||
branchExists := ""
|
||||
if strings.Count(c.Params("branch"), "")-1 > 0 {
|
||||
branchExists = "/tree/" + c.Params("branch")
|
||||
}
|
||||
repoUrl := strings.TrimSuffix(c.Params("repo"), ".git")
|
||||
resp, statusErr := http.Get("https://github.com/" + c.Params("user") + "/" + repoUrl + branchExists)
|
||||
if statusErr != nil {
|
||||
log.Println(statusErr)
|
||||
}
|
||||
if resp.StatusCode == 404 {
|
||||
// I need a better way to do this
|
||||
return c.Status(404).Render("error", fiber.Map{
|
||||
"title": "Error",
|
||||
"error": "Repository " + c.Params("user") + "/" + repoUrl + branchExists + " not found",
|
||||
})
|
||||
}
|
||||
var repoArray []Repo
|
||||
var repoFilesArray []RepoFiles
|
||||
var readmeOutput string
|
||||
branchExists := ""
|
||||
if strings.Count(c.Params("branch"), "")-1 > 0 {
|
||||
branchExists = "/tree/" + c.Params("branch")
|
||||
}
|
||||
repoUrl := strings.TrimSuffix(c.Params("repo"), ".git")
|
||||
resp, statusErr := http.Get("https://github.com/" + c.Params("user") + "/" + repoUrl + branchExists)
|
||||
if statusErr != nil {
|
||||
log.Println(statusErr)
|
||||
}
|
||||
if resp.StatusCode == 404 {
|
||||
// I need a better way to do this
|
||||
return c.Status(404).Render("error", fiber.Map{
|
||||
"title": "Error",
|
||||
"error": "Repository " + c.Params("user") + "/" + repoUrl + branchExists + " not found",
|
||||
})
|
||||
}
|
||||
|
||||
// Scraping
|
||||
Scrape := Repo{}
|
||||
// Scraping
|
||||
Scrape := Repo{}
|
||||
|
||||
UserAgent, ok := os.LookupEnv("GITLIN_USER_AGENT")
|
||||
if !ok {
|
||||
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
|
||||
}
|
||||
UserAgent, ok := os.LookupEnv("GITLIN_USER_AGENT")
|
||||
if !ok {
|
||||
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
|
||||
}
|
||||
|
||||
sc := colly.NewCollector(colly.AllowedDomains("github.com"), colly.UserAgent(UserAgent))
|
||||
sc.OnHTML("div.Layout-sidebar", func(e *colly.HTMLElement) {
|
||||
Scrape.Fullname = c.Params("user") + "/" + repoUrl
|
||||
Scrape.Description = e.ChildText("p.f4")
|
||||
Scrape.Stars = e.ChildText("a[href*='/" + c.Params("user") + "/" + repoUrl + "/stargazers' i] strong")
|
||||
Scrape.Watchers = e.ChildText("a[href*='/" + c.Params("user") + "/" + repoUrl + "/watchers' i] strong")
|
||||
Scrape.Forks = e.ChildText("a[href*='/" + c.Params("user") + "/" + repoUrl + "/forks' i] strong")
|
||||
Scrape.Link = e.ChildAttr("span.css-truncate a.text-bold", "href")
|
||||
Scrape.License = e.ChildText("a[data-analytics-event*='{\"category\":\"Repository Overview\",\"action\":\"click\",\"label\":\"location:sidebar;file:license\"}']")
|
||||
e.ForEach("a.topic-tag", func(i int, el *colly.HTMLElement) {
|
||||
Scrape.Tags = append(Scrape.Tags, strings.TrimPrefix(el.Attr("data-octo-dimensions"), "topic:"))
|
||||
})
|
||||
})
|
||||
sc.OnHTML("div.Box-body div.d-flex div span", func(e *colly.HTMLElement) {
|
||||
Scrape.CommitsBehind = strings.TrimSuffix(e.ChildText("a"), " commits behind")
|
||||
})
|
||||
sc.OnHTML("div#readme", func(e *colly.HTMLElement) {
|
||||
Scrape.Readme = e.ChildText("a[href='#readme']")
|
||||
})
|
||||
sc.OnHTML("div#readme div.Box-body", func(e *colly.HTMLElement) {
|
||||
Content, _ := e.DOM.Html()
|
||||
readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1)
|
||||
})
|
||||
sc.OnHTML("div.BorderGrid-cell ul.list-style-none", func(e *colly.HTMLElement) {
|
||||
e.ForEach("li.d-inline .d-inline-flex", func(i int, el *colly.HTMLElement) {
|
||||
Scrape.Language = append(Scrape.Language, el.ChildText("span.text-bold")+" "+el.ChildText("span:contains('%')"))
|
||||
})
|
||||
})
|
||||
sc.OnHTML("div#repository-container-header", func(e *colly.HTMLElement) {
|
||||
Scrape.Parent = e.ChildText("span.text-small a")
|
||||
})
|
||||
sc.OnHTML("summary[title*='Switch branches or tags']", func(e *colly.HTMLElement) {
|
||||
Scrape.DefaultBranch = e.ChildText("span.css-truncate-target")
|
||||
})
|
||||
sc.OnHTML("div.js-details-container div.Details-content--hidden-not-important", func(e *colly.HTMLElement) {
|
||||
e.ForEach("div.js-navigation-item", func(i int, el *colly.HTMLElement) {
|
||||
var FileType string
|
||||
if el.ChildAttr("div.flex-shrink-0 svg", "aria-label") == "Directory" {
|
||||
FileType = "dir"
|
||||
} else {
|
||||
FileType = "file"
|
||||
}
|
||||
repoFilesArray = append(repoFilesArray, RepoFiles{
|
||||
Name: el.ChildText("div.flex-auto span.d-block a.js-navigation-open"),
|
||||
Path: el.ChildText("div.flex-auto span.d-block a.js-navigation-open"),
|
||||
Type: FileType,
|
||||
Fullname: Scrape.Fullname,
|
||||
DefaultBranch: Scrape.DefaultBranch,
|
||||
})
|
||||
})
|
||||
})
|
||||
sc.Visit("https://github.com/" + c.Params("user") + "/" + repoUrl + branchExists)
|
||||
// Add scrape-based info to repoArray
|
||||
repoArray = append(repoArray, Scrape)
|
||||
return c.Render("repo", fiber.Map{
|
||||
"title": c.Params("user") + "/" + repoUrl + branchExists,
|
||||
"branch": utils.Branch,
|
||||
"repo": repoArray,
|
||||
"files": repoFilesArray,
|
||||
"readme": readmeOutput,
|
||||
})
|
||||
sc := colly.NewCollector(colly.AllowedDomains("github.com"), colly.UserAgent(UserAgent))
|
||||
sc.OnHTML("div.Layout-sidebar", func(e *colly.HTMLElement) {
|
||||
Scrape.Fullname = c.Params("user") + "/" + repoUrl
|
||||
Scrape.Description = e.ChildText("p.f4")
|
||||
Scrape.Stars = e.ChildText("a[href*='/" + c.Params("user") + "/" + repoUrl + "/stargazers' i] strong")
|
||||
Scrape.Watchers = e.ChildText("a[href*='/" + c.Params("user") + "/" + repoUrl + "/watchers' i] strong")
|
||||
Scrape.Forks = e.ChildText("a[href*='/" + c.Params("user") + "/" + repoUrl + "/forks' i] strong")
|
||||
Scrape.Link = e.ChildAttr("span.css-truncate a.text-bold", "href")
|
||||
Scrape.License = e.ChildText("a[data-analytics-event*='{\"category\":\"Repository Overview\",\"action\":\"click\",\"label\":\"location:sidebar;file:license\"}']")
|
||||
e.ForEach("a.topic-tag", func(i int, el *colly.HTMLElement) {
|
||||
Scrape.Tags = append(Scrape.Tags, strings.TrimPrefix(el.Attr("data-octo-dimensions"), "topic:"))
|
||||
})
|
||||
})
|
||||
sc.OnHTML("div.Box-body div.d-flex div span", func(e *colly.HTMLElement) {
|
||||
Scrape.CommitsBehind = strings.TrimSuffix(e.ChildText("a"), " commits behind")
|
||||
})
|
||||
sc.OnHTML("div#readme", func(e *colly.HTMLElement) {
|
||||
Scrape.Readme = e.ChildText("a[href='#readme']")
|
||||
})
|
||||
sc.OnHTML("div#readme div.Box-body", func(e *colly.HTMLElement) {
|
||||
Content, _ := e.DOM.Html()
|
||||
readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1)
|
||||
})
|
||||
sc.OnHTML("div.BorderGrid-cell ul.list-style-none", func(e *colly.HTMLElement) {
|
||||
e.ForEach("li.d-inline .d-inline-flex", func(i int, el *colly.HTMLElement) {
|
||||
var lang Languages
|
||||
lang.Name = el.ChildText("span.text-bold")
|
||||
lang.Percent = el.ChildText("span:contains('%')")
|
||||
lang.Color = strings.ReplaceAll(strings.ReplaceAll(el.ChildAttr("svg", "style"), "color:", ""), ";", "")
|
||||
Scrape.Language = append(Scrape.Language, lang)
|
||||
})
|
||||
})
|
||||
sc.OnHTML("div#repository-container-header", func(e *colly.HTMLElement) {
|
||||
Scrape.Parent = e.ChildText("span.text-small a")
|
||||
})
|
||||
sc.OnHTML("summary[title*='Switch branches or tags']", func(e *colly.HTMLElement) {
|
||||
Scrape.DefaultBranch = e.ChildText("span.css-truncate-target")
|
||||
})
|
||||
sc.OnHTML("div.js-details-container div.Details-content--hidden-not-important", func(e *colly.HTMLElement) {
|
||||
e.ForEach("div.js-navigation-item", func(i int, el *colly.HTMLElement) {
|
||||
var FileType string
|
||||
if el.ChildAttr("div.flex-shrink-0 svg", "aria-label") == "Directory" {
|
||||
FileType = "dir"
|
||||
} else {
|
||||
FileType = "file"
|
||||
}
|
||||
repoFilesArray = append(repoFilesArray, RepoFiles{
|
||||
Name: el.ChildText("div.flex-auto span.d-block a.js-navigation-open"),
|
||||
Path: el.ChildText("div.flex-auto span.d-block a.js-navigation-open"),
|
||||
Type: FileType,
|
||||
Fullname: Scrape.Fullname,
|
||||
DefaultBranch: Scrape.DefaultBranch,
|
||||
})
|
||||
})
|
||||
})
|
||||
sc.Visit("https://github.com/" + c.Params("user") + "/" + repoUrl + branchExists)
|
||||
// Add scrape-based info to repoArray
|
||||
repoArray = append(repoArray, Scrape)
|
||||
return c.Render("repo", fiber.Map{
|
||||
"title": c.Params("user") + "/" + repoUrl + branchExists,
|
||||
"branch": utils.Branch,
|
||||
"repo": repoArray,
|
||||
"files": repoFilesArray,
|
||||
"readme": readmeOutput,
|
||||
})
|
||||
}
|
||||
|
@ -305,3 +305,13 @@ a:hover {
|
||||
.vimvixen-console-frame {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.ui.segment:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.ui.segment {
|
||||
width: 100%;
|
||||
display: inherit;
|
||||
height: 10px;
|
||||
}
|
||||
|
@ -3,12 +3,8 @@
|
||||
<main>
|
||||
{{ if .repo }} {{ range $key, $value := .repo}}
|
||||
<div class="button-parent">
|
||||
<a href="/{{.Fullname}}/archive/{{.DefaultBranch}}.zip" class="button"
|
||||
>Download (zip)</a
|
||||
>
|
||||
<a rel="noreferrer" href="https://github.com/{{.Fullname}}" class="button"
|
||||
>View on GitHub</a
|
||||
>
|
||||
<a href="/{{.Fullname}}/archive/{{.DefaultBranch}}.zip" class="button">Download (zip)</a>
|
||||
<a rel="noreferrer" href="https://github.com/{{.Fullname}}" class="button">View on GitHub</a>
|
||||
</div>
|
||||
|
||||
<div class="user-profile">
|
||||
@ -31,7 +27,15 @@
|
||||
{{.DefaultBranch}}
|
||||
</p>
|
||||
{{ end }} {{ if .Language }}
|
||||
<p>🗒️ {{range .Language}} {{.}} {{end}}</p>
|
||||
<p>
|
||||
{{range $o := .Language}}{{$o.Name}} {{$o.Percent}}, {{end}}
|
||||
<div class="ui segment">{{range $i, $o := .Language}}
|
||||
<span style="width: {{$o.Percent}}; background-color: {{$o.Color}}">
|
||||
|
||||
</span>
|
||||
{{end}}
|
||||
</div>
|
||||
</p>
|
||||
{{end}} {{ if .CommitsBehind }} This repository is {{.CommitsBehind}}
|
||||
commits behind its parent. {{end}}
|
||||
</div>
|
||||
@ -43,18 +47,12 @@
|
||||
{{ range $key, $value := .files}} {{ if eq .Type "dir" }}
|
||||
<li class="file-list">
|
||||
📁
|
||||
<a
|
||||
href="/{{.Fullname}}/tree/{{.DefaultBranch}}/{{.Path}}"
|
||||
class="filesA"
|
||||
>{{.Path}}</a
|
||||
>
|
||||
<a href="/{{.Fullname}}/tree/{{.DefaultBranch}}/{{.Path}}" class="filesA">{{.Path}}</a>
|
||||
</li>
|
||||
{{ else }}
|
||||
<li class="file-list">
|
||||
🗒️
|
||||
<a href="/{{.Fullname}}/blob/{{.DefaultBranch}}/{{.Path}}"
|
||||
>{{.Path}}</a
|
||||
>
|
||||
<a href="/{{.Fullname}}/blob/{{.DefaultBranch}}/{{.Path}}">{{.Path}}</a>
|
||||
</li>
|
||||
{{ end }} {{ end }}
|
||||
</ul>
|
||||
|
読み込み中…
新しいイシューから参照
ユーザーをブロックする