-
Notifications
You must be signed in to change notification settings - Fork 0
/
barf_macos
executable file
·155 lines (132 loc) · 3.96 KB
/
barf_macos
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
#!/bin/sh
set -eu
MARKDOWN=smu
IFS=' '
# Create tab separated file with filename, title, creation date, last update
index_tsv() {
for f in "$1"/*.md
do
title=$(gsed -n '/^# /{s/# //p; q}' "$f")
printf '%s\t%s\t%s\t%s\n' "$f" "${title:="No Title"}"
done
}
index_html() {
# Print header
title=$(gsed -n '/^# /{s/# //p; q}' index.md)
gsed "s/{{TITLE}}/$title/" header.html
# Intro text
$MARKDOWN index.md
# Posts
while read -r f title created; do
link=$(echo "$f" | gsed -E 's|.*/(.*).md|\1/|')
created=$(echo $(head -3 "$f" | tail -1))
echo \
"<span class='created'>" \
"<a style=\"font-size: x-large;\" href=\"$link\"> $title</a>" \
"<a style=\"font-size: small; align-self: center;\">$created</a>" \
"</span>"
# echo "<li><a>$created</a> <a href=\"$link\">$title</a></li>"
done < "$1" | sort -r
# Print footer after post list
cat footer.html
}
til_index_html() {
# Print header
title=$(gsed -n '/^# /{s/# //p; q}' til.md)
sed "s/{{TITLE}}/$title/" header.html
# Intro text
$MARKDOWN til.md
# Posts
while read -r f title created; do
link=$(echo "$f" | sed -E 's|.*/(.*).md|til/\1/|')
created=$(echo $(head -3 "$f" | tail -1))
echo \
"<span class='created'>" \
"<a style=\"font-size: x-large;\" href=\"$link\"> $title</a>" \
"<a style=\"font-size: small; align-self: center;\">$created</a>" \
"</span>"
done < "$1" | sort -r
# Print footer after post list
cat footer.html
}
atom_xml() {
uri=$(gsed -rn '/atom.xml/ s/.*href="([^"]*)".*/\1/ p' header.html)
domain=$(echo "$uri" | gsed 's/atom.xml//g' | gsed 's|/[^/]*$||')
cat <<EOF
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>$(gsed -n '/^# /{s/# //p; q}' index.md)</title>
<link href="$domain/atom.xml" rel="self" />
<updated>$(date +%FT%TZ)</updated>
<author>
<name>$(git config user.name)</name>
</author>
<id>$domain,$(date +%F):default-atom-feed/</id>
EOF
while read -r f title created; do
content=$($MARKDOWN "$f" | gsed 's/&/\&/g; s/</\</g; s/>/\>/g;
s/"/\"/g; s/'"'"'/\'/g')
post_link=$(echo "$f" | gsed -E 's|posts/(.*).md|\1|')
basic_date=$(echo $(head -3 "$f" | tail -1))
published_date=$(gdate -d $basic_date -u +%Y-%m-%dT10:%M:%SZ)
cat <<EOF
<entry>
<title>$title</title>
<content type="html">$content</content>
<link href="$domain/$post_link"/>
<id>$domain/$post_link</id>
<updated>$published_date</updated>
<published>$published_date</published>
</entry>
EOF
done < "$1"
echo '</feed>'
}
write_page() {
filename=$1
directory=$(echo $(basename "$filename" .md))
$(mkdir -p build/$directory)
target=$(echo "$filename" | gsed -r 's|\w+/(.*).md|build/\1/index.html|')
created=$(echo $(head -3 "$filename" | tail -1))
title=$2
$MARKDOWN "$filename" | \
cat header.html - |\
gsed "s|{{TITLE}}|$title|" \
> "$target" && cat footer.html >> "$target"
}
write_til_page() {
filename=$1
directory=$(echo $(basename "$filename" .md))
$(mkdir -p build/til/$directory)
target=$(echo "$filename" | gsed -r 's|\w+/(.*).md|build/til/\1/index.html|')
created=$(echo $(head -3 "$filename" | tail -1))
title=$2
$MARKDOWN "$filename" | \
cat header.html - |\
sed "s|{{TITLE}}|$title|" \
> "$target" && cat footer.html >> "$target"
}
rm -rf build && mkdir build
# Blog posts
index_tsv posts | sort -rt " " -k 3 > build/posts.tsv
index_html build/posts.tsv > build/index.html
atom_xml build/posts.tsv > build/atom.xml
while read -r f title created; do
write_page "$f" "$title" "$created"
done < build/posts.tsv
# TIL posts
index_tsv tils | sort -rt " " -k 3 > build/tils.tsv
til_index_html build/tils.tsv > build/til.html
while read -r f title created; do
write_til_page "$f" "$title" "$created"
done < build/tils.tsv
# Pages
index_tsv pages > build/pages.tsv
while read -r f title created; do
write_page "$f" "$title" "$created"
done < build/pages.tsv
# TILs
index_tsv tils > build/tils.tsv
while read -r f title created; do
write_page "$f" "$title" "$created"
done < build/tils.tsv