-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.ts
235 lines (208 loc) · 6.8 KB
/
templates.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// deno-lint-ignore-file no-explicit-any
import { Post } from "./build.ts";
const site_url = "https://pastor.github.io";
export const base = (
{ content, src, title, path, description, extra_css }: {
content: HtmlString;
src: string;
title: string;
description: string;
path: string;
extra_css?: string;
},
): HtmlString =>
html`
<!DOCTYPE html>
<html lang='en-US'>
<head>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${title}</title>
<meta name="description" content="${description}">
<link rel="icon" href="/favicon.png" type="image/png">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="canonical" href="${site_url}${path}">
<link rel="alternate" type="application/rss+xml" title="pastor" href="${site_url}/feed.xml">
<style>
@font-face {
font-family: 'Open Sans'; src: url('/css/OpenSans-300-Normal.woff2') format('woff2');
font-weight: 300; font-style: normal;
}
@font-face {
font-family: 'JetBrains Mono'; src: url('/css/JetBrainsMono-400-Normal.woff2') format('woff2');
font-weight: 400; font-style: normal;
}
@font-face {
font-family: 'JetBrains Mono'; src: url('/css/JetBrainsMono-700-Normal.woff2') format('woff2');
font-weight: 700; font-style: normal;
}
@font-face {
font-family: 'EB Garamond'; src: url('/css/EBGaramond-400-Normal.woff2') format('woff2');
font-weight: 400; font-style: normal;
}
@font-face {
font-family: 'EB Garamond'; src: url('/css/EBGaramond-400-Italic.woff2') format('woff2');
font-weight: 400; font-style: italic;
}
@font-face {
font-family: 'EB Garamond'; src: url('/css/EBGaramond-700-Normal.woff2') format('woff2');
font-weight: 700; font-style: normal;
}
@font-face {
font-family: 'EB Garamond'; src: url('/css/EBGaramond-700-Italic.woff2') format('woff2');
font-weight: 700; font-style: italic;
}
* { box-sizing: border-box; margin: 0; padding: 0; margin-block-start: 0; margin-block-end: 0; }
body {
max-width: 80ch;
padding: 2ch;
margin-left: auto;
margin-right: auto;
}
header { margin-bottom: 2rem; }
header > nav { display: flex; column-gap: 2ch; align-items: baseline; flex-wrap: wrap; }
header a { font-style: normal; color: rgba(0, 0, 0, .8); text-decoration: none; }
header a:hover { color: rgba(0, 0, 0, .8); text-decoration: underline; }
header .title { font-size: 1.25em; flex-grow: 2; }
footer { margin-top: 2rem; }
footer > p { display: flex; column-gap: 2ch; justify-content: center; flex-wrap: wrap; }
footer a { color: rgba(0, 0, 0, .8); text-decoration: none; white-space: nowrap; }
footer i { vertical-align: middle; color: rgba(0, 0, 0, .8) }
</style>
<link rel="stylesheet" href="/css/main.css">
${extra_css ? html`<link rel="stylesheet" href="/css/${extra_css}">` : ""}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
</head>
<body>
<header>
<nav>
<a class="title" href="/">Pastor</a>
<a href="/about.html">Обо мне</a>
<a href="/links.html">Ссылки</a>
</nav>
</header>
<main>
${content}
</main>
<footer class="site-footer">
<p>
<a href="https://github.com/pastor/pastor.github.io/edit/master${src}">
<i class="fa fa-edit"></i> Исправить
</a>
<a href="/feed.xml">
<i class="fa fa-rss"></i> RSS
</a>
<a href="https://github.com/pastor">
<i class="fa fa-github"></i> Pastor
</a>
</p>
</footer>
</body>
</html>
`;
const blurb = "Yet another programming blog by Andrey Khlebnikov aka Pastor.";
export function page(name: string, content: HtmlString) {
return base({
path: `/${name}`,
title: "Pastor",
description: blurb,
src: `/src/${name}.dj`,
extra_css: name === "resume" ? "resume.css" : undefined,
content,
});
}
export const post_list = (posts: Post[]): HtmlString => {
const list_items = posts.map((post) =>
html`
<li>
<h2>${time(post.date)} <a href="${post.path}">${post.title}</a></h2>
</li>`
);
return base({
path: "",
title: "Pastor",
description: blurb,
src: "/templates.ts",
content: html`<ul class="post-list">${list_items}</ul>`,
});
};
export function post(post: Post, spellcheck: boolean): HtmlString {
return base({
src: post.src,
title: post.title,
description: post.summary,
path: post.path,
content: html`<article ${
spellcheck ? 'contentEditable="true"' : ""
}>\n${post.content}</article>`,
});
}
export function time(date: Date): HtmlString {
const human = date.toLocaleDateString("en-US", {
month: "short",
day: "numeric",
year: "numeric",
timeZone: "UTC",
});
const machine = yyyy_mm_dd(date);
return html`<time datetime="${machine}">${human}</time>`;
}
function yyyy_mm_dd(date: Date): string {
return date.toISOString().slice(0, 10);
}
export const feed = (posts: Post[]): HtmlString => {
const entries = posts.slice(0, 10).map(feed_entry);
return html`<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<link href="${site_url}/feed.xml" rel="self" type="application/atom+xml"/>
<link href="${site_url}" rel="alternate" type="text/html"/>
<updated>${new Date().toISOString()}</updated>
<id>${site_url}/feed.xml</id>
<title type="html">Pastor</title>
<subtitle>Yet another programming blog by Andrey Khlebnikov aka Pastor.</subtitle>
<author><name>Andrey Khlebnikov</name></author>
${entries}
</feed>
`;
};
export const feed_entry = (post: Post): HtmlString => {
return html`
<entry>
<title type="text">${post.title}</title>
<link href="${site_url}${post.path}" rel="alternate" type="text/html" title="${post.title}" />
<published>${yyyy_mm_dd(post.date)}T00:00:00+00:00</published>
<updated>${yyyy_mm_dd(post.date)}T00:00:00+00:00</updated>
<id>${site_url}${post.path.replace(".html", "")}</id>
<author><name>Andrey Khlebnikov</name></author>
<summary type="html"><![CDATA[${post.summary}]]></summary>
<content type="html" xml:base="${site_url}${post.path}"><![CDATA[${post.content}]]></content>
</entry>
`;
};
export function html(
strings: ArrayLike<string>,
...values: any[]
): HtmlString {
function content(value: any): string[] {
if (value === undefined) return [];
if (value instanceof HtmlString) return [value.value];
if (Array.isArray(value)) return value.flatMap(content);
return [escapeHtml(value)];
}
return new HtmlString(
String.raw({ raw: strings }, ...values.map((it) => content(it).join(""))),
);
}
export class HtmlString {
constructor(public value: string) {
}
push(other: HtmlString) {
this.value = `${this.value}\n${other.value}`;
}
}
function escapeHtml(data: any): string {
return `${data}`
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
}