mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-04-27 14:57:40 +00:00
WIP: Simplify by removing known pages for now
This commit is contained in:
parent
4785f241c8
commit
0091566f00
10 changed files with 27 additions and 220 deletions
|
|
@ -88,47 +88,14 @@ public final class Generator {
|
||||||
let bodyMarkdown = try String(contentsOf: sourceURL, encoding: .utf8)
|
let bodyMarkdown = try String(contentsOf: sourceURL, encoding: .utf8)
|
||||||
let bodyHTML = mdParser.html(from: bodyMarkdown).trimmingCharacters(in: .whitespacesAndNewlines)
|
let bodyHTML = mdParser.html(from: bodyMarkdown).trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
let metadata = try markdownMetadata(from: sourceURL)
|
let metadata = try markdownMetadata(from: sourceURL)
|
||||||
let context = templateContext(url: sourceURL, metadata: metadata, body: bodyHTML)
|
let page = Page(metadata: metadata)
|
||||||
let siteHTML = try templateRenderer.renderTemplate(name: "\(context.template).html", context: context.dictionary)
|
let context = TemplateContext(site: site, page: page, metadata: metadata, body: bodyHTML)
|
||||||
try siteHTML.write(to: targetURL, atomically: true, encoding: .utf8)
|
let pageHTML = try templateRenderer.renderTemplate(name: "\(context.template).html", context: context.dictionary)
|
||||||
|
try pageHTML.write(to: targetURL, atomically: true, encoding: .utf8)
|
||||||
}
|
}
|
||||||
|
|
||||||
func markdownMetadata(from url: URL) throws -> [String: String] {
|
func markdownMetadata(from url: URL) throws -> [String: String] {
|
||||||
let md = try String(contentsOf: url, encoding: .utf8)
|
let md = try String(contentsOf: url, encoding: .utf8)
|
||||||
return mdParser.parse(md).metadata
|
return mdParser.parse(md).metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
func templateContext(url: URL, metadata: [String: String], body: String) -> TemplateContext {
|
|
||||||
let template = metadata["Template"]
|
|
||||||
let styles = metadata["Styles"]?.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
|
|
||||||
let scripts = metadata["Scripts"]?.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
|
|
||||||
|
|
||||||
#warning("FIXME: this is all fucked ... we need to handle index, archive, etc. differently, just make them built in pages and use a templates in templates/archive.html or something like that")
|
|
||||||
|
|
||||||
switch url.lastPathComponent {
|
|
||||||
/*
|
|
||||||
case "index.md":
|
|
||||||
#warning("TODO: fetch recentPosts")
|
|
||||||
let index = Index(template: template, styles: styles, scripts: scripts, recentPosts: [])
|
|
||||||
return .index(index)
|
|
||||||
|
|
||||||
case "archive.md":
|
|
||||||
let archive = Archive(title: "Archive", template: template, styles: styles, scripts: scripts)
|
|
||||||
return .archive(archive)
|
|
||||||
case "projects.md":
|
|
||||||
*/
|
|
||||||
|
|
||||||
default:
|
|
||||||
let title = metadata["Title"]!
|
|
||||||
let page = DefaultPage(title: title, template: template, styles: styles ?? [], scripts: scripts ?? [])
|
|
||||||
return TemplateContext(site: site, pageType: .page(page))
|
|
||||||
}
|
|
||||||
// case projects(Projects)
|
|
||||||
// case project(Project)
|
|
||||||
// let title = (metadata["Title"] as? String) ?? ""
|
|
||||||
// case post(Post)
|
|
||||||
// let title = (metadata["Title"] as? String) ?? ""
|
|
||||||
// case page(DefaultPage)
|
|
||||||
let title = (metadata["Title"] as? String) ?? ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
//
|
|
||||||
// Archive.swift
|
|
||||||
// SiteGenerator
|
|
||||||
//
|
|
||||||
// Created by Sami Samhuri on 2019-12-01.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct Archive: Page {
|
|
||||||
// Page properties
|
|
||||||
let title: String
|
|
||||||
let template: String?
|
|
||||||
let styles: [String]
|
|
||||||
let scripts: [String]
|
|
||||||
|
|
||||||
// Other properties
|
|
||||||
}
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
//
|
|
||||||
// DefaultPage.swift
|
|
||||||
// SiteGenerator
|
|
||||||
//
|
|
||||||
// Created by Sami Samhuri on 2019-12-01.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct DefaultPage: Page {
|
|
||||||
let title: String
|
|
||||||
let template: String?
|
|
||||||
let styles: [String]
|
|
||||||
let scripts: [String]
|
|
||||||
}
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
//
|
|
||||||
// Index.swift
|
|
||||||
// SiteGenerator
|
|
||||||
//
|
|
||||||
// Created by Sami Samhuri on 2019-12-01.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct Index: Page {
|
|
||||||
// Page properties
|
|
||||||
let template: String?
|
|
||||||
let styles: [String]
|
|
||||||
let scripts: [String]
|
|
||||||
|
|
||||||
var title: String {
|
|
||||||
assertionFailure("Don't use this. Use Site.title instead.")
|
|
||||||
return "easter egg"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Other properties
|
|
||||||
let recentPosts: [Post]
|
|
||||||
}
|
|
||||||
|
|
@ -7,9 +7,19 @@
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
protocol Page {
|
struct Page {
|
||||||
var title: String { get }
|
let title: String
|
||||||
var template: String? { get }
|
let template: String?
|
||||||
var styles: [String] { get }
|
let styles: [String]
|
||||||
var scripts: [String] { get }
|
let scripts: [String]
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Page {
|
||||||
|
init(metadata: [String: String]) {
|
||||||
|
let template = metadata["Template"]
|
||||||
|
let styles = metadata["Styles", default: ""].split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
|
||||||
|
let scripts = metadata["Scripts", default: ""].split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
|
||||||
|
let title = metadata["Title", default: ""]
|
||||||
|
self.init(title: title, template: template, styles: styles, scripts: scripts)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
//
|
|
||||||
// Post.swift
|
|
||||||
// SiteGenerator
|
|
||||||
//
|
|
||||||
// Created by Sami Samhuri on 2019-12-01.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct Post: Page {
|
|
||||||
// Page properties
|
|
||||||
let title: String
|
|
||||||
let template: String?
|
|
||||||
let styles: [String]
|
|
||||||
let scripts: [String]
|
|
||||||
|
|
||||||
// Other properties
|
|
||||||
let date: Date
|
|
||||||
let formattedDate: String
|
|
||||||
}
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
//
|
|
||||||
// Project.swift
|
|
||||||
// SiteGenerator
|
|
||||||
//
|
|
||||||
// Created by Sami Samhuri on 2019-12-01.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct Project: Page {
|
|
||||||
// Page properties
|
|
||||||
let title: String
|
|
||||||
let template: String?
|
|
||||||
let styles: [String]
|
|
||||||
let scripts: [String]
|
|
||||||
|
|
||||||
// Other properties
|
|
||||||
}
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
//
|
|
||||||
// Projects.swift
|
|
||||||
// SiteGenerator
|
|
||||||
//
|
|
||||||
// Created by Sami Samhuri on 2019-12-01.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct Projects: Page {
|
|
||||||
// Page properties
|
|
||||||
let title: String
|
|
||||||
let template: String?
|
|
||||||
let styles: [String]
|
|
||||||
let scripts: [String]
|
|
||||||
|
|
||||||
// Other properties
|
|
||||||
let projects: [Project]
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
//
|
|
||||||
// RenderedPage.swift
|
|
||||||
// SiteGenerator
|
|
||||||
//
|
|
||||||
// Created by Sami Samhuri on 2019-12-01.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
struct RenderedPage<SomePage: Page> {
|
|
||||||
let page: SomePage
|
|
||||||
let body: String
|
|
||||||
}
|
|
||||||
|
|
@ -7,39 +7,17 @@
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
enum PageType {
|
|
||||||
case page(DefaultPage)
|
|
||||||
|
|
||||||
case index(Index)
|
|
||||||
case archive(Archive)
|
|
||||||
// case monthPosts(MonthPosts)
|
|
||||||
// case yearPosts(YearPosts)
|
|
||||||
case post(Post)
|
|
||||||
|
|
||||||
case projects(Projects)
|
|
||||||
case project(Project)
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TemplateContext {
|
struct TemplateContext {
|
||||||
let site: Site
|
let site: Site
|
||||||
let pageType: PageType
|
let page: Page
|
||||||
|
let metadata: [String: String]
|
||||||
var page: Page {
|
let body: String
|
||||||
switch pageType {
|
|
||||||
case let .index(page as Page),
|
|
||||||
let .archive(page as Page),
|
|
||||||
let .projects(page as Page),
|
|
||||||
let .project(page as Page),
|
|
||||||
let .post(page as Page),
|
|
||||||
let .page(page as Page):
|
|
||||||
return page
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var title: String {
|
var title: String {
|
||||||
if case .index = pageType {
|
guard !page.title.isEmpty else {
|
||||||
return site.title
|
return site.title
|
||||||
}
|
}
|
||||||
|
|
||||||
return "\(site.title): \(page.title)"
|
return "\(site.title): \(page.title)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,38 +28,16 @@ struct TemplateContext {
|
||||||
|
|
||||||
// MARK: - Dictionary form
|
// MARK: - Dictionary form
|
||||||
|
|
||||||
extension PageType {
|
|
||||||
var dictionary: [String: Any] {
|
|
||||||
switch self {
|
|
||||||
case let .index(index):
|
|
||||||
return ["index": index]
|
|
||||||
|
|
||||||
case let .archive(archive):
|
|
||||||
return ["archive": archive]
|
|
||||||
|
|
||||||
case let .projects(projects):
|
|
||||||
return ["projects": projects]
|
|
||||||
|
|
||||||
case let .project(project):
|
|
||||||
return ["project": project]
|
|
||||||
|
|
||||||
case let .post(post):
|
|
||||||
return ["post": post]
|
|
||||||
|
|
||||||
case .page:
|
|
||||||
return [:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension TemplateContext {
|
extension TemplateContext {
|
||||||
var dictionary: [String: Any] {
|
var dictionary: [String: Any] {
|
||||||
[
|
[
|
||||||
"site": site,
|
"site": site,
|
||||||
"page": page,
|
"page": page,
|
||||||
|
"metadata": metadata,
|
||||||
"title": title,
|
"title": title,
|
||||||
|
"body": body,
|
||||||
"styles": site.styles + page.styles,
|
"styles": site.styles + page.styles,
|
||||||
"scripts": site.scripts + page.scripts,
|
"scripts": site.scripts + page.scripts,
|
||||||
].merging(pageType.dictionary, uniquingKeysWith: { current, _ in current })
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue