add initial wiki support in backend and a test sample for frontend

This commit is contained in:
Arya Kiran
2023-04-25 23:20:12 +05:30
parent d3a4f02809
commit 16fa0305bd
3 changed files with 103 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
package pages
import (
"github.com/gocolly/colly"
"github.com/gofiber/fiber/v2"
"log"
"net/http"
"os"
"regexp"
"strings"
)
type Wiki struct {
RepoUser string
RepoName string
Title string
LastEdit string
LastEditor string
RevisionNum string
PageNum string
PageContent string
Sidebar []Sidebar
}
type Sidebar struct {
Name string
Link string
}
func WikiView(c *fiber.Ctx) error {
var wikiArray []Wiki
resp, statusErr := http.Get("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + "/wiki/" + c.Params("page"))
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": "Wiki" + c.Params("page") + "not found",
})
}
// Scraping
Scrape := Wiki{}
UserAgent, ok := os.LookupEnv("GOTHUB_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#repository-container-header", func(e *colly.HTMLElement) {
Scrape.RepoUser = e.ChildText("span.author a")
Scrape.RepoName = e.ChildText("strong a")
})
sc.OnHTML("div#wiki-wrapper", func(e *colly.HTMLElement) {
Scrape.Title = e.ChildText("div.d-flex h1")
Scrape.LastEdit = e.ChildText("div.gh-header-meta relative-time")
re := regexp.MustCompile(`\ edited.*`)
Scrape.LastEditor = re.ReplaceAllString(strings.Replace(e.ChildText("div.gh-header-meta"), "\n", "", -1), "${1}")
Scrape.RevisionNum = strings.TrimSuffix(e.ChildText("a.Link--muted"), " revisions")
})
sc.OnHTML("div.Box--condensed", func(e *colly.HTMLElement) {
Scrape.PageNum = e.ChildText("span.Counter--primary")
e.ForEach("ul.list-style-none li.Box-row", func(i int, el *colly.HTMLElement) {
Scrape.Sidebar = append(Scrape.Sidebar, Sidebar{
Name: el.ChildText("summary span a.Truncate-text"),
Link: el.ChildAttr("summary span a.Truncate-text", "href"),
})
})
})
sc.Visit("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + "/wiki/" + c.Params("page"))
// Add scrape-based info to wikiArray
wikiArray = append(wikiArray, Scrape)
return c.Render("wiki", fiber.Map{
"title": "Wiki page " + c.Params("page") + " | " + c.Params("user") + "/" + c.Params("repo"),
"wiki": wikiArray,
})
}
+5
View File
@@ -111,6 +111,11 @@ func Serve(port string) {
app.Get("/:user/:repo", pages.HandleRepo)
app.Get("/:user/:repo/blob/:branch/+", pages.FileView)
app.Get("/:user/:repo/tree/:branch/+", pages.DirView)
app.Get("/:user/:repo/wiki", func(c *fiber.Ctx) error {
c.Redirect("/" + c.Params("user") + "/" + c.Params("repo") + "/wiki/Home")
return nil
})
app.Get("/:user/:repo/wiki/:page", pages.WikiView)
app.Get("/:user/:repo/tree/:branch", pages.HandleRepo)
app.Get("/download/:user/:repo/:branch", func(c *fiber.Ctx) error {
utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch")+".zip")
+17
View File
@@ -0,0 +1,17 @@
{{ template "header" .}}
<main>
{{ if .wiki }} {{ range $key, $value := .wiki}}
<p>RepoUser: {{.RepoUser}}</p>
<p>RepoName: {{.RepoName}}</p>
<p>Title: {{.Title}}</p>
<p>LastEdit: {{.LastEdit}}</p>
<p>LastEditor: {{.LastEditor}}</p>
<p>RevisionNum: {{.RevisionNum}}</p>
<p>Sidebar: {{.Sidebar}}</p>
{{ end }} {{ else }}
<h2>Wiki not found</h2>
<p>That wiki doesn't exist.</p>
{{ end }}
</main>
{{ template "footer" .}}