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
|
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/style.css">
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1">
<meta content="#ffffff" name="theme-color">
<meta name="HandheldFriendly" content="true">
<meta property="og:title" content="nerdypepper">
<meta property="og:type" content="website">
<meta property="og:description" content="a static site {for, by, about} me ">
<meta property="og:url" content="https://nerdypepper.tech">
<body>
<div class="posts">
<div class="post">
<div class="date">
23/11 2019
<span class="commit-hash">
<a href="https://github.com/nerdypepper/site/blob/master/posts/static_sites_with_bash.md
" style="text-decoration: none">
d1faa7e
</a>
</span>
</div>
<span class="post-title">
Static Sites With Bash
</span>
<div class="post-text">
<p>After going through a bunch of static site generators
(<a href="https://blog.getpelican.com/">pelican</a>,
<a href="https://gohugo.io">hugo</a>,
<a href="https://github.com/icyphox/vite">vite</a>), I decided to roll
my own. If you are more of the ‘show me the code’ kinda guy,
<a href="https://github.com/nerdypepper/site">here</a> you go.</p>
<p><strong>Text formatting</strong>: I chose to write in markdown, and convert
to html with <a href="https://kristaps.bsd.lv/lowdown/">lowdown</a>.</p>
<p><strong>Directory structure</strong>: I host my site on GitHub pages, so
<code>docs/</code> has to be the entry point. Markdown formatted posts
go into <code>posts/</code>, get converted into html, and end up in
<code>docs/index.html</code>, something like this:</p>
<pre><code>posts=$(ls -t ./posts) # chronological order!
for f in $posts; do
file="./posts/"$f # `ls` mangled our file paths
echo "generating post $file"
html=$(lowdown "$file")
echo -e "html" >> docs/index.html
done
</code></pre>
<p><strong>Assets</strong>: Most static site generators recommend dropping image
assets into the site source itself. That does have it’s
merits, but I prefer hosting images separately:</p>
<pre><code># strip file extension
ext="${1##*.}"
# generate a random file name
id=$( cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 2 | head -n 1 )
id="$id.$ext"
# copy to my file host
scp -P 443 "$1" emerald:files/"$id"
echo "https://files.nerdypepper.tech/$id"
</code></pre>
<p><strong>Templating</strong>:
<a href="https://github.com/NerdyPepper/site/blob/master/generate.sh"><code>generate.sh</code></a>
brings the above bits and pieces together (with some extra
cruft to avoid javascript). It uses <code>sed</code> to produce nice
titles from the file names (removes underscores,
title-case), and <code>date(1)</code> to add the date to each post
listing!</p>
</div>
<a href="/" class="post-end-link">⟵ Back</a>
<div class="separator"></div>
</div>
</div>
</body>
</html>
|