diff --git a/src/Foliage-Core-Tests/FOBlogPostRenderContextTest.class.st b/src/Foliage-Core-Tests/FOBlogPostRenderContextTest.class.st index 4b8f9bf..6c45602 100644 --- a/src/Foliage-Core-Tests/FOBlogPostRenderContextTest.class.st +++ b/src/Foliage-Core-Tests/FOBlogPostRenderContextTest.class.st @@ -58,6 +58,39 @@ FOBlogPostRenderContextTest >> testSeriesNavigationAndTitleForMiddlePart [ self assert: context seriesNextPost equals: part3 ] +{ #category : #private } +FOBlogPostRenderContextTest >> makePostTitled: aTitle publishDate: aDateString tags: aTagsArray [ + | post | + post := FOFoliagePage new + meta: (Dictionary new + at: #title put: aTitle; + at: #publishDate put: aDateString; + yourself); + document: (FOMarkdownParser parse: aTitle, ' text.'); + yourself. + post meta at: #tags put: aTagsArray. + ^ post +] + +{ #category : #tests } +FOBlogPostRenderContextTest >> testHasTagsIsFalseWhenPostHasNoTags [ + blog at: #solo put: (self makePostTitled: 'Solo' publishDate: '2020-01-01' series: nil). + self deny: (self contextFor: (blog / #solo)) hasTags +] + +{ #category : #tests } +FOBlogPostRenderContextTest >> testHasTagsIsTrueWhenPostHasTags [ + blog at: #tagged put: (self makePostTitled: 'Tagged' publishDate: '2020-01-01' tags: #('soil')). + self assert: (self contextFor: (blog / #tagged)) hasTags +] + +{ #category : #tests } +FOBlogPostRenderContextTest >> testTagsHtmlListsAllTagsAsLinksToTheirOverviewPages [ + blog at: #tagged put: (self makePostTitled: 'Tagged' publishDate: '2020-01-01' tags: #('soil' 'byob-database')). + self assert: (self contextFor: (blog / #tagged)) tagsHtml equals: + 'Soil, Byob Database' +] + { #category : #tests } FOBlogPostRenderContextTest >> testSeriesNavigationHasNoPreviousOnFirstPartOrNextOnLastPart [ | part1 part2 | diff --git a/src/Foliage-Core-Tests/FOTagIndexBuilderTest.class.st b/src/Foliage-Core-Tests/FOTagIndexBuilderTest.class.st new file mode 100644 index 0000000..1dc289e --- /dev/null +++ b/src/Foliage-Core-Tests/FOTagIndexBuilderTest.class.st @@ -0,0 +1,114 @@ +Class { + #name : #FOTagIndexBuilderTest, + #superclass : #TestCase, + #instVars : [ + 'tempDir', + 'site', + 'blog' + ], + #category : #'Foliage-Core-Tests' +} + +{ #category : #running } +FOTagIndexBuilderTest >> setUp [ + super setUp. + tempDir := FileLocator temp / ('fo-tag-index-test-', UUID new asString36). + tempDir ensureCreateDirectory. + (tempDir / 'templates') ensureCreateDirectory. + (tempDir / 'templates' / 'blog') writeStreamDo: [ :s | + s nextPutAll: '{{page.title}}{{{ body }}}' ]. + (tempDir / 'templates' / 'blog-post-abstract') writeStreamDo: [ :s | + s nextPutAll: '
{{abstract}}
' ]. + (tempDir / 'templates' / 'blog-post') writeStreamDo: [ :s | + s nextPutAll: '{{page.title}}{{{ body }}}' ]. + (tempDir / 'templates' / 'tag-abstract') writeStreamDo: [ :s | + s nextPutAll: '
{{title}} ({{postCount}} posts): {{abstract}}
' ]. + site := FOWebSite new + outputPath: (tempDir / 'docs'); + templatePath: (tempDir / 'templates'); + yourself. + blog := FOBlog new title: 'My Blog'; yourself. + site root at: #blog put: blog +] + +{ #category : #running } +FOTagIndexBuilderTest >> tearDown [ + tempDir ifNotNil: [ tempDir exists ifTrue: [ tempDir deleteAll ] ]. + super tearDown +] + +{ #category : #private } +FOTagIndexBuilderTest >> makePostTitled: aTitle publishDate: aDateString tags: aTagsArray [ + | post | + post := FOFoliagePage new + meta: (Dictionary new + at: #title put: aTitle; + at: #publishDate put: aDateString; + yourself); + document: (FOMarkdownParser parse: aTitle, ' text.'); + yourself. + post meta at: #tags put: aTagsArray. + ^ post +] + +{ #category : #tests } +FOTagIndexBuilderTest >> testTagRenderAbstractUsesTheTagAbstractTemplate [ + | aTag | + blog at: #part1 put: (self makePostTitled: 'Part 1' publishDate: '2026-07-20' tags: #('soil')). + blog at: #part2 put: (self makePostTitled: 'Part 2' publishDate: '2026-07-21' tags: #('soil')). + aTag := blog tags detect: [ :t | t slug = 'soil' ]. + self assert: aTag renderAbstract equals: + '
Soil (2 posts): Part 2 text.
' +] + +{ #category : #tests } +FOTagIndexBuilderTest >> testAbstractPrefersHandWrittenDataFileOverAutoDerivedOne [ + | aTag | + site dataPath: (tempDir / 'data'). + (tempDir / 'data' / 'tags') ensureCreateDirectory. + (tempDir / 'data' / 'tags' / 'soil.md') writeStreamDo: [ :s | + s nextPutAll: 'A hand-written description of the whole tag.' ]. + blog at: #part1 put: (self makePostTitled: 'Part 1' publishDate: '2026-07-20' tags: #('soil')). + aTag := blog tags detect: [ :t | t slug = 'soil' ]. + self assert: aTag abstract equals: 'A hand-written description of the whole tag.' +] + +{ #category : #tests } +FOTagIndexBuilderTest >> testAbstractFallsBackToNewestPostWhenNoDataFileExists [ + "Unlike FOSeries (falls back to the oldest/part-1 post), a tag has no + 'part 1' - the newest post is the most sensible representative excerpt." + | aTag | + site dataPath: (tempDir / 'data'). + blog at: #older put: (self makePostTitled: 'Older' publishDate: '2026-07-20' tags: #('soil')). + blog at: #newer put: (self makePostTitled: 'Newer' publishDate: '2026-07-21' tags: #('soil')). + aTag := blog tags detect: [ :t | t slug = 'soil' ]. + self assert: aTag abstract equals: 'Newer text.' +] + +{ #category : #tests } +FOTagIndexBuilderTest >> testBuildPageListsEveryTagNewestActiveFirst [ + | builder page stream | + blog at: #soilOld put: (self makePostTitled: 'Soil old' publishDate: '2020-01-01' tags: #('soil')). + blog at: #byobNew put: (self makePostTitled: 'Byob new' publishDate: '2026-07-01' tags: #('byob')). + builder := FOTagIndexBuilder new blog: blog; yourself. + page := builder buildPage. + self assert: page title equals: 'Tags'. + stream := WriteStream on: String new. + page renderOn: stream. + self assert: stream contents equals: + '
Byob (1 posts): Byob new text.
', + '
Soil (1 posts): Soil old text.
' +] + +{ #category : #tests } +FOTagIndexBuilderTest >> testBuildPageListsTagsSharedAcrossMultiplePosts [ + | builder page stream | + blog at: #a put: (self makePostTitled: 'Post A' publishDate: '2020-01-01' tags: #('soil' 'database')). + blog at: #b put: (self makePostTitled: 'Post B' publishDate: '2020-02-01' tags: #('soil')). + builder := FOTagIndexBuilder new blog: blog; yourself. + page := builder buildPage. + stream := WriteStream on: String new. + page renderOn: stream. + self assert: (stream contents includesSubstring: 'href="/blog/tags/soil/index.html">Soil (2 posts)'). + self assert: (stream contents includesSubstring: 'href="/blog/tags/database/index.html">Database (1 posts)') +] diff --git a/src/Foliage-Core-Tests/FOTagOverviewBuilderTest.class.st b/src/Foliage-Core-Tests/FOTagOverviewBuilderTest.class.st new file mode 100644 index 0000000..33a0ed5 --- /dev/null +++ b/src/Foliage-Core-Tests/FOTagOverviewBuilderTest.class.st @@ -0,0 +1,141 @@ +Class { + #name : #FOTagOverviewBuilderTest, + #superclass : #TestCase, + #instVars : [ + 'tempDir', + 'site', + 'blog' + ], + #category : #'Foliage-Core-Tests' +} + +{ #category : #running } +FOTagOverviewBuilderTest >> setUp [ + super setUp. + tempDir := FileLocator temp / ('fo-tag-overview-test-', UUID new asString36). + tempDir ensureCreateDirectory. + (tempDir / 'templates') ensureCreateDirectory. + (tempDir / 'templates' / 'blog') writeStreamDo: [ :s | + s nextPutAll: '{{page.title}}{{{ body }}}' ]. + (tempDir / 'templates' / 'blog-post-abstract') writeStreamDo: [ :s | + s nextPutAll: '
{{abstract}}
' ]. + (tempDir / 'templates' / 'blog-post') writeStreamDo: [ :s | + s nextPutAll: '{{page.title}}{{{ body }}}' ]. + site := FOWebSite new + outputPath: (tempDir / 'docs'); + templatePath: (tempDir / 'templates'); + yourself. + blog := FOBlog new title: 'My Blog'; yourself. + site root at: #blog put: blog +] + +{ #category : #running } +FOTagOverviewBuilderTest >> tearDown [ + tempDir ifNotNil: [ tempDir exists ifTrue: [ tempDir deleteAll ] ]. + super tearDown +] + +{ #category : #private } +FOTagOverviewBuilderTest >> makePostTitled: aTitle publishDate: aDateString tags: aTagsArray [ + | post | + post := FOFoliagePage new + meta: (Dictionary new + at: #title put: aTitle; + at: #publishDate put: aDateString; + yourself); + document: (FOMarkdownParser parse: aTitle, ' text.'); + yourself. + post meta at: #tags put: aTagsArray. + ^ post +] + +{ #category : #tests } +FOTagOverviewBuilderTest >> testBuildPagesContainsOnlyThatTagAbstractsNewestFirst [ + | builder aTag pages stream | + blog at: #part1 put: (self makePostTitled: 'Part 1' publishDate: '2026-07-20' tags: #('soil')). + blog at: #unrelated put: (self makePostTitled: 'Unrelated' publishDate: '2026-07-21' tags: #('other')). + blog at: #part2 put: (self makePostTitled: 'Part 2' publishDate: '2026-07-22' tags: #('soil')). + aTag := blog tags detect: [ :t | t slug = 'soil' ]. + builder := FOTagOverviewBuilder new blog: blog; tag: aTag; yourself. + pages := builder buildPages. + self assert: pages size equals: 1. + self assert: pages first title equals: 'Soil'. + stream := WriteStream on: String new. + pages first renderOn: stream. + self assert: stream contents equals: + '
Part 2 text.
Part 1 text.
' +] + +{ #category : #tests } +FOTagOverviewBuilderTest >> testBuildPageShowsHandWrittenAbstractAsIntroOnPageOneOnly [ + | builder aTag pages stream | + site dataPath: (tempDir / 'data'). + (tempDir / 'data' / 'tags') ensureCreateDirectory. + (tempDir / 'data' / 'tags' / 'soil.md') writeStreamDo: [ :s | + s nextPutAll: 'A hand-written intro.' ]. + blog at: #part1 put: (self makePostTitled: 'Part 1' publishDate: '2026-07-20' tags: #('soil')). + blog at: #part2 put: (self makePostTitled: 'Part 2' publishDate: '2026-07-21' tags: #('soil')). + aTag := blog tags detect: [ :t | t slug = 'soil' ]. + builder := FOTagOverviewBuilder new blog: blog; tag: aTag; pageSize: 1; yourself. + pages := builder buildPages. + self assert: pages size equals: 2. + stream := WriteStream on: String new. + pages first renderOn: stream. + self assert: (stream contents includesSubstring: + '

A hand-written intro.

Part 2 text.
'). + self assert: (pages first htmlString includesSubstring: 'href="/blog/tags/soil/page2.html">older'). + self deny: (pages second htmlString includesSubstring: 'hand-written intro') +] + +{ #category : #tests } +FOTagOverviewBuilderTest >> testBuildPageHasNoIntroWhenNoDataFileExists [ + | builder aTag pages stream | + blog at: #part1 put: (self makePostTitled: 'Part 1' publishDate: '2026-07-20' tags: #('soil')). + aTag := blog tags detect: [ :t | t slug = 'soil' ]. + builder := FOTagOverviewBuilder new blog: blog; tag: aTag; yourself. + pages := builder buildPages. + stream := WriteStream on: String new. + pages first renderOn: stream. + self assert: stream contents equals: '
Part 1 text.
' +] + +{ #category : #tests } +FOTagOverviewBuilderTest >> testFewerPostsThanPageSizeYieldsOnePageWithNoNavigationLinks [ + | builder aTag pages | + blog at: #part1 put: (self makePostTitled: 'Part 1' publishDate: '2026-07-20' tags: #('soil')). + aTag := blog tags detect: [ :t | t slug = 'soil' ]. + builder := FOTagOverviewBuilder new blog: blog; tag: aTag; yourself. + pages := builder buildPages. + self assert: pages size equals: 1. + self assert: (pages first htmlString includesSubstring: 'older') not. + self assert: (pages first htmlString includesSubstring: 'newer') not +] + +{ #category : #tests } +FOTagOverviewBuilderTest >> testPaginationSplitsPostsAcrossPagesNewestFirstUnderTagPath [ + "5 posts tagged 'soil', page size 2 -> [mar,feb] [jan,dec] [nov], same + shape as FOBlogOverviewBuilderTest's pagination test but links are rooted + under /blog/tags/soil/ instead of /blog/." + | builder aTag pages | + blog at: #jan put: (self makePostTitled: 'Jan' publishDate: '2020-01-01' tags: #('soil')). + blog at: #feb put: (self makePostTitled: 'Feb' publishDate: '2020-02-01' tags: #('soil')). + blog at: #mar put: (self makePostTitled: 'Mar' publishDate: '2020-03-01' tags: #('soil')). + blog at: #dec put: (self makePostTitled: 'Dec' publishDate: '2019-12-01' tags: #('soil')). + blog at: #nov put: (self makePostTitled: 'Nov' publishDate: '2019-11-01' tags: #('soil')). + aTag := blog tags detect: [ :t | t slug = 'soil' ]. + builder := FOTagOverviewBuilder new blog: blog; tag: aTag; pageSize: 2; yourself. + pages := builder buildPages. + self assert: pages size equals: 3. + + self assert: (pages first htmlString includesSubstring: '
Mar text.
Feb text.
'). + self assert: (pages first htmlString includesSubstring: 'href="/blog/tags/soil/page2.html">older'). + self assert: (pages first htmlString includesSubstring: 'newer') not. + + self assert: (pages second htmlString includesSubstring: '
Jan text.
Dec text.
'). + self assert: (pages second htmlString includesSubstring: 'href="/blog/tags/soil/index.html">newer'). + self assert: (pages second htmlString includesSubstring: 'href="/blog/tags/soil/page3.html">older'). + + self assert: (pages third htmlString includesSubstring: '
Nov text.
'). + self assert: (pages third htmlString includesSubstring: 'href="/blog/tags/soil/page2.html">newer'). + self assert: (pages third htmlString includesSubstring: 'older') not +] diff --git a/src/Foliage-Core-Tests/FOTagTest.class.st b/src/Foliage-Core-Tests/FOTagTest.class.st new file mode 100644 index 0000000..7e6cd4e --- /dev/null +++ b/src/Foliage-Core-Tests/FOTagTest.class.st @@ -0,0 +1,30 @@ +Class { + #name : #FOTagTest, + #superclass : #TestCase, + #category : #'Foliage-Core-Tests' +} + +{ #category : #tests } +FOTagTest >> testTitleFromSlugCapitalizesEachHyphenatedWord [ + self assert: (FOTag titleFromSlug: 'soil') equals: 'Soil'. + self assert: (FOTag titleFromSlug: 'byob-database') equals: 'Byob Database' +] + +{ #category : #tests } +FOTagTest >> testTitleFromSlugHandlesSingleLetterWords [ + self assert: (FOTag titleFromSlug: 'a-b') equals: 'A B' +] + +{ #category : #tests } +FOTagTest >> testTitleDelegatesToClassSideTitleFromSlug [ + | tag | + tag := FOTag new slug: 'soil'; yourself. + self assert: tag title equals: 'Soil' +] + +{ #category : #tests } +FOTagTest >> testPostCountIsPostsSize [ + | tag | + tag := FOTag new posts: #(1 2 3); yourself. + self assert: tag postCount equals: 3 +] diff --git a/src/Foliage-Core/FOBlog.class.st b/src/Foliage-Core/FOBlog.class.st index e0dcf48..b9c9420 100644 --- a/src/Foliage-Core/FOBlog.class.st +++ b/src/Foliage-Core/FOBlog.class.st @@ -140,6 +140,33 @@ FOBlog >> seriesPostBefore: aFOBlogPost [ ifFalse: [ nil ] ] +{ #category : #tags } +FOBlog >> tags [ + "One FOTag per distinct tag slug found among published posts, newest- + active-first: sorted by each tag's most recent post. Unlike #series, + posts within a tag are themselves stored newest-first (no reading order), + so that's posts first here, not posts last." + | unsorted | + unsorted := self tagSlugs asSortedCollection asArray collect: [ :slug | + FOTag new + slug: slug; + posts: (self postsTagged: slug); + blog: self; + yourself ]. + ^ unsorted asSortedCollection: [ :a :b | a posts first publishDate > b posts first publishDate ] +] + +{ #category : #tags } +FOBlog >> tagSlugs [ + ^ self publishedPosts inject: Set new into: [ :set :post | set addAll: post tags; yourself ] +] + +{ #category : #tags } +FOBlog >> postsTagged: aSlug [ + "Newest-first, unlike #postsInSeries: - tags have no reading order." + ^ (self publishedPosts select: [ :post | post tags includes: aSlug ]) sorted: #publishDate descending +] + { #category : #accessing } FOBlog >> buildRssFeed [ ^ (FORSSVisitor new visit: self) diff --git a/src/Foliage-Core/FOBlogPost.class.st b/src/Foliage-Core/FOBlogPost.class.st index 8169e7e..c89837f 100644 --- a/src/Foliage-Core/FOBlogPost.class.st +++ b/src/Foliage-Core/FOBlogPost.class.st @@ -53,6 +53,11 @@ FOBlogPost >> hasSeries [ ^ self series notNil ] +{ #category : #testing } +FOBlogPost >> hasTags [ + ^ self tags notEmpty +] + { #category : #testing } FOBlogPost >> isPost [ ^ true @@ -70,6 +75,17 @@ FOBlogPost >> series [ ^ self meta at: #series ifAbsent: [ nil ] ] +{ #category : #accessing } +FOBlogPost >> tags [ + "Frontmatter 'tags: [a, b]'. A missing key yields no tags; a value that + came through as a plain string (e.g. a forgotten-brackets typo, 'tags: a') + is treated as a single-element list rather than iterated character-by- + character." + | raw | + raw := self meta at: #tags ifAbsent: [ #() ]. + ^ raw isString ifTrue: [ { raw } ] ifFalse: [ raw ] +] + { #category : #'instance creation' } FOBlogPost >> renderContextClass [ ^ FOBlogPostRenderContext diff --git a/src/Foliage-Core/FOBlogPostRenderContext.class.st b/src/Foliage-Core/FOBlogPostRenderContext.class.st index c7f29c6..74e43cd 100644 --- a/src/Foliage-Core/FOBlogPostRenderContext.class.st +++ b/src/Foliage-Core/FOBlogPostRenderContext.class.st @@ -58,3 +58,21 @@ FOBlogPostRenderContext >> seriesPreviousPost [ FOBlogPostRenderContext >> seriesNextPost [ ^ page parent seriesPostAfter: self page ] + +{ #category : #tags } +FOBlogPostRenderContext >> hasTags [ + ^ self page hasTags +] + +{ #category : #tags } +FOBlogPostRenderContext >> tagsHtml [ + "Templates here never iterate raw collections (see FOBlogOverviewBuilder/ + FOSeriesOverviewBuilder - multi-item HTML is always assembled in + Smalltalk). Same idiom for a post's own tag list." + ^ String streamContents: [ :s | + self page tags + do: [ :slug | + s nextPutAll: '', + (FOTag titleFromSlug: slug), '' ] + separatedBy: [ s nextPutAll: ', ' ] ] +] diff --git a/src/Foliage-Core/FOTag.class.st b/src/Foliage-Core/FOTag.class.st new file mode 100644 index 0000000..4faaf1a --- /dev/null +++ b/src/Foliage-Core/FOTag.class.st @@ -0,0 +1,119 @@ +Class { + #name : #FOTag, + #superclass : #Object, + #instVars : [ + 'slug', + 'posts', + 'blog' + ], + #category : #'Foliage-Core-Blog' +} + +{ #category : #'instance creation' } +FOTag class >> titleFromSlug: aSlug [ + "'soil' -> 'Soil', 'byob-database' -> 'Byob Database'. Same zero-config + stand-in as FOSeries class>>titleFromSlug: - deliberately duplicated + rather than shared, see FOTag class comment / roadmap notes." + ^ String streamContents: [ :s | + (aSlug substrings: '-') do: [ :word | + s nextPutAll: word first asUppercase asString. + word size > 1 ifTrue: [ s nextPutAll: (word copyFrom: 2 to: word size) ] ] + separatedBy: [ s nextPut: $ ] ] +] + +{ #category : #accessing } +FOTag >> abstract [ + "A hand-written data/tags/.md overrides the auto-derived abstract, + if present - same mechanism as FOSeries>>abstract. Falls back to the + NEWEST post's abstract (posts first, since tag posts are stored newest- + first) when no such file exists - unlike FOSeries, a tag has no 'part 1', + so the most recent post is the most sensible representative excerpt." + | dataFile | + dataFile := self dataAbstractFile. + ^ dataFile exists + ifTrue: [ self renderDataAbstract: dataFile ] + ifFalse: [ self posts first abstract ] +] + +{ #category : #accessing } +FOTag >> dataAbstractFile [ + ^ (self website dataPath / 'tags' / (slug, '.md')) asFileReference +] + +{ #category : #testing } +FOTag >> hasCustomAbstract [ + ^ self dataAbstractFile exists +] + +{ #category : #accessing } +FOTag >> renderDataAbstract: aFileReference [ + | frontmatter | + frontmatter := FOFrontmatterParser parse: aFileReference contents. + ^ FOPlainTextVisitor render: (FOMarkdownParser parse: frontmatter body) +] + +{ #category : #accessing } +FOTag >> blog [ + ^ blog +] + +{ #category : #accessing } +FOTag >> blog: aFOBlog [ + blog := aFOBlog +] + +{ #category : #accessing } +FOTag >> postCount [ + ^ self posts size +] + +{ #category : #accessing } +FOTag >> pathString [ + ^ blog pathString, '/tags/', slug, '/index.html' +] + +{ #category : #accessing } +FOTag >> posts [ + ^ posts +] + +{ #category : #accessing } +FOTag >> posts: aCollection [ + posts := aCollection +] + +{ #category : #accessing } +FOTag >> slug [ + ^ slug +] + +{ #category : #accessing } +FOTag >> slug: aString [ + slug := aString +] + +{ #category : #accessing } +FOTag >> title [ + ^ self class titleFromSlug: slug +] + +{ #category : #accessing } +FOTag >> website [ + ^ blog website +] + +{ #category : #accessing } +FOTag >> abstractLayout [ + ^ 'tag-abstract' +] + +{ #category : #accessing } +FOTag >> abstractTemplate [ + ^ (self website templatePathForLayout: self abstractLayout) asMustacheTemplate +] + +{ #category : #rendering } +FOTag >> renderAbstract [ + ^ (FOTagRenderContext new website: self website; tag: self; yourself) + renderTemplate: self abstractTemplate +] diff --git a/src/Foliage-Core/FOTagIndexBuilder.class.st b/src/Foliage-Core/FOTagIndexBuilder.class.st new file mode 100644 index 0000000..38f9a31 --- /dev/null +++ b/src/Foliage-Core/FOTagIndexBuilder.class.st @@ -0,0 +1,30 @@ +Class { + #name : #FOTagIndexBuilder, + #superclass : #Object, + #instVars : [ + 'blog' + ], + #category : #'Foliage-Core-Blog' +} + +{ #category : #accessing } +FOTagIndexBuilder >> blog: aBlog [ + blog := aBlog +] + +{ #category : #building } +FOTagIndexBuilder >> buildPage [ + "One entry per tag, each rendered through the 'tag-abstract' template via + FOTag>>renderAbstract (title/pathString/abstract/postCount) - same + approach as FOSeriesIndexBuilder, listing blog tags (newest-active-first, + see FOBlog>>tags) instead of individual posts." + | html | + html := String streamContents: [ :s | + blog tags do: [ :aTag | s nextPutAll: aTag renderAbstract ] ]. + ^ FOFoliagePage new + parent: blog parent; + title: 'Tags'; + layout: blog layout; + bodyHtml: html; + yourself +] diff --git a/src/Foliage-Core/FOTagOverviewBuilder.class.st b/src/Foliage-Core/FOTagOverviewBuilder.class.st new file mode 100644 index 0000000..53b32ee --- /dev/null +++ b/src/Foliage-Core/FOTagOverviewBuilder.class.st @@ -0,0 +1,98 @@ +Class { + #name : #FOTagOverviewBuilder, + #superclass : #Object, + #instVars : [ + 'blog', + 'tag', + 'pageSize' + ], + #category : #'Foliage-Core-Blog' +} + +{ #category : #accessing } +FOTagOverviewBuilder >> blog: aBlog [ + blog := aBlog +] + +{ #category : #accessing } +FOTagOverviewBuilder >> tag: aFOTag [ + tag := aFOTag +] + +{ #category : #accessing } +FOTagOverviewBuilder >> defaultPageSize [ + ^ 10 +] + +{ #category : #accessing } +FOTagOverviewBuilder >> pageSize [ + ^ pageSize ifNil: [ self defaultPageSize ] +] + +{ #category : #accessing } +FOTagOverviewBuilder >> pageSize: anInteger [ + pageSize := anInteger +] + +{ #category : #building } +FOTagOverviewBuilder >> buildPages [ + "Paginated like FOBlogOverviewBuilder, unlike FOSeriesOverviewBuilder's + single-page assumption - a tag can accumulate many posts over the + lifetime of a blog, unlike a series. tag posts is already newest-first + (set by FOBlog>>tags), so no re-sorting needed here." + | posts pageCount | + posts := tag posts. + pageCount := (posts size / self pageSize) ceiling max: 1. + ^ (1 to: pageCount) collect: [ :i | + self + buildPageIndex: i + ofTotal: pageCount + posts: posts ] +] + +{ #category : #building } +FOTagOverviewBuilder >> buildPageIndex: aPageNumber ofTotal: aPageCount posts: allPosts [ + "Same bodyHtml/outer-template approach as FOBlogOverviewBuilder. A hand- + written data/tags/.md abstract, if present, is shown as an intro + paragraph - only on page 1, not repeated on every page." + | start stop pagePosts html | + start := (aPageNumber - 1) * self pageSize + 1. + stop := (aPageNumber * self pageSize) min: allPosts size. + pagePosts := start <= allPosts size + ifTrue: [ allPosts copyFrom: start to: stop ] + ifFalse: [ #() ]. + html := String streamContents: [ :s | + (aPageNumber = 1 and: [ tag hasCustomAbstract ]) + ifTrue: [ s nextPutAll: '

', tag abstract, '

' ]. + pagePosts do: [ :post | s nextPutAll: post renderAbstract ]. + s nextPutAll: (self navigationHtmlForPageIndex: aPageNumber ofTotal: aPageCount) ]. + ^ FOFoliagePage new + parent: blog parent; + title: tag title; + layout: blog layout; + bodyHtml: html; + yourself +] + +{ #category : #building } +FOTagOverviewBuilder >> navigationHtmlForPageIndex: aPageNumber ofTotal: aPageCount [ + "Same layout/wording as FOBlogOverviewBuilder>>navigationHtmlForPageIndex: + ofTotal:, but rooted under this tag's own folder + (blog/tags//...) rather than the blog root - so it can't reuse that + method directly. Filename computation itself (index.html/pageN.html) is + reused via FOBlogOverviewBuilder class>>pageFilenameFor:, which is + stateless and tag-agnostic." + | base newerLink olderLink | + base := blog pathString, '/tags/', tag slug. + newerLink := aPageNumber > 1 + ifTrue: [ '<-- newer' ] + ifFalse: [ '' ]. + olderLink := aPageNumber < aPageCount + ifTrue: [ 'older -->' ] + ifFalse: [ '' ]. + (newerLink isEmpty and: [ olderLink isEmpty ]) ifTrue: [ ^ '' ]. + ^ '
+
', newerLink, '
+
', olderLink, '
+
' +] diff --git a/src/Foliage-Core/FOTagRenderContext.class.st b/src/Foliage-Core/FOTagRenderContext.class.st new file mode 100644 index 0000000..3805b54 --- /dev/null +++ b/src/Foliage-Core/FOTagRenderContext.class.st @@ -0,0 +1,43 @@ +Class { + #name : #FOTagRenderContext, + #superclass : #FORenderContext, + #instVars : [ + 'tag' + ], + #category : #'Foliage-Core-Blog' +} + +{ #category : #accessing } +FOTagRenderContext >> abstract [ + ^ tag abstract +] + +{ #category : #accessing } +FOTagRenderContext >> layout [ + ^ tag abstractLayout +] + +{ #category : #accessing } +FOTagRenderContext >> postCount [ + ^ tag postCount +] + +{ #category : #accessing } +FOTagRenderContext >> pathString [ + ^ tag pathString +] + +{ #category : #accessing } +FOTagRenderContext >> tag [ + ^ tag +] + +{ #category : #accessing } +FOTagRenderContext >> tag: aFOTag [ + tag := aFOTag +] + +{ #category : #accessing } +FOTagRenderContext >> title [ + ^ tag title +] diff --git a/src/Foliage-Frontmatter-Tests/FOFrontmatterParserTest.class.st b/src/Foliage-Frontmatter-Tests/FOFrontmatterParserTest.class.st index 9aea266..7bb3118 100644 --- a/src/Foliage-Frontmatter-Tests/FOFrontmatterParserTest.class.st +++ b/src/Foliage-Frontmatter-Tests/FOFrontmatterParserTest.class.st @@ -65,6 +65,41 @@ FOFrontmatterParserTest >> testNoFrontmatterMeansWholeFileIsBody [ self assert: result body equals: original ] +{ #category : #tests } +FOFrontmatterParserTest >> testBracketListValueBecomesACollectionOfTrimmedElements [ + | result | + result := FOFrontmatterParser parse: 'tags: [soil, database]', self nl, self nl, 'body text'. + self assert: (result at: #tags) equals: #('soil' 'database') +] + +{ #category : #tests } +FOFrontmatterParserTest >> testBracketListValueTrimsWhitespaceAroundElements [ + | result | + result := FOFrontmatterParser parse: 'tags: [ soil , database ]', self nl, self nl, 'body text'. + self assert: (result at: #tags) equals: #('soil' 'database') +] + +{ #category : #tests } +FOFrontmatterParserTest >> testBracketListValueStripsQuotesFromElements [ + | result | + result := FOFrontmatterParser parse: 'tags: ["soil", "database"]', self nl, self nl, 'body text'. + self assert: (result at: #tags) equals: #('soil' 'database') +] + +{ #category : #tests } +FOFrontmatterParserTest >> testEmptyBracketListValueBecomesAnEmptyCollection [ + | result | + result := FOFrontmatterParser parse: 'tags: []', self nl, self nl, 'body text'. + self assert: (result at: #tags) equals: #() +] + +{ #category : #tests } +FOFrontmatterParserTest >> testSingleElementBracketListValue [ + | result | + result := FOFrontmatterParser parse: 'tags: [soil]', self nl, self nl, 'body text'. + self assert: (result at: #tags) equals: #('soil') +] + { #category : #tests } FOFrontmatterParserTest >> testProseWithColonIsNotMistakenForFrontmatter [ | result original | diff --git a/src/Foliage-Frontmatter/FOFrontmatterParser.class.st b/src/Foliage-Frontmatter/FOFrontmatterParser.class.st index e821fd8..45e1531 100644 --- a/src/Foliage-Frontmatter/FOFrontmatterParser.class.st +++ b/src/Foliage-Frontmatter/FOFrontmatterParser.class.st @@ -28,7 +28,7 @@ FOFrontmatterParser >> parse: aString [ ifFalse: [ key := (line copyFrom: 1 to: colonIndex - 1) trimBoth. value := (line copyFrom: colonIndex + 1 to: line size) trimBoth. - properties at: key asSymbol put: (self stripQuotes: value) ] ] ] ]. + properties at: key asSymbol put: (self parseValue: value) ] ] ] ]. ^ FOFrontmatter properties: properties body: (bodyStartIndex isNil @@ -43,6 +43,22 @@ FOFrontmatterParser >> isValidKey: aSubstring [ ^ trimmed isEmpty not and: [ (trimmed anySatisfy: [ :c | c isSeparator ]) not ] ] +{ #category : #private } +FOFrontmatterParser >> parseValue: aString [ + "A value that looks like '[a, b, c]' becomes a collection of trimmed, + unquoted elements - generic (not tied to any specific key) so future + multi-value frontmatter fields work without another parser change. No + comma-escaping inside elements is supported, matching this parser's + existing 'only what real content needs' scope." + | trimmed inner | + trimmed := aString trimBoth. + (trimmed size >= 2 and: [ trimmed first = $[ and: [ trimmed last = $] ] ]) + ifFalse: [ ^ self stripQuotes: trimmed ]. + inner := (trimmed copyFrom: 2 to: trimmed size - 1) trimBoth. + inner isEmpty ifTrue: [ ^ #() ]. + ^ (inner substrings: ',') collect: [ :item | self stripQuotes: item trimBoth ] +] + { #category : #private } FOFrontmatterParser >> stripQuotes: aString [ (aString size >= 2 and: [ aString first = $" and: [ aString last = $" ] ]) diff --git a/src/Foliage-Publisher-Tests/FOPublisherTest.class.st b/src/Foliage-Publisher-Tests/FOPublisherTest.class.st index 126ea3a..a049c84 100644 --- a/src/Foliage-Publisher-Tests/FOPublisherTest.class.st +++ b/src/Foliage-Publisher-Tests/FOPublisherTest.class.st @@ -29,6 +29,8 @@ FOPublisherTest >> setUp [ s nextPutAll: '
{{abstract}}
' ]. (tempDir / 'source' / 'templates' / 'series-abstract') writeStreamDo: [ :s | s nextPutAll: '
{{title}}: {{abstract}}
' ]. + (tempDir / 'source' / 'templates' / 'tag-abstract') writeStreamDo: [ :s | + s nextPutAll: '
{{title}}: {{abstract}}
' ]. (tempDir / 'source' / 'templates' / 'rss.xml') writeStreamDo: [ :s | s nextPutAll: '{{# channels }}{{# items }}{{title}}{{/ items }}{{/ channels }}' ]. publisher := FOPublisher new @@ -118,6 +120,62 @@ FOPublisherTest >> testPublishCreatesNoSeriesFolderWhenNoPostHasASeries [ self deny: (tempDir / 'generated' / 'blog' / 'series') exists ] +{ #category : #tests } +FOPublisherTest >> testPublishGeneratesTagOverviewPage [ + | html | + (tempDir / 'source' / 'blog' / 'soil1.md') writeStreamDo: [ :s | + s nextPutAll: 'title: Soil post 1', String lf, + 'layout: blog-post', String lf, + 'publishDate: 2026-07-20', String lf, + 'tags: [soil, database]', String lf, + String lf, + 'Soil post 1 text.' ]. + (tempDir / 'source' / 'blog' / 'soil2.md') writeStreamDo: [ :s | + s nextPutAll: 'title: Soil post 2', String lf, + 'layout: blog-post', String lf, + 'publishDate: 2026-07-21', String lf, + 'tags: [soil]', String lf, + String lf, + 'Soil post 2 text.' ]. + publisher publish. + self assert: (tempDir / 'generated' / 'blog' / 'tags' / 'soil' / 'index.html') exists. + html := (tempDir / 'generated' / 'blog' / 'tags' / 'soil' / 'index.html') contents. + self assert: (html includesSubstring: 'Soil'). + self assert: (html includesSubstring: 'Soil post 1 text.'). + self assert: (html includesSubstring: 'Soil post 2 text.'). + self assert: (html indexOfSubCollection: 'Soil post 2 text.') < + (html indexOfSubCollection: 'Soil post 1 text.'). + self assert: (tempDir / 'generated' / 'blog' / 'tags' / 'database' / 'index.html') exists. + self assert: (tempDir / 'generated' / 'blog' / 'tags' / 'index.html') exists. + self assert: ((tempDir / 'generated' / 'blog' / 'tags' / 'index.html') contents + includesSubstring: 'Soil') +] + +{ #category : #tests } +FOPublisherTest >> testPublishPaginatesTagOverviewPastDefaultPageSizeOfTen [ + 1 to: 11 do: [ :n | + (tempDir / 'source' / 'blog' / ('soil', n printString, '.md')) writeStreamDo: [ :s | + s nextPutAll: 'title: Soil post ', n printString, String lf, + 'layout: blog-post', String lf, + 'publishDate: 2026-08-', (n < 10 ifTrue: [ '0', n printString ] ifFalse: [ n printString ]), String lf, + 'tags: [soil]', String lf, + String lf, + 'Soil post ', n printString, ' text.' ] ]. + publisher publish. + self assert: (tempDir / 'generated' / 'blog' / 'tags' / 'soil' / 'index.html') exists. + self assert: (tempDir / 'generated' / 'blog' / 'tags' / 'soil' / 'page2.html') exists. + self assert: ((tempDir / 'generated' / 'blog' / 'tags' / 'soil' / 'index.html') contents + includesSubstring: 'href="/blog/tags/soil/page2.html">older'). + self assert: ((tempDir / 'generated' / 'blog' / 'tags' / 'soil' / 'page2.html') contents + includesSubstring: 'href="/blog/tags/soil/index.html">newer') +] + +{ #category : #tests } +FOPublisherTest >> testPublishCreatesNoTagsFolderWhenNoPostHasATag [ + publisher publish. + self deny: (tempDir / 'generated' / 'blog' / 'tags') exists +] + { #category : #tests } FOPublisherTest >> testDefaults [ | fresh | diff --git a/src/Foliage-Publisher/FOPublisher.class.st b/src/Foliage-Publisher/FOPublisher.class.st index 92a3e94..45c84fe 100644 --- a/src/Foliage-Publisher/FOPublisher.class.st +++ b/src/Foliage-Publisher/FOPublisher.class.st @@ -93,7 +93,7 @@ FOPublisher >> publish [ allFiles := site contentPath allFiles. site importer readAll: allFiles. self blogs do: [ :assoc | - | blog seriesFolder | + | blog seriesFolder tagsFolder | blog := site / assoc key. blog title: assoc value. blog overviewPages doWithIndex: [ :page :i | @@ -105,7 +105,15 @@ FOPublisher >> publish [ blog series do: [ :aSeries | seriesFolder at: (aSeries slug, '.html') asSymbol - put: (FOSeriesOverviewBuilder new blog: blog; series: aSeries; buildPage) ] ] ]. + put: (FOSeriesOverviewBuilder new blog: blog; series: aSeries; buildPage) ] ]. + blog tags ifNotEmpty: [ + tagsFolder := blog resolvePath: (RelativePath from: 'tags'). + tagsFolder at: #'index.html' put: (FOTagIndexBuilder new blog: blog; buildPage). + blog tags do: [ :aTag | + | tagFolder | + tagFolder := tagsFolder resolvePath: (RelativePath from: aTag slug). + (FOTagOverviewBuilder new blog: blog; tag: aTag; buildPages) doWithIndex: [ :page :i | + tagFolder at: (FOBlogOverviewBuilder pageFilenameFor: i) put: page ] ] ] ]. site publish ]