From 42a838666aa9da0799dcd3c3d9c6ad0344d1e8d6 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 14 Apr 2024 22:42:33 +0200 Subject: wip: render posts and lists --- go.mod | 3 ++ go.sum | 40 +++++++++++++++ src/build.go | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/config.go | 38 ++++++++++++++ 4 files changed, 230 insertions(+), 6 deletions(-) create mode 100644 src/config.go diff --git a/go.mod b/go.mod index 5f15dec..da450e0 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,13 @@ go 1.22.1 require ( github.com/BurntSushi/toml v1.2.1 // indirect + github.com/PuerkitoBio/goquery v1.9.1 // indirect github.com/adrg/frontmatter v0.2.0 // indirect + github.com/andybalholm/cascadia v1.3.2 // indirect github.com/deckarep/golang-set/v2 v2.6.0 // indirect github.com/yuin/goldmark v1.7.1 // indirect go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect + golang.org/x/net v0.21.0 // indirect gopkg.in/yaml.v2 v2.3.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index e0469b8..0a9eadb 100644 --- a/go.sum +++ b/go.sum @@ -1,14 +1,54 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI= +github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY= github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4= github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE= +github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= +github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM= github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U= github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= go.abhg.dev/goldmark/frontmatter v0.2.0 h1:P8kPG0YkL12+aYk2yU3xHv4tcXzeVnN+gU0tJ5JnxRw= go.abhg.dev/goldmark/frontmatter v0.2.0/go.mod h1:XqrEkZuM57djk7zrlRUB02x8I5J0px76YjkOzhB4YlU= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/src/build.go b/src/build.go index b2847c3..3c2dfd5 100644 --- a/src/build.go +++ b/src/build.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" "log" "os" @@ -10,8 +11,11 @@ import ( "strings" "time" + "github.com/PuerkitoBio/goquery" "github.com/adrg/frontmatter" mapset "github.com/deckarep/golang-set/v2" + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/extension" ) type PostMatter struct { @@ -40,14 +44,14 @@ func check(err error) { } } -func getPost(filename string) (PostMatter, string) { +func getPost(filename string) (PostMatter, []byte) { matter := PostMatter{} content, err := os.Open(filename) check(err) rest, err := frontmatter.Parse(content, &matter) check(err) - return matter, string(rest) + return matter, rest } func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags) { @@ -58,6 +62,13 @@ func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags) { outputReplacer := strings.NewReplacer(root, outputDir, ".md", "/index.html") urlReplacer := strings.NewReplacer(root, "", ".md", "/") check(err) + md := goldmark.New( + goldmark.WithExtensions( + extension.GFM, + extension.Footnote, + extension.Typographer, + ), + ) for _, f := range files { pathFromRoot := filepath.Join(subdir, f.Name()) check(err) @@ -70,13 +81,16 @@ func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags) { tags.Add(strings.ToLower(tag)) } + var buf bytes.Buffer + err := md.Convert(content, &buf) + check(err) post := Post{ Input: pathFromRoot, Output: output, Basename: filepath.Base(url), URL: url, PostMatter: matter, - Content: content, + Content: buf.String(), } posts = append(posts, post) @@ -88,15 +102,144 @@ func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags) { return posts, tags } +func layout(html string, config Config, pageTitle string) *goquery.Document { + css, err := os.ReadFile("templates/style.css") + check(err) + doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) + check(err) + doc.Find("html").SetAttr("lang", config.DefaultLanguage) + doc.Find("head > link[rel=alternate]").Add(".title").SetAttr("title", config.Title) + doc.Find("title").Add(".p-name").SetText(pageTitle) + doc.Find("head > style").SetText(string(css)) + nav := doc.Find("nav") + navLink := doc.Find("nav a") + nav.Empty() + for _, link := range config.Menus["main"] { + nav.AppendSelection(navLink.Clone().SetAttr("href", link.URL).SetText(link.Name)) + } + return doc +} + +func renderPost(post Post, config Config) string { + tbytes, err := os.ReadFile("templates/post.html") + check(err) + doc := layout(string(tbytes), config, post.PostMatter.Title) + doc.Find(".title").AddClass("h-card p-author").SetAttr("rel", "author") + datetime, err := post.PostMatter.Date.MarshalText() + check(err) + doc.Find(".h-entry .dt-published").SetAttr("datetime", string(datetime)).SetText( + post.PostMatter.Date.Format("2006-01-02"), + ) + doc.Find(".h-entry .e-content").SetHtml(post.Content) + categories := doc.Find(".h-entry .p-categories") + cat := categories.Find(".p-category").ParentsUntilSelection(categories) + cat.Remove() + for _, tag := range post.Taxonomies.Tags { + categories.AppendSelection(cat.Clone().Find(".p-category").SetAttr("href", fmt.Sprintf("/tags/%s/", tag)).SetText("#" + tag)).Parent() + } + html, err := doc.Html() + check(err) + return html +} + +func renderTags(tags Tags, config Config) string { + tbytes, err := os.ReadFile("templates/tags.html") + check(err) + + doc := layout(string(tbytes), config, config.Title) + tagList := doc.Find(".tags") + tpl := doc.Find(".h-feed") + tpl.Remove() + for _, tag := range mapset.Sorted(tags) { + tagList.AppendSelection( + tpl.Clone().SetAttr("href", fmt.Sprintf("/tags/%s/", tag)).SetText("#" + tag), + ) + } + html, err := doc.Html() + check(err) + return html +} + +func renderListPage(tag string, config Config, posts []Post) string { + tbytes, err := os.ReadFile("templates/list.html") + check(err) + var title string + if len(tag) > 0 { + title = tag + } else { + title = config.Title + } + doc := layout(string(tbytes), config, title) + feed := doc.Find(".h-feed") + tpl := feed.Find(".h-entry") + tpl.Remove() + + doc.Find(".title").AddClass("h-card p-author").SetAttr("rel", "author") + if tag == "" { + doc.Find(".filter").Remove() + } else { + doc.Find(".filter").Find("h3").SetText("#" + tag) + } + + for _, post := range posts { + entry := tpl.Clone() + datetime, err := post.PostMatter.Date.MarshalText() + check(err) + + entry.Find(".p-name").SetText(post.Title).SetAttr("href", post.URL) + entry.Find(".dt-published").SetAttr("datetime", string(datetime)).SetText(post.PostMatter.Date.Format("2006-01-02")) + feed.AppendSelection(entry) + } + + html, err := doc.Html() + check(err) + return html +} + func main() { - err := os.MkdirAll("public/post", 755) + config := getConfig() + err := os.MkdirAll("public/post", 0755) check(err) log.Print("Generating site...") posts, tags := readPosts("content", "post", "public") for _, post := range posts { - err := os.MkdirAll(path.Join("public", "post", post.Basename), 755) + err := os.MkdirAll(path.Join("public", "post", post.Basename), 0755) check(err) - fmt.Printf("%+v\n", post.Date) + // output := renderPost(post, config) + // err = os.WriteFile(post.Output, []byte(output), 0755) + // check(err) } + err = os.MkdirAll("public/tags", 0755) + check(err) + // fmt.Printf("%+v\n", renderTags(tags, config)) + // err = os.WriteFile("public/tags/index.html", []byte(renderTags(tags, config)), 0755) + // check(err) + for _, tag := range tags.ToSlice() { + matchingPosts := []Post{} + for _, post := range posts { + if slices.Contains(post.Taxonomies.Tags, tag) { + matchingPosts = append(matchingPosts, post) + } + } + err := os.MkdirAll(path.Join("public", "tags", tag), 0755) + check(err) + // tagPage := renderListPage(tag, config, matchingPosts) + // fmt.Printf("%+v\n", tagPage) + // err = os.WriteFile(path.Join("public", "tags", tag, "index.html"), []byte(tagPage), 0755) + // check(err) + + // TODO: tag atom + + fmt.Printf("%+v\n", renderListPage("", config, posts)) + + // TODO: atom + + // TODO: renderFeedStyles + + // TODO: renderHomepage + + // TODO: render404Page + } + fmt.Printf("%+v\n", tags) } diff --git a/src/config.go b/src/config.go new file mode 100644 index 0000000..bfa3052 --- /dev/null +++ b/src/config.go @@ -0,0 +1,38 @@ +package main + +import ( + "github.com/BurntSushi/toml" +) + +type Taxonomy struct { + Name string + Feed bool +} + +type MenuItem struct { + Name string + URL string `toml:"url"` +} + +type Config struct { + DefaultLanguage string `toml:"default_language"` + BaseURL string `toml:"base_url"` + RedirectOtherHostnames bool `toml:"redirect_other_hostnames"` + Title string + Email string + Description string + DomainStartDate string `toml:"domain_start_date"` + OriginalDomain string `toml:"original_domain"` + Taxonomies []Taxonomy + Extra struct { + Headers map[string]string + } + Menus map[string][]MenuItem +} + +func getConfig() Config { + config := Config{} + _, err := toml.DecodeFile("config.toml", &config) + check(err) + return config +} -- cgit 1.4.1