2021-12-22 08:14:34 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-02-14 14:06:46 -05:00
|
|
|
"errors"
|
2021-12-22 08:14:34 -05:00
|
|
|
"fmt"
|
2023-04-03 07:16:02 -04:00
|
|
|
cp "github.com/otiai10/copy"
|
2021-12-22 08:14:34 -05:00
|
|
|
"os"
|
2023-02-03 20:39:51 -05:00
|
|
|
"path/filepath"
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-14 12:58:36 -05:00
|
|
|
// Source files
|
2023-02-03 20:39:51 -05:00
|
|
|
const readmePath = "README.md"
|
2023-02-14 12:49:59 -05:00
|
|
|
|
2023-02-14 12:58:36 -05:00
|
|
|
// This files should be copied 'as is' to outDir directory
|
2023-02-14 12:49:59 -05:00
|
|
|
var staticFiles = []string{
|
|
|
|
"tmpl/assets",
|
|
|
|
"tmpl/_redirects",
|
|
|
|
"tmpl/robots.txt",
|
|
|
|
}
|
2023-02-03 20:39:51 -05:00
|
|
|
|
2023-02-14 13:12:57 -05:00
|
|
|
// TODO: embed
|
|
|
|
// Templates
|
|
|
|
var tplIndex = template.Must(template.ParseFiles("tmpl/tmpl.html"))
|
|
|
|
var tplCategoryIndex = template.Must(template.ParseFiles("tmpl/cat-tmpl.html"))
|
|
|
|
var tplSitemap = template.Must(template.ParseFiles("tmpl/sitemap-tmpl.xml"))
|
2023-02-03 20:39:51 -05:00
|
|
|
|
2023-02-14 12:58:36 -05:00
|
|
|
// Output files
|
|
|
|
const outDir = "out/" // NOTE: trailing slash is required
|
2023-04-03 07:16:02 -04:00
|
|
|
|
|
|
|
var outIndexFile = filepath.Join(outDir, "index.html")
|
|
|
|
var outSitemapFile = filepath.Join(outDir, "sitemap.xml")
|
2023-02-03 20:39:51 -05:00
|
|
|
|
2021-12-22 08:14:34 -05:00
|
|
|
func main() {
|
2023-02-03 21:01:22 -05:00
|
|
|
// Cleanup and re-create output directory
|
|
|
|
{
|
|
|
|
if err := os.RemoveAll(outDir); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mkdirAll(outDir); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := GenerateHTML(readmePath, outIndexFile)
|
2022-08-30 09:20:58 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-02-03 20:39:51 -05:00
|
|
|
|
2023-04-03 07:16:02 -04:00
|
|
|
input, err := os.ReadFile(outIndexFile)
|
2021-12-22 08:14:34 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-02-03 20:39:51 -05:00
|
|
|
|
2023-02-14 13:12:57 -05:00
|
|
|
query, err := goquery.NewDocumentFromReader(bytes.NewReader(input))
|
2021-12-22 08:14:34 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-02-14 13:56:32 -05: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
|
|
|
|
}
|
2023-02-14 14:06:46 -05:00
|
|
|
obj, err := makeObjByID(selector, query.Find("body"))
|
|
|
|
if err != nil {
|
2022-08-30 09:20:58 -04:00
|
|
|
return
|
|
|
|
}
|
2023-02-14 14:06:46 -05:00
|
|
|
|
2023-02-14 13:56:32 -05:00
|
|
|
objs[selector] = *obj
|
2021-12-22 08:14:34 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-02-03 20:39:51 -05:00
|
|
|
if err := makeSiteStruct(objs); err != nil {
|
|
|
|
// FIXME: remove all panics
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-08-30 10:14:48 -04:00
|
|
|
changeLinksInIndex(string(input), query, objs)
|
2022-08-30 09:20:58 -04:00
|
|
|
|
2021-12-22 08:14:34 -05:00
|
|
|
makeSitemap(objs)
|
2023-04-03 07:16:02 -04:00
|
|
|
|
2023-02-14 12:49:59 -05:00
|
|
|
for _, srcFilename := range staticFiles {
|
|
|
|
dstFilename := filepath.Join(outDir, filepath.Base(srcFilename))
|
|
|
|
fmt.Printf("Copy static file: %s -> %s\n", srcFilename, dstFilename)
|
|
|
|
if err := cp.Copy(srcFilename, dstFilename); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-04-03 07:16:02 -04:00
|
|
|
}
|
2021-12-22 08:14:34 -05:00
|
|
|
}
|
|
|
|
|
2023-02-03 20:39:51 -05:00
|
|
|
func mkdirAll(path string) error {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
// NOTE: directory is exists
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: unknown error
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: directory is not exists
|
2023-02-14 15:26:37 -05:00
|
|
|
// FIXME: fix rights
|
2023-02-03 20:39:51 -05:00
|
|
|
if err := os.MkdirAll(path, 0o755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-14 13:56:32 -05:00
|
|
|
func makeSiteStruct(objs map[string]Object) error {
|
2021-12-22 08:14:34 -05:00
|
|
|
for _, obj := range objs {
|
2023-04-03 07:16:02 -04:00
|
|
|
categoryDir := filepath.Join(outDir, obj.Slug)
|
|
|
|
if err := mkdirAll(categoryDir); err != nil {
|
2023-02-03 20:39:51 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: embed templates
|
|
|
|
// FIXME: parse templates once at start
|
2023-02-14 12:50:14 -05:00
|
|
|
categoryIndexFilename := filepath.Join(categoryDir, "index.html")
|
|
|
|
f, err := os.Create(categoryIndexFilename)
|
2021-12-22 08:14:34 -05:00
|
|
|
if err != nil {
|
2023-02-03 20:39:51 -05:00
|
|
|
return err
|
2021-12-22 08:14:34 -05:00
|
|
|
}
|
|
|
|
|
2023-02-14 12:50:14 -05:00
|
|
|
fmt.Printf("Write category Index file: %s\n", categoryIndexFilename)
|
|
|
|
|
2023-02-14 13:12:57 -05:00
|
|
|
if err := tplCategoryIndex.Execute(f, obj); err != nil {
|
2023-02-03 20:39:51 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-12-22 08:14:34 -05:00
|
|
|
}
|
2023-02-03 20:39:51 -05:00
|
|
|
|
|
|
|
return nil
|
2021-12-22 08:14:34 -05:00
|
|
|
}
|
|
|
|
|
2023-02-14 13:56:32 -05:00
|
|
|
func makeSitemap(objs map[string]Object) {
|
2023-02-14 12:50:14 -05:00
|
|
|
// FIXME: handle error
|
2023-04-03 07:16:02 -04:00
|
|
|
f, _ := os.Create(outSitemapFile)
|
2023-02-14 12:50:14 -05:00
|
|
|
fmt.Printf("Render Sitemap to: %s\n", outSitemapFile)
|
|
|
|
|
2023-02-14 13:12:57 -05:00
|
|
|
_ = tplSitemap.Execute(f, objs)
|
2021-12-22 08:14:34 -05:00
|
|
|
}
|
|
|
|
|
2023-02-14 14:06:46 -05:00
|
|
|
func makeObjByID(selector string, s *goquery.Selection) (*Object, error) {
|
|
|
|
var obj Object
|
|
|
|
var err error
|
|
|
|
|
2023-02-14 14:24:30 -05:00
|
|
|
s.Find(selector).Each(func(_ int, selCatHeader *goquery.Selection) {
|
|
|
|
selDescr := selCatHeader.NextFiltered("p")
|
|
|
|
// FIXME: bug. this would select links from all neighboring
|
|
|
|
// sub-categories until the next category. To prevent this we should
|
|
|
|
// find only first ul
|
|
|
|
ul := selCatHeader.NextFilteredUntil("ul", "h2")
|
2021-12-22 08:14:34 -05:00
|
|
|
|
2023-02-14 14:06:46 -05:00
|
|
|
var links []Link
|
2023-02-14 14:24:30 -05:00
|
|
|
ul.Find("li").Each(func(_ int, selLi *goquery.Selection) {
|
|
|
|
selLink := selLi.Find("a")
|
|
|
|
url, _ := selLink.Attr("href")
|
2021-12-22 08:14:34 -05:00
|
|
|
link := Link{
|
2023-02-14 14:24:30 -05:00
|
|
|
Title: selLink.Text(),
|
|
|
|
// FIXME: Title contains only title but description contains Title + description
|
|
|
|
Description: selLi.Text(),
|
2021-12-22 08:14:34 -05:00
|
|
|
Url: url,
|
|
|
|
}
|
|
|
|
links = append(links, link)
|
|
|
|
})
|
2023-02-14 14:06:46 -05:00
|
|
|
// FIXME: In this case we would have an empty category in main index.html with link to 404 page.
|
2022-08-30 10:21:44 -04:00
|
|
|
if len(links) == 0 {
|
2023-02-14 14:06:46 -05:00
|
|
|
err = errors.New("object has no links")
|
2022-08-30 10:21:44 -04:00
|
|
|
return
|
|
|
|
}
|
2023-02-14 14:06:46 -05:00
|
|
|
obj = Object{
|
2023-02-14 14:24:30 -05:00
|
|
|
Slug: slug.Generate(selCatHeader.Text()),
|
|
|
|
Title: selCatHeader.Text(),
|
|
|
|
Description: selDescr.Text(),
|
2021-12-22 08:14:34 -05:00
|
|
|
Items: links,
|
|
|
|
}
|
|
|
|
})
|
2023-02-14 14:06:46 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to build an object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &obj, nil
|
2021-12-22 08:14:34 -05:00
|
|
|
}
|
2021-12-25 09:32:20 -05:00
|
|
|
|
2023-02-14 13:56:32 -05: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 {
|
2023-02-14 13:46:53 -05:00
|
|
|
// FIXME: looks like is an error. Tag `a` in our case always
|
|
|
|
// should have `href` attr.
|
2022-08-30 09:20:58 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-14 13:46:53 -05:00
|
|
|
// FIXME: parse url
|
2022-08-30 09:20:58 -04:00
|
|
|
uri := strings.SplitAfter(href, "#")
|
|
|
|
if len(uri) >= 2 && uri[1] != "contents" {
|
2023-02-14 13:46:53 -05:00
|
|
|
// FIXME: use s.SetAttr
|
2022-08-30 09:20:58 -04:00
|
|
|
html = strings.ReplaceAll(
|
2023-02-14 13:46:53 -05:00
|
|
|
html,
|
|
|
|
fmt.Sprintf(`href="%s"`, href),
|
|
|
|
fmt.Sprintf(`href="%s"`, uri[1]),
|
|
|
|
)
|
2021-12-25 09:32:20 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-02-14 12:50:14 -05:00
|
|
|
fmt.Printf("Rewrite links in Index file: %s\n", outIndexFile)
|
|
|
|
_ = os.WriteFile(outIndexFile, []byte(html), 0644)
|
2021-12-25 09:32:20 -05:00
|
|
|
}
|