Skip to content

Commit

Permalink
context files
Browse files Browse the repository at this point in the history
  • Loading branch information
tib committed May 10, 2024
1 parent 3bc81b0 commit 196e8de
Show file tree
Hide file tree
Showing 20 changed files with 329 additions and 64 deletions.
13 changes: 13 additions & 0 deletions Sources/Toucan/Contexts/MetadataContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File 2.swift
//
//
// Created by Tibor Bodecs on 10/05/2024.
//

struct MetadataContext {
let permalink: String
let title: String
let description: String
let imageUrl: String?
}
12 changes: 12 additions & 0 deletions Sources/Toucan/Contexts/PageContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// File 2.swift
//
//
// Created by Tibor Bodecs on 10/05/2024.
//

struct PageContext<T> {
let site: SiteContext
let metadata: MetadataContext
let content: T
}
14 changes: 14 additions & 0 deletions Sources/Toucan/Contexts/SiteContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// File 2.swift
//
//
// Created by Tibor Bodecs on 10/05/2024.
//

struct SiteContext {
let baseUrl: String
let name: String
let tagline: String
let imageUrl: String?
let language: String?
}
13 changes: 13 additions & 0 deletions Sources/Toucan/Contexts/_partials/FigureContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File 2.swift
//
//
// Created by Tibor Bodecs on 10/05/2024.
//

struct FigureContext {
let src: String
let darkSrc: String?
let alt: String?
let title: String?
}
13 changes: 13 additions & 0 deletions Sources/Toucan/Contexts/_partials/PostContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File 2.swift
//
//
// Created by Tibor Bodecs on 10/05/2024.
//

struct PostContext {
let title: String
let exceprt: String
let date: String
let figure: FigureContext?
}
11 changes: 11 additions & 0 deletions Sources/Toucan/Contexts/_partials/TagContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// File 2.swift
//
//
// Created by Tibor Bodecs on 10/05/2024.
//

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

import Foundation
15 changes: 15 additions & 0 deletions Sources/Toucan/Contexts/page/single/SinglePostContext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// File 2.swift
//
//
// Created by Tibor Bodecs on 10/05/2024.
//

struct SinglePostContext {
let title: String
let exceprt: String
let date: String
let figure: FigureContext?
let tags: [TagContext]
let body: String
}
145 changes: 107 additions & 38 deletions Tests/ToucanTests/TemplateLibraryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,6 @@
import XCTest
@testable import Toucan

struct SiteContext {
let baseUrl: String
let name: String
let tagline: String
let imageUrl: String?
let language: String?
}

struct MetadataContext {
let permalink: String
let title: String
let description: String
let imageUrl: String?
}

struct TemplateContext<T> {
let site: SiteContext
let metadata: MetadataContext
let contents: T
}

struct PostCardContext {
let title: String
let exceprt: String
let date: String
let figure: FigureContext?
}

struct FigureContext {
let src: String
let darkSrc: String?
let alt: String?
let title: String?
}

final class TemplateLibraryTests: XCTestCase {

func testHomePageTemplate() throws {
Expand All @@ -66,7 +31,7 @@ final class TemplateLibraryTests: XCTestCase {

let templates = try TemplateLibrary(templatesUrl: templatesUrl)

let context = TemplateContext(
let context = PageContext(
site: .init(
baseUrl: site.baseUrl,
name: site.name,
Expand All @@ -80,8 +45,8 @@ final class TemplateLibraryTests: XCTestCase {
description: "doloor sit amet",
imageUrl: nil
),
contents: [
PostCardContext(
content: [
PostContext(
title: "foo",
exceprt: "foo",
date: "2022",
Expand All @@ -103,4 +68,108 @@ final class TemplateLibraryTests: XCTestCase {
print(res!)
}

func testSinglePageTemplate() throws {

let path =
"/"
+ #file
.split(separator: "/")
.dropLast(2)
.joined(separator: "/")

let baseUrl = URL(fileURLWithPath: path)
let srcUrl = baseUrl.appendingPathComponent("src")
let contentsUrl = srcUrl.appendingPathComponent("contents")
let templatesUrl = srcUrl.appendingPathComponent("templates")

let loader = ContentLoader(path: contentsUrl.path)

let site = try loader.load()

let templates = try TemplateLibrary(templatesUrl: templatesUrl)

let context = PageContext(
site: .init(
baseUrl: site.baseUrl,
name: site.name,
tagline: site.tagline,
imageUrl: site.imageUrl,
language: site.language
),
metadata: .init(
permalink: site.baseUrl + "slug-comes-here/",
title: "Lorem ipsum",
description: "doloor sit amet",
imageUrl: nil
),
content: "just a <b>simple</b> page"

)

let res = try templates.render(
template: "pages.single.page",
with: context
)
print(res!)
}

func testSinglePostTemplate() throws {

let path =
"/"
+ #file
.split(separator: "/")
.dropLast(2)
.joined(separator: "/")

let baseUrl = URL(fileURLWithPath: path)
let srcUrl = baseUrl.appendingPathComponent("src")
let contentsUrl = srcUrl.appendingPathComponent("contents")
let templatesUrl = srcUrl.appendingPathComponent("templates")

let loader = ContentLoader(path: contentsUrl.path)

let site = try loader.load()

let templates = try TemplateLibrary(templatesUrl: templatesUrl)

let context = PageContext(
site: .init(
baseUrl: site.baseUrl,
name: site.name,
tagline: site.tagline,
imageUrl: site.imageUrl,
language: site.language
),
metadata: .init(
permalink: site.baseUrl + "slug-comes-here/",
title: "Lorem ipsum",
description: "doloor sit amet",
imageUrl: nil
),
content: SinglePostContext(
title: "foo",
exceprt: "bar",
date: "2025",
figure: .init(
src: "http://lorempixel.com/light.jpg",
darkSrc: "http://lorempixel.com/dark.jpg",
alt: "foo",
title: "lorem foo"
),
tags: [
.init(permalink: "https://bb.com/foo", name: "Foo")
],
body: "<b>lorem ipsum</b> dolor sit amet"
)

)

let res = try templates.render(
template: "pages.single.post",
with: context
)
print(res!)
}

}
2 changes: 1 addition & 1 deletion Tests/src/templates/_partials/tag.mustache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<a href="{link}">{tag}</a>
<a href="{{permalink}}">{{name}}</a>
3 changes: 0 additions & 3 deletions Tests/src/templates/page.mustache

This file was deleted.

20 changes: 20 additions & 0 deletions Tests/src/templates/pages/authors.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{<index}}

{{$main}}
<div id="posts" class="wrapper">
<header>
<h1>{{site.name}}</h1>
<p>{{site.tagline}}</p>
<br>
<a href="mailto:[email protected]">Contact us</a>
</header>

<div class="grid">
{{#content}}
{{> _partials.post}}
{{/content}}
</div>
</div>
{{/main}}

{{/index}}
4 changes: 2 additions & 2 deletions Tests/src/templates/pages/home.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</header>

<div class="grid">
{{#contents}}
{{#content}}
{{> _partials.post}}
{{/contents}}
{{/content}}
</div>
</div>
{{/main}}
Expand Down
20 changes: 20 additions & 0 deletions Tests/src/templates/pages/posts.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{<index}}

{{$main}}
<div id="posts" class="wrapper">
<header>
<h1>{{site.name}}</h1>
<p>{{site.tagline}}</p>
<br>
<a href="mailto:[email protected]">Contact us</a>
</header>

<div class="grid">
{{#content}}
{{> _partials.post}}
{{/content}}
</div>
</div>
{{/main}}

{{/index}}
9 changes: 9 additions & 0 deletions Tests/src/templates/pages/single/author.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{<index}}

{{$main}}
<div class="author content-wrapper">
{{& content}}
</div>
{{/main}}

{{/index}}
9 changes: 9 additions & 0 deletions Tests/src/templates/pages/single/page.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{<index}}

{{$main}}
<div class="page content-wrapper">
{{& content}}
</div>
{{/main}}

{{/index}}
33 changes: 33 additions & 0 deletions Tests/src/templates/pages/single/post.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{<index}}

{{$main}}
{{#content}}
<article>
<header>
<section id="post-header" class="content-wrapper">
<time datetime="{{date}}">{{date}}</time>
<h1 class="title">{{title}}</h1>
<p class="excerpt">{{excerpt}}</p>
{{#tags}}
<div class="meta">
{{> _partials.tag}}
</div>
{{/tags}}
</section>

{{> _partials.figure}}

</header>

<section id="contents" class="content-wrapper">

{{& body}}

</section>

</article>
{{/content}}
{{/main}}

{{/index}}

Loading

0 comments on commit 196e8de

Please sign in to comment.