Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tib committed May 13, 2024
1 parent 6953c5f commit 630e87f
Show file tree
Hide file tree
Showing 35 changed files with 277 additions and 129 deletions.
66 changes: 41 additions & 25 deletions Sources/Toucan/Generator/SiteGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,23 +227,28 @@ struct SiteGenerator {
let context = PageContext(
site: site.getContext(),
metadata: .init(
permalink: site.permalink("posts/\(pageIndex)"),
permalink: site.permalink("posts/page/\(pageIndex)"),
title: "posts page 1",
description: "posts page 1 description",
imageUrl: nil
),
content: PostsContext(
posts: posts.map { $0.getContext() },
pagination: (0..<count)
.map { idx in
let currentPageIndex = idx + 1
return .init(
name: "\(currentPageIndex)",
url: site.permalink("posts/\(currentPageIndex)"),
isCurrent: index == idx
)
}
)
posts: .init(posts.map { $0.getContext() }),
pagination: .init(
(0..<count)
.map { idx in
let currentPageIndex = idx + 1
return .init(
name: "\(currentPageIndex)",
url: site.permalink(
"posts/page/\(currentPageIndex)"
),
isCurrent: index == idx
)
}
)
),
userDefined: [:]
)

try templates.render(
Expand Down Expand Up @@ -272,8 +277,11 @@ struct SiteGenerator {
content: SingleTagContext(
name: tag.metatags.title,
description: tag.metatags.description,
posts: site.postsBy(tagId: tag.id).map { $0.getContext() }
)
posts: .init(
site.postsBy(tagId: tag.id).map { $0.getContext() }
)
),
userDefined: [:]
)

try templates.render(
Expand Down Expand Up @@ -301,8 +309,11 @@ struct SiteGenerator {
content: SingleAuthorContext(
name: author.metatags.title,
description: author.metatags.description,
posts: site.postsBy(authorId: author.id).map { $0.getContext() }
)
posts: .init(
site.postsBy(authorId: author.id).map { $0.getContext() }
)
),
userDefined: [:]
)

try templates.render(
Expand Down Expand Up @@ -337,11 +348,12 @@ struct SiteGenerator {
alt: post.metatags.title,
title: post.metatags.title
),
tags: [
tags: .init([
.init(permalink: site.permalink("foo"), name: "Foo")
],
]),
body: body
)
),
userDefined: [:]
)

try templates.render(
Expand All @@ -354,18 +366,22 @@ struct SiteGenerator {
// MARK: -

func renderHomePage(_ templates: TemplateLibrary) throws {

let page = site.page(id: "home")

let context = PageContext(
site: site.getContext(),
metadata: .init(
permalink: site.permalink(""),
title: "home page",
description: "home page description",
title: page?.metatags.title ?? "Home",
description: page?.metatags.description ?? "Home page",
imageUrl: nil
),
content: PostsContext(
posts: [],
pagination: []
)
content: HomeContext(
// TODO: sort by & first N
posts: .init(site.posts.map { $0.getContext() })
),
userDefined: page?.variables ?? [:]
)

let indexUrl = outputUrl.appendingPathComponent("index.html")
Expand Down
4 changes: 4 additions & 0 deletions Sources/Toucan/Models/Site.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ extension Site {
posts.filter { $0.authorIds.contains(authorId) }
}

func page(id: String) -> Page? {
pages.filter { $0.id == id }.first
}

}
17 changes: 17 additions & 0 deletions Sources/Toucan/TemplateLibrary/Contexts/ArrayContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// File.swift
//
//
// Created by Tibor Bodecs on 13/05/2024.
//

struct ArrayContext<T> {

let elements: [T]
let hasElements: Bool

init(_ elements: [T]) {
self.elements = elements
self.hasElements = !elements.isEmpty
}
}
1 change: 1 addition & 0 deletions Sources/Toucan/TemplateLibrary/Contexts/PageContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ struct PageContext<T> {
let site: SiteContext
let metadata: MetadataContext
let content: T
let userDefined: [String: String]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// File.swift
//
//
// Created by Tibor Bodecs on 13/05/2024.
//

struct AuthorContext {
let permalink: String
let name: String
}
10 changes: 10 additions & 0 deletions Sources/Toucan/TemplateLibrary/Contexts/page/AuthorsContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// File.swift
//
//
// Created by Tibor Bodecs on 13/05/2024.
//

struct AuthorsContext {
let authors: ArrayContext<AuthorContext>
}
10 changes: 10 additions & 0 deletions Sources/Toucan/TemplateLibrary/Contexts/page/HomeContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// File.swift
//
//
// Created by Tibor Bodecs on 13/05/2024.
//

struct HomeContext {
let posts: ArrayContext<PostContext>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
//

struct PostsContext {
let posts: [PostContext]
let pagination: [PaginationContext]
let posts: ArrayContext<PostContext>
let pagination: ArrayContext<PaginationContext>
}
10 changes: 10 additions & 0 deletions Sources/Toucan/TemplateLibrary/Contexts/page/TagsContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// File.swift
//
//
// Created by Tibor Bodecs on 13/05/2024.
//

struct TagsContext {
let tags: ArrayContext<TagContext>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
struct SingleAuthorContext {
let name: String
let description: String
let posts: [PostContext]
let posts: ArrayContext<PostContext>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ struct SinglePostContext {
let exceprt: String
let date: String
let figure: FigureContext?
let tags: [TagContext]
let tags: ArrayContext<TagContext>
let body: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
struct SingleTagContext {
let name: String
let description: String
let posts: [PostContext]
let posts: ArrayContext<PostContext>
}
9 changes: 9 additions & 0 deletions Sources/Toucan/TemplateLibrary/TemplateLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
import Foundation
import Mustache

extension String {

func minifyHTML() -> String {
self
}
}

struct TemplateLibrary {

enum Error: Swift.Error {
Expand Down Expand Up @@ -70,10 +77,12 @@ struct TemplateLibrary {
object,
withTemplate: template
)?
.minifyHTML()
.write(
to: destination,
atomically: true,
encoding: .utf8
)
}

}
39 changes: 3 additions & 36 deletions Sources/Toucan/Toucan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Created by Tibor Bodecs on 03/05/2024.
//

import Foundation

/// A static site generator.
public struct Toucan {

Expand All @@ -31,42 +33,7 @@ public struct Toucan {
public func generate(
_ baseUrl: String?
) throws {
//
// var toucanFilesKit = ToucanFilesKit()
// try toucanFilesKit.createURLs(inputUrl)
// try toucanFilesKit.createOutputs(outputUrl)
// try toucanFilesKit.createInfo(needToCopy: true)
//
// var toucanContentKit = ToucanContentKit()
// try toucanContentKit.create(
// baseUrl: baseUrl,
// contentsUrl: toucanFilesKit.contentsUrl,
// templatesUrl: toucanFilesKit.templatesUrl,
// postFileInfos: toucanFilesKit.postFileInfos,
// pageFileInfos: toucanFilesKit.pageFileInfos
// )
//
// for post in toucanContentKit.posts {
// let content = try post.generate()
// try toucanFilesKit.savePostContentToFile(post.slug, content)
// }
//
// for page in toucanContentKit.pages {
// let content = try page.generate()
// try toucanFilesKit.savePageContentToFile(page.slug, content)
// }
//
// let homeContent = try toucanContentKit.home?.generate()
// try toucanFilesKit.saveHomeContentToFile(homeContent)
//
// let notFoundContent = try toucanContentKit.notFound?.generate()
// try toucanFilesKit.saveNotFoundContentToFile(notFoundContent)
//
// let rssContent = try toucanContentKit.rss?.generate()
// try toucanFilesKit.saveRSSContentToFile(rssContent)
//
// let sitemapContent = try toucanContentKit.sitemap?.generate()
// try toucanFilesKit.saveSiteMapContentToFile(sitemapContent)

}

}
2 changes: 1 addition & 1 deletion Tests/ToucanTests/ContentLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class ContentLoaderTests: XCTestCase {
let site = try loader.load()

XCTAssertEqual(site.posts.count, 3)
XCTAssertEqual(site.pages.count, 2)
XCTAssertEqual(site.pages.count, 3)
XCTAssertEqual(site.authors.count, 2)
XCTAssertEqual(site.tags.count, 2)
}
Expand Down
24 changes: 24 additions & 0 deletions Tests/ToucanTests/MinifyHTMLTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// File.swift
//
//
// Created by Tibor Bodecs on 13/05/2024.
//

import Foundation

import XCTest
@testable import Toucan

final class MinifyHTMLTests: XCTestCase {

func testMinify() throws {

let html =
"<html> <body> <h1>Hello, world!</h1> </body> </html>"
let minifiedHTML = html.minifyHTML()
print(minifiedHTML)

}

}
2 changes: 1 addition & 1 deletion Tests/ToucanTests/SiteGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class SiteGeneratorTests: XCTestCase {
let site = try loader.load()

XCTAssertEqual(site.posts.count, 3)
XCTAssertEqual(site.pages.count, 2)
XCTAssertEqual(site.pages.count, 3)
XCTAssertEqual(site.authors.count, 2)
XCTAssertEqual(site.tags.count, 2)

Expand Down
15 changes: 8 additions & 7 deletions Tests/ToucanTests/TemplateLibraryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ final class TemplateLibraryTests: XCTestCase {
title: "lorem foo"
)
)
]

],
userDefined: [:]
)

let res = try templates.render(
Expand Down Expand Up @@ -102,7 +102,8 @@ final class TemplateLibraryTests: XCTestCase {
description: "doloor sit amet",
imageUrl: nil
),
content: "just a <b>simple</b> page"
content: "just a <b>simple</b> page",
userDefined: [:]

)

Expand Down Expand Up @@ -157,12 +158,12 @@ final class TemplateLibraryTests: XCTestCase {
alt: "foo",
title: "lorem foo"
),
tags: [
tags: .init([
.init(permalink: "https://bb.com/foo", name: "Foo")
],
]),
body: "<b>lorem ipsum</b> dolor sit amet"
)

),
userDefined: [:]
)

let res = try templates.render(
Expand Down
3 changes: 1 addition & 2 deletions Tests/dist/authors/nandi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ <h1>Nandi</h1>
<p></p>
</header>

<div class="grid">
</div>

</div>


Expand Down
Loading

0 comments on commit 630e87f

Please sign in to comment.