Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

category listings #9

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"

"github.com/fogo-sh/almanac/pkg/content"
"github.com/fogo-sh/almanac/pkg/templates"
)

var outputCmd = &cobra.Command{
Expand All @@ -24,7 +23,7 @@ var outputCmd = &cobra.Command{

slog.Info(fmt.Sprintf("discovered %d pages, outputting to %s", len(pages), outputDir))

err = templates.OutputAllPagesToDisk(pages, outputDir)
err = content.OutputAllPagesToDisk(pages, outputDir)
checkError(err, "failed to output pages")

slog.Info("done!")
Expand Down
62 changes: 62 additions & 0 deletions pkg/content/discover.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
package content

import (
"bytes"
"fmt"
"path/filepath"
"sort"
)

func CreateSpecialPages(pages map[string]*Page) error {
pagesByCategory := PagesByCategory(pages)
allCategories := AllCategories(pages)

for _, category := range allCategories {
keysOfPagesInCategory := make([]string, 0, len(pagesByCategory[category]))
for _, page := range pagesByCategory[category] {
keysOfPagesInCategory = append(keysOfPagesInCategory, page.Title)
}

var buf bytes.Buffer
err := LinkListingTemplate.Execute(&buf, LinkListingData{
LinkList: keysOfPagesInCategory,
})
if err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}

pages[fmt.Sprintf("$Category:%s", category)] = &Page{
Title: fmt.Sprintf("$Category:%s", category),
LinksTo: keysOfPagesInCategory,
ParsedContent: buf.Bytes(),
}
}

return nil
}

func DiscoverPages(path string) (map[string]*Page, error) {
paths, error := filepath.Glob(filepath.Join(path, "*.md"))

Expand All @@ -24,6 +53,11 @@ func DiscoverPages(path string) (map[string]*Page, error) {
pages[page.Title] = &page
}

err := CreateSpecialPages(pages)
if err != nil {
return nil, fmt.Errorf("failed to create special pages: %w", err)
}

PopulateBacklinks(pages)

return pages, nil
Expand Down Expand Up @@ -52,6 +86,34 @@ func AllPageTitles(pages map[string]*Page) []string {
return allPageTitles
}

func PagesByCategory(pages map[string]*Page) map[string][]*Page {
pagesByCategory := make(map[string][]*Page)

for _, page := range pages {
for _, category := range page.Meta.Categories {
pagesByCategory[category] = append(pagesByCategory[category], page)
}
}

return pagesByCategory
}

func AllCategories(pages map[string]*Page) []string {
categories := map[string]struct{}{}
for _, page := range pages {
for _, category := range page.Meta.Categories {
categories[category] = struct{}{}
}
}

keys := make([]string, 0)
for k := range categories {
keys = append(keys, k)
}

return keys
}

func FindRootPage(pages map[string]*Page) (*Page, error) {
var rootPage *Page

Expand Down
8 changes: 3 additions & 5 deletions pkg/templates/output.go → pkg/content/output.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package templates
package content

import (
"bufio"
"fmt"
"html/template"
"os"

"github.com/fogo-sh/almanac/pkg/content"

cp "github.com/otiai10/copy"
)

func OutputAllPagesToDisk(pages map[string]*content.Page, outputDir string) error {
func OutputAllPagesToDisk(pages map[string]*Page, outputDir string) error {
os.RemoveAll(outputDir)

err := os.MkdirAll(outputDir, 0755)
if err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}

allPageTitles := content.AllPageTitles(pages)
allPageTitles := AllPageTitles(pages)

for key, page := range pages {
outputPath := outputDir + key + ".html"
Expand Down
4 changes: 2 additions & 2 deletions pkg/content/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type PageMeta struct {

type Page struct {
Title string
Path string
Path *string
LinksTo []string
Backlinks []string
Meta PageMeta
Expand Down Expand Up @@ -88,7 +88,7 @@ func ParsePageFile(path string) (Page, error) {
return Page{
Title: pageTitle,
LinksTo: linksTo,
Path: path,
Path: &path,
Meta: pageMeta,
ParsedContent: buf.Bytes(),
}, nil
Expand Down
41 changes: 31 additions & 10 deletions pkg/templates/page.go → pkg/content/templates.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package templates
package content

import (
"html/template"

"github.com/fogo-sh/almanac/pkg/content"
)

type PageTemplateData struct {
AllPageTitles []string
Page *content.Page
Page *Page
Content template.HTML
}

Expand All @@ -30,10 +28,16 @@ var pageTemplateContent = `<!DOCTYPE html>
<main>
<h1>{{ .Page.Title }}</h1>

{{ if .Page.Meta.Categories }}
<p>
{{ range .Page.Meta.Categories }}
<a href="/$Category:{{ . }}">{{ . }}</a>
{{ end }}
</p>
{{ end }}

{{ if .Page.Meta.Date }}
<section>
<p>{{ .Page.Meta.Date.Format "Aug 2, 2006" }}</p>
</section>
<p>{{ .Page.Meta.Date.Format "Aug 2, 2006" }}</p>
{{ end }}

{{ if .Page.Meta.YoutubeId }}
Expand Down Expand Up @@ -69,10 +73,27 @@ type PageData struct {
Content template.HTML
}

func init() {
t, err := template.New("page").Parse(pageTemplateContent)
var linkListingTemplateContent = `<ul>
{{ range .LinkList }}
<li><a href="/{{ . }}">{{ . }}</a></li>
{{ end }}
</ul>`

var LinkListingTemplate *template.Template

type LinkListingData struct {
LinkList []string
}

func initTemplate(name string, content string) *template.Template {
t, err := template.New(name).Parse(content)
if err != nil {
panic(err)
}
PageTemplate = t
return t
}

func init() {
PageTemplate = initTemplate("page", pageTemplateContent)
LinkListingTemplate = initTemplate("linkListing", linkListingTemplateContent)
}
7 changes: 3 additions & 4 deletions pkg/devserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/fogo-sh/almanac/pkg/content"
"github.com/fogo-sh/almanac/pkg/static"
"github.com/fogo-sh/almanac/pkg/templates"
)

type Config struct {
Expand Down Expand Up @@ -43,11 +42,11 @@ func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Con
return fmt.Errorf("unknown template: %s", name)
}

return templates.PageTemplate.Execute(w, data)
return content.PageTemplate.Execute(w, data)
}

func serveNotFound(c echo.Context) error {
return c.Render(http.StatusNotFound, "page", templates.PageTemplateData{
return c.Render(http.StatusNotFound, "page", content.PageTemplateData{
Content: template.HTML("<p>Looks like this page doesn't exist yet</p>"),
Page: &content.Page{
Title: "Not Found",
Expand Down Expand Up @@ -83,7 +82,7 @@ func (s *Server) servePage(c echo.Context) error {

allPageTitles := content.AllPageTitles(pages)

return c.Render(http.StatusOK, "page", templates.PageTemplateData{
return c.Render(http.StatusOK, "page", content.PageTemplateData{
AllPageTitles: allPageTitles,
Content: template.HTML(string(page.ParsedContent)),
Page: page,
Expand Down
Loading