From f47bfce66febe97680e1f03f0109eb7fb7e35a11 Mon Sep 17 00:00:00 2001 From: glacials Date: Sun, 28 Jun 2026 07:10:01 -0700 Subject: [PATCH] allow templates to list drafts --- README.md | 23 ++++++++++---- cmd/build.go | 2 +- cmd/build_test.go | 2 +- document/template.go | 40 ++++++++++++++++++------ document/template_test.go | 66 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5f02849..4018783 100644 --- a/README.md +++ b/README.md @@ -224,8 +224,9 @@ See the [top of this page](#toc) for an example. The kind of document. Possible values are `post`, `page`, `draft`. -`post` documents are programmatically included in template functions. -`page` and `draft` documents have no programmatic action taken on +`post` documents are included in the `posts` template function. +`draft` documents are included in the `drafts` template function. +`page` documents have no programmatic action taken on them and will not be discoverable unless linked to. The default template set provided by `winter init` gives each a different visual treatment: @@ -344,15 +345,25 @@ from most to least recent. See [Document Fields](#fields) for a list of fields available to documents. +##### `drafts` + +Usage: `{{ range drafts }} ... {{ end }}` + +Returns a list of all documents with type `draft`, +from most to least recent. + +See [Document Fields](#fields) for a list of fields available to documents. + ##### `yearly` Usage: `{{ range yearly posts }}{{ .Year }}: {{ range .Documents.All }} ... {{ end }}{{ end }}` Returns a list of `year` types ordered from most to least recent. -A `year` has two fields, -`.Year` (integer) +A `year` has three fields, +`.Year` (integer), +`.Undated` (boolean) and -`.Documents` (array of documents). -This allows templates to display posts sectioned by year. +`.Documents` (collection of documents). +This allows templates to display posts or drafts sectioned by year. See [Document Fields](#fields) for a list of fields available to documents. diff --git a/cmd/build.go b/cmd/build.go index 213d09a..922c304 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -165,5 +165,5 @@ func serveWithListener( } func servingMessage(servedDir, baseURL string) string { - return cliutils.Sprintf("Serving %s on %s", servedDir, baseURL) + "\n" + return fmt.Sprintf("Serving %s on %s\n", servedDir, baseURL) } diff --git a/cmd/build_test.go b/cmd/build_test.go index ae1a63d..ae5abd0 100644 --- a/cmd/build_test.go +++ b/cmd/build_test.go @@ -34,7 +34,7 @@ func TestServeWithListenerAnnouncesURL(t *testing.T) { ) }() - want := servingMessage(dist, "http://localhost:8100") + want := "Serving dist on http://localhost:8100\n" deadline := time.Now().Add(2 * time.Second) for !strings.Contains(out.String(), want) { if time.Now().After(deadline) { diff --git a/document/template.go b/document/template.go index 9f5aa04..3048fd5 100644 --- a/document/template.go +++ b/document/template.go @@ -188,6 +188,7 @@ func (doc *TemplateDocument) funcmap(tmplPath string) (template.FuncMap, error) ), ) }, + "drafts": doc.draftsFunc, "posts": doc.postsFunc, "yearly": yearly, }, nil @@ -199,17 +200,30 @@ func (doc *TemplateDocument) galleryFunc(name string) []*img { return doc.photos[name] } +// draftsFunc is a function to be used by templates. +// It retrieves a slice of documents of type draft. +func (doc *TemplateDocument) draftsFunc() []Document { + return doc.documentsOfKind(draft) +} + // postsFunc is a function to be used by templates. -// It retrieves a slice of metadatas for all documents of type post. +// It retrieves a slice of documents of type post. func (doc *TemplateDocument) postsFunc() []Document { - posts := &documents{All: make([]Document, 0, len(doc.docs.All))} + return doc.documentsOfKind(post) +} + +func (doc *TemplateDocument) documentsOfKind(k kind) []Document { + if doc.docs == nil { + return nil + } + docs := &documents{All: make([]Document, 0, len(doc.docs.All))} for _, doc := range doc.docs.All { - if doc.Metadata().Kind == post { - posts.add(doc) + if doc.Metadata().Kind == k { + docs.add(doc) } } - sort.Sort(posts) - return posts.All + sort.Sort(docs) + return docs.All } // render is a function available to templates. @@ -234,12 +248,17 @@ func yearly(docs []Document) years { // groups is a map of year to data for that year. groups := map[int]*year{} for _, doc := range docs { - if doc.Metadata().Kind != post { - continue + y := 0 + undated := doc.Metadata().CreatedAt.IsZero() + if !undated { + y = doc.Metadata().CreatedAt.Year() } - y := doc.Metadata().CreatedAt.Year() if _, ok := groups[y]; !ok { - groups[y] = &year{Documents: &documents{}, Year: y} + groups[y] = &year{ + Documents: &documents{}, + Undated: undated, + Year: y, + } } groups[y].Documents.add(doc) } @@ -308,6 +327,7 @@ func (a years) Swap(i, j int) { type year struct { Year int Documents *documents + Undated bool } type iconfunc func(graphic.SRC, graphic.Alt) (template.HTML, error) diff --git a/document/template_test.go b/document/template_test.go index ad3fef0..eaf6fb9 100644 --- a/document/template_test.go +++ b/document/template_test.go @@ -3,9 +3,11 @@ package document // import "twos.dev/winter/document" import ( "bytes" "fmt" + "io" "path/filepath" "strings" "testing" + "time" "gotest.tools/v3/assert" ) @@ -53,3 +55,67 @@ func TestTemplate(t *testing.T) { }) } } + +func TestTemplateDocumentDraftsFunc(t *testing.T) { + docs := &documents{} + docs.add(testDocument("Old Draft", draft, time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC))) + docs.add(testDocument("Post", post, time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC))) + docs.add(testDocument("New Draft", draft, time.Date(2025, time.January, 1, 0, 0, 0, 0, time.UTC))) + + doc := NewTemplateDocument( + "src/test/drafts", + NewMetadata("src/test/drafts", filepath.Join("testdata", "templates")), + docs, + nil, + nil, + ) + + got := doc.draftsFunc() + assert.Equal(t, len(got), 2) + assert.Equal(t, got[0].Metadata().Title, "New Draft") + assert.Equal(t, got[1].Metadata().Title, "Old Draft") +} + +func TestYearlyGroupsGivenDocuments(t *testing.T) { + got := yearly([]Document{ + testDocument("Draft", draft, time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC)), + testDocument("Post", post, time.Date(2024, time.February, 1, 0, 0, 0, 0, time.UTC)), + testDocument("Undated Draft", draft, time.Time{}), + }) + + assert.Equal(t, len(got), 2) + assert.Equal(t, got[0].Year, 2024) + assert.Equal(t, got[0].Undated, false) + assert.Equal(t, len(got[0].Documents.All), 2) + assert.Equal(t, got[1].Year, 0) + assert.Equal(t, got[1].Undated, true) + assert.Equal(t, len(got[1].Documents.All), 1) +} + +type testDoc struct { + meta *Metadata +} + +func testDocument(title string, k kind, createdAt time.Time) Document { + meta := NewMetadata("src/test/"+strings.ToLower(strings.ReplaceAll(title, " ", "-")), filepath.Join("testdata", "templates")) + meta.CreatedAt = createdAt + meta.Kind = k + meta.Title = title + return testDoc{meta: meta} +} + +func (doc testDoc) DependsOn(string) bool { + return false +} + +func (doc testDoc) Load(io.Reader) error { + return nil +} + +func (doc testDoc) Metadata() *Metadata { + return doc.meta +} + +func (doc testDoc) Render(io.Writer) error { + return nil +} -- knotmirror 0.0.1