Skip to content

Commit 8c74dd5

Browse files
committed
wip: wip
1 parent 4d4862d commit 8c74dd5

File tree

8 files changed

+264
-237
lines changed

8 files changed

+264
-237
lines changed

Sources/App/Middlewares/SiteMiddleware.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ struct SiteMiddleware<Context: RequestContext>: RouterController {
1919
LiveReloadMiddleware()
2020
#endif
2121

22+
NotFoundMiddleware()
23+
2224
if self.publicAssets.baseURL.isFileURL {
2325
FileMiddleware(
2426
self.publicAssets.baseURL.path(),
@@ -60,3 +62,21 @@ struct SiteMiddleware<Context: RequestContext>: RouterController {
6062
}
6163
}
6264
}
65+
66+
private struct NotFoundMiddleware<Context: RequestContext>: RouterMiddleware {
67+
func handle(
68+
_ input: Request,
69+
context: Context,
70+
next: (Request, Context) async throws -> Response
71+
) async throws -> Response {
72+
do {
73+
return try await next(input, context)
74+
} catch let error as HTTPError {
75+
guard error.status == .notFound else {
76+
throw error
77+
}
78+
return try NotFoundPage()
79+
.response(from: input, context: context)
80+
}
81+
}
82+
}

Sources/Pages/Components/BaseStylings.swift

Lines changed: 0 additions & 43 deletions
This file was deleted.

Sources/Pages/Components/Page.swift

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import ConcurrencyExtras
2+
import Dependencies
3+
import Elementary
4+
import Hummingbird
5+
6+
public protocol Page: Sendable, HTML, ResponseGenerator {
7+
associatedtype Head: HTML
8+
associatedtype Body: HTML
9+
10+
@HTMLBuilder var head: Head { get }
11+
@HTMLBuilder var body: Body { get }
12+
13+
var title: String { get }
14+
var lang: String { get }
15+
16+
var chunkSize: Int { get }
17+
var headers: HTTPFields { get }
18+
}
19+
20+
public extension Page {
21+
var lang: String { "en" }
22+
var chunkSize: Int { 1024 }
23+
var headers: HTTPFields { [.contentType: "text/html; charset=utf-8"] }
24+
25+
@HTMLBuilder var content: some HTML {
26+
withDependencies {
27+
$0.styleSheetGenerator = StyleSheetGenerator()
28+
} operation: {
29+
HTMLBuilder.builder {
30+
@Dependency(\.styleSheetGenerator) var generator
31+
let _ = generator.addElements {
32+
Elementary.body {
33+
self.body
34+
VueScript()
35+
}
36+
.inlineStyle("background-color", "#1c1c1c")
37+
.inlineStyle("color", "#fafafa")
38+
.inlineStyle("font-optical-sizing", "auto")
39+
.inlineStyle("font-size", "0.7em")
40+
.inlineStyle("font-size", "0.8em", media: .minWidth(390))
41+
.inlineStyle("font-size", "0.9em", media: .minWidth(480))
42+
}
43+
44+
HTMLRaw("<!DOCTYPE html>")
45+
html {
46+
Elementary.head {
47+
meta(.charset(.utf8))
48+
Elementary.title { self.title }
49+
meta(name: .viewport, content: "width=device-width, initial-scale=1.0")
50+
style { HTMLRaw("/*! modern-normalize v3.0.1 | MIT License | https://github.com/sindresorhus/modern-normalize */*,::after,::before{box-sizing:border-box}html{font-family:system-ui,'Segoe UI',Roboto,Helvetica,Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji';line-height:1.15;-webkit-text-size-adjust:100%;tab-size:4}body{margin:0}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Consolas,'Liberation Mono',Menlo,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}table{border-color:currentcolor}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;margin:0}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}legend{padding:0}progress{vertical-align:baseline}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}") }
51+
style {
52+
HTMLRaw(
53+
"""
54+
@font-face {
55+
font-family: "CommitMono";
56+
src: url("https://raw.githubusercontent.com/eigilnikolajsen/commit-mono/ecd81cdbd7f7eb2acaaa2f2f7e1a585676f9beff/src/fonts/fontlab/CommitMonoV143-VF.woff2");
57+
font-style: normal;
58+
font-weight: 400;
59+
font-display: swap;
60+
}
61+
html {
62+
line-height: 1.5;
63+
}
64+
pre a {
65+
text-decoration: none;
66+
}
67+
h1, h2, h3, h4, h5, figure, p, ol, ul, pre {
68+
margin: 0;
69+
}
70+
ol[role="list"], ul[role="list"] {
71+
list-style: none;
72+
padding-inline: 0;
73+
}
74+
img, video {
75+
display: block;
76+
max-inline-size: 100%;
77+
}
78+
code {
79+
font-family: "CommitMono", monospace;
80+
font-feature-settings", "ss03", "ss04", "ss05";
81+
line-height: 1;
82+
}
83+
"""
84+
)
85+
}
86+
/// Xcode Styling
87+
style { HTMLRaw(".xml .hljs-meta{color:#6C7986}.hljs-comment,.hljs-quote{color:#6C7986}.hljs-tag,.hljs-attribute,.hljs-keyword,.hljs-selector-tag,.hljs-literal,.hljs-name{color:#FC5FA3}.hljs-variable,.hljs-template-variable{color:#FC5FA3}.hljs-code,.hljs-string,.hljs-meta-string{color:#FC6A5D}.hljs-regexp,.hljs-link{color:#5482FF}.hljs-title,.hljs-symbol,.hljs-bullet,.hljs-number{color:#41A1C0}.hljs-section,.hljs-meta{color:#FC5FA3}.hljs-class .hljs-title,.hljs-type,.hljs-built_in,.hljs-builtin-name,.hljs-params{color:#D0A8FF}.hljs-attr{color:#BF8555}.hljs-subst{color:#FFF}.hljs-formula{font-style:italic}.hljs-selector-id,.hljs-selector-class{color:#9b703f}.hljs-doctag,.hljs-strong{font-weight:bold}.hljs-emphasis{font-style:italic}") }
88+
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"), .defer)
89+
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/swift.min.js"), .defer)
90+
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/rust.min.js"), .defer)
91+
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/javascript.min.js"), .defer)
92+
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/typescript.min.js"), .defer)
93+
script(.type(.module)) { HTMLRaw("hljs.highlightAll();") }
94+
style {
95+
HTMLRaw(generator.renderStyleSheet())
96+
}
97+
self.head
98+
}
99+
100+
HTMLRaw(generator.renderedElements())
101+
}
102+
.attributes(.lang(self.lang))
103+
}
104+
}
105+
}
106+
}
107+
108+
private extension HTMLBuilder {
109+
static func builder<R: HTML>(@HTMLBuilder operation: () -> R) -> R {
110+
operation()
111+
}
112+
}

Sources/Pages/HomePage.swift

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@ public struct HomePage: Page {
1010
public init() {}
1111

1212
public var head: some HTML {
13-
/// Xcode Styling
14-
style { HTMLRaw(".xml .hljs-meta{color:#6C7986}.hljs-comment,.hljs-quote{color:#6C7986}.hljs-tag,.hljs-attribute,.hljs-keyword,.hljs-selector-tag,.hljs-literal,.hljs-name{color:#FC5FA3}.hljs-variable,.hljs-template-variable{color:#FC5FA3}.hljs-code,.hljs-string,.hljs-meta-string{color:#FC6A5D}.hljs-regexp,.hljs-link{color:#5482FF}.hljs-title,.hljs-symbol,.hljs-bullet,.hljs-number{color:#41A1C0}.hljs-section,.hljs-meta{color:#FC5FA3}.hljs-class .hljs-title,.hljs-type,.hljs-built_in,.hljs-builtin-name,.hljs-params{color:#D0A8FF}.hljs-attr{color:#BF8555}.hljs-subst{color:#FFF}.hljs-formula{font-style:italic}.hljs-selector-id,.hljs-selector-class{color:#9b703f}.hljs-doctag,.hljs-strong{font-weight:bold}.hljs-emphasis{font-style:italic}") }
15-
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"), .defer)
16-
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/swift.min.js"), .defer)
17-
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/rust.min.js"), .defer)
18-
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/javascript.min.js"), .defer)
19-
script(.src("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/typescript.min.js"), .defer)
20-
script(.type(.module)) { HTMLRaw("hljs.highlightAll();") }
13+
EmptyHTML()
2114
}
2215

2316
public var body: some HTML {
@@ -33,12 +26,6 @@ public struct HomePage: Page {
3326
FooterView()
3427
}
3528
.inlineStyle("overflow-x", "hidden")
36-
.inlineStyle("background-color", "#1c1c1c")
37-
.inlineStyle("color", "#fafafa")
38-
.inlineStyle("font-optical-sizing", "auto")
39-
.inlineStyle("font-size", "0.7em")
40-
.inlineStyle("font-size", "0.8em", media: .minWidth(390))
41-
.inlineStyle("font-size", "0.9em", media: .minWidth(480))
4229
}
4330

4431
struct HeaderView: HTML {

Sources/Pages/NotFoundPage.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public struct NotFoundPage: Page {
1111
}
1212

1313
public var body: some HTML {
14-
EmptyHTML()
14+
div {
15+
16+
}
1517
}
1618
}

Sources/Pages/Page.swift

Lines changed: 0 additions & 117 deletions
This file was deleted.

0 commit comments

Comments
 (0)