2021-12-22 08:14:34 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-12-25 09:32:20 -05:00
|
|
|
"strings"
|
2021-12-22 08:14:34 -05:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2022-08-30 17:52:53 -04:00
|
|
|
"github.com/avelino/awesome-go/pkg/slug"
|
2021-12-22 08:14:34 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
Title string
|
|
|
|
Url string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Object struct {
|
|
|
|
Title string
|
|
|
|
Slug string
|
|
|
|
Description string
|
|
|
|
Items []Link
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-08-30 09:20:58 -04:00
|
|
|
err := GenerateHTML()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
input, err := os.ReadFile("./tmpl/index.html")
|
2021-12-22 08:14:34 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
buf := bytes.NewBuffer(input)
|
|
|
|
query, err := goquery.NewDocumentFromReader(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-08-30 09:20:58 -04:00
|
|
|
objs := make(map[string]*Object)
|
2022-08-30 10:09:19 -04:00
|
|
|
query.Find("body #contents").NextFiltered("ul").Find("ul").Each(func(_ int, s *goquery.Selection) {
|
2021-12-22 08:14:34 -05:00
|
|
|
s.Find("li a").Each(func(_ int, s *goquery.Selection) {
|
2022-08-30 09:20:58 -04:00
|
|
|
selector, exists := s.Attr("href")
|
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
2021-12-22 08:14:34 -05:00
|
|
|
obj := makeObjById(selector, query.Find("body"))
|
2022-08-30 09:20:58 -04:00
|
|
|
if obj == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
objs[selector] = obj
|
2021-12-22 08:14:34 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-08-30 10:14:48 -04:00
|
|
|
makeSiteStruct(objs)
|
|
|
|
changeLinksInIndex(string(input), query, objs)
|
2022-08-30 09:20:58 -04:00
|
|
|
|
2021-12-22 08:14:34 -05:00
|
|
|
makeSitemap(objs)
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:14:48 -04:00
|
|
|
func makeSiteStruct(objs map[string]*Object) {
|
2021-12-22 08:14:34 -05:00
|
|
|
for _, obj := range objs {
|
|
|
|
folder := fmt.Sprintf("tmpl/%s", obj.Slug)
|
|
|
|
err := os.Mkdir(folder, 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t := template.Must(template.ParseFiles("tmpl/cat-tmpl.html"))
|
|
|
|
f, _ := os.Create(fmt.Sprintf("%s/index.html", folder))
|
|
|
|
t.Execute(f, obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 09:20:58 -04:00
|
|
|
func makeSitemap(objs map[string]*Object) {
|
2021-12-22 08:14:34 -05:00
|
|
|
t := template.Must(template.ParseFiles("tmpl/sitemap-tmpl.xml"))
|
|
|
|
f, _ := os.Create("tmpl/sitemap.xml")
|
|
|
|
t.Execute(f, objs)
|
|
|
|
}
|
|
|
|
|
2022-08-30 09:20:58 -04:00
|
|
|
func makeObjById(selector string, s *goquery.Selection) (obj *Object) {
|
2021-12-22 08:14:34 -05:00
|
|
|
s.Find(selector).Each(func(_ int, s *goquery.Selection) {
|
|
|
|
desc := s.NextFiltered("p")
|
2022-08-30 09:20:58 -04:00
|
|
|
ul := s.NextFilteredUntil("ul", "h2")
|
2021-12-22 08:14:34 -05:00
|
|
|
|
|
|
|
links := []Link{}
|
|
|
|
ul.Find("li").Each(func(_ int, s *goquery.Selection) {
|
|
|
|
url, _ := s.Find("a").Attr("href")
|
|
|
|
link := Link{
|
|
|
|
Title: s.Find("a").Text(),
|
|
|
|
Description: s.Text(),
|
|
|
|
Url: url,
|
|
|
|
}
|
|
|
|
links = append(links, link)
|
|
|
|
})
|
2022-08-30 09:20:58 -04:00
|
|
|
obj = &Object{
|
2022-08-30 17:52:53 -04:00
|
|
|
Slug: slug.Generate(s.Text()),
|
2021-12-22 08:14:34 -05:00
|
|
|
Title: s.Text(),
|
|
|
|
Description: desc.Text(),
|
|
|
|
Items: links,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-12-25 09:32:20 -05:00
|
|
|
|
2022-08-30 10:14:48 -04:00
|
|
|
func changeLinksInIndex(html string, query *goquery.Document, objs map[string]*Object) {
|
2021-12-25 09:32:20 -05:00
|
|
|
query.Find("body #content ul li ul li a").Each(func(_ int, s *goquery.Selection) {
|
2022-08-30 09:20:58 -04:00
|
|
|
href, hrefExists := s.Attr("href")
|
|
|
|
if !hrefExists {
|
|
|
|
return
|
|
|
|
}
|
2021-12-25 09:32:20 -05:00
|
|
|
|
2022-08-30 09:20:58 -04:00
|
|
|
// do not replace links if no page has been created for it
|
|
|
|
_, objExists := objs[href]
|
|
|
|
if !objExists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uri := strings.SplitAfter(href, "#")
|
|
|
|
if len(uri) >= 2 && uri[1] != "contents" {
|
|
|
|
html = strings.ReplaceAll(
|
|
|
|
html, fmt.Sprintf(`href="%s"`, href), fmt.Sprintf(`href="%s"`, uri[1]))
|
2021-12-25 09:32:20 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
os.WriteFile("./tmpl/index.html", []byte(html), 0644)
|
|
|
|
}
|