initial logic for generating folder/htmld sub cat

Signed-off-by: Avelino <avelinorun@gmail.com>
This commit is contained in:
Avelino 2021-12-22 10:14:34 -03:00
parent 27e9630564
commit b8e8fc724e
2 changed files with 156 additions and 0 deletions

99
make_site.go Normal file
View File

@ -0,0 +1,99 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"text/template"
"github.com/PuerkitoBio/goquery"
"github.com/avelino/slugify"
)
type Link struct {
Title string
Url string
Description string
}
type Object struct {
Title string
Slug string
Description string
Items []Link
}
func main() {
GenerateHTML()
input, err := ioutil.ReadFile("./tmpl/index.html")
if err != nil {
panic(err)
}
buf := bytes.NewBuffer(input)
query, err := goquery.NewDocumentFromReader(buf)
if err != nil {
panic(err)
}
objs := []Object{}
query.Find("body #content ul ul").First().Each(func(_ int, s *goquery.Selection) {
s.Find("li a").Each(func(_ int, s *goquery.Selection) {
selector, _ := s.Attr("href")
obj := makeObjById(selector, query.Find("body"))
objs = append(objs, obj)
})
})
makeSiteStruct(objs)
makeSitemap(objs)
}
func makeSiteStruct(objs []Object) {
for _, obj := range objs {
folder := fmt.Sprintf("tmpl/%s", obj.Slug)
fmt.Println(folder)
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)
}
}
func makeSitemap(objs []Object) {
t := template.Must(template.ParseFiles("tmpl/sitemap-tmpl.xml"))
f, _ := os.Create("tmpl/sitemap.xml")
t.Execute(f, objs)
}
func makeObjById(selector string, s *goquery.Selection) (obj Object) {
s.Find(selector).Each(func(_ int, s *goquery.Selection) {
desc := s.NextFiltered("p")
ul := desc.NextFiltered("ul")
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)
})
obj = Object{
Slug: slugify.Slugify(s.Text()),
Title: s.Text(),
Description: desc.Text(),
Items: links,
}
})
return
}

57
tmpl/cat-tmpl.html vendored Normal file
View File

@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Language" content="en">
<meta name="viewport" content="width=device-width">
<title>{{.Title}} - Awesome Go</title>
<meta name="description" content="{{.Description}} - Awesome Go">
<meta name="keywords" content="golang, go, awesome, awesome-go, go framework, golang framework">
<meta name="twitter:card" value="summary">
<meta property="og:title" content="{{.Description}} - Awesome Go" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://awesome-go.com/{{.Slug}}" />
<meta property="og:image" content="https://awesome-go.com/assets/logo.png" />
<meta property="og:description" content="{{.Description}} - Awesome Go" />
<link rel="stylesheet" type="text/css" href="/assets/fonts/firasans.css">
<link rel="stylesheet" type="text/css" href="/assets/normalize.css">
<link rel="stylesheet" type="text/css" href="/assets/awesome-go.css">
</head>
<body>
<div id="content">
<ul>
{{range .Items}}
<li href="/{{.Url}}">{{.Descrition}}</li>
{{end}}
</ul>
<a href="https://www.netlify.com">
<img src="https://www.netlify.com/img/global/badges/netlify-dark.svg" alt="Deploys by Netlify" />
</a>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/1.1.0/marked.min.js" integrity="sha512-uggp1jOpxGjqTeS8Fit5x6+lqyJoIuXXn/VziVPlxBRnqZ0FhCaxsUnQsPL5PKylHr0KIoMtNbBIiU6n31dDTg==" crossorigin="anonymous"></script>
<script>
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-85465107-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>