-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_site.sh
executable file
·180 lines (152 loc) · 5.73 KB
/
generate_site.sh
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
#!/usr/bin/env bash
set -e
BASE_DIR="$(dirname "$(realpath "$0")")"
TEMPLATE_FILE="$BASE_DIR/template.html"
OUTPUT_BASE_DIR="$BASE_DIR/notes"
MARKDOWN_DIR="$OUTPUT_BASE_DIR/markdown"
TMP_CONTENT="$(mktemp)"
INDEX_HTML="$BASE_DIR/index.html"
NOTES_HTML="$BASE_DIR/notes.html"
# ----------------------------------------------------------------------
# Simple URL-encoding helper
# ----------------------------------------------------------------------
urlencode() {
local string="$1"
local length="${#string}"
local encoded=""
for (( i = 0; i < length; i++ )); do
local c="${string:i:1}"
case "$c" in
[a-zA-Z0-9.~_-]) encoded+="$c" ;;
*) printf -v hex '%%%02X' "'$c"
encoded+="$hex" ;;
esac
done
echo "$encoded"
}
# ----------------------------------------------------------------------
# Helper to insert a new <tr> for an article into both index.html and notes.html
# using awk rather than sed to avoid escaping issues.
# ----------------------------------------------------------------------
add_article_to_index_and_notes() {
local date="$1"
local title="$2"
local category="$3"
local filename_no_ext="$4"
# For the link inside HTML:
local article_link="notes/$category/$filename_no_ext.html"
# -------------------------------
# 1. Insert into index.html
# -------------------------------
if grep -q "$article_link" "$INDEX_HTML"; then
echo "Article already found in index.html. Skipping insertion."
else
# Build the row for index.html
# Notice we use cat <<EOF ... EOF to create a multi-line string
local index_row
index_row="$(cat <<EOF
<tr>
<td>
<time datetime="$date">$date</time>
</td>
<td>
<a href="$article_link" title="$title">$title</a>
</td>
<td class="hide-phone">
<a class="btn btn-sm bg-dark-green white hover-white hover-bg-black measure-6"
href="$article_link"
title="Read Article - $title">Read
→</a><br />
</td>
</tr>
EOF
)"
# Use awk to inject above </tbody>:
awk -v row="$index_row" '
/<\/tbody>/ {
print row
}
{ print }
' "$INDEX_HTML" > "$INDEX_HTML.tmp"
mv "$INDEX_HTML.tmp" "$INDEX_HTML"
echo "Appended new article to index.html."
fi
# -------------------------------
# 2. Insert into notes.html
# -------------------------------
if grep -q "$article_link" "$NOTES_HTML"; then
echo "Article already found in notes.html. Skipping insertion."
else
# Build the row for notes.html
local notes_row
notes_row="$(cat <<EOF
<tr>
<td>
<time datetime="$date">$date</time>
</td>
<td class="pv1 pr4 dtc">
<a href="$article_link" title="$title">$title
</a>
</td>
</tr>
EOF
)"
awk -v row="$notes_row" '
/<\/tbody>/ {
print row
}
{ print }
' "$NOTES_HTML" > "$NOTES_HTML.tmp"
mv "$NOTES_HTML.tmp" "$NOTES_HTML"
echo "Appended new article to notes.html."
fi
}
# ----------------------------------------------------------------------
# Check for Pandoc
# ----------------------------------------------------------------------
if ! command -v pandoc &> /dev/null; then
echo "Error: pandoc is not installed. Please install pandoc to proceed."
exit 1
fi
# ----------------------------------------------------------------------
# Main loop: discover all .md, parse metadata, generate HTML if needed
# ----------------------------------------------------------------------
find "$MARKDOWN_DIR" -name '*.md' | while read -r mdfile; do
TITLE=$(grep '^Title:' "$mdfile" | sed 's/^Title:[ \t]*//')
CATEGORY=$(grep '^Category:' "$mdfile" | sed 's/^Category:[ \t]*//')
DATE=$(grep '^Date:' "$mdfile" | sed 's/^Date:[ \t]*//')
if [[ -z "$TITLE" || -z "$CATEGORY" || -z "$DATE" ]]; then
echo "Skipping '$mdfile' due to missing metadata (Title, Category, or Date)."
continue
fi
FILENAME=$(basename "$mdfile" .md)
CATEGORY_DIR="$OUTPUT_BASE_DIR/$CATEGORY"
mkdir -p "$CATEGORY_DIR"
OUTPUT_FILE="$CATEGORY_DIR/$FILENAME.html"
# If the file already exists, skip generation
if [[ -f "$OUTPUT_FILE" ]]; then
echo "Skipping '$OUTPUT_FILE' as it already exists."
continue
fi
# Extract everything after the first blank line as the content
CONTENT=$(sed '1,/^$/d' "$mdfile")
# Convert markdown to HTML via pandoc
echo "$CONTENT" | pandoc -f markdown -t html -o "$TMP_CONTENT"
ARTICLE_CONTENT=$(cat "$TMP_CONTENT")
# Build OG url
CATEGORY_ENCODED=$(urlencode "$CATEGORY")
FILENAME_ENCODED=$(urlencode "$FILENAME.html")
OG_URL="https://iljo.dev/notes/$CATEGORY_ENCODED/$FILENAME_ENCODED"
export TITLE DATE CONTENT ARTICLE_CONTENT CATEGORY_ENCODED FILENAME_ENCODED OG_URL
# Insert your article content into the template
OUTPUT_CONTENT=$(envsubst < "$TEMPLATE_FILE")
echo "$OUTPUT_CONTENT" > "$OUTPUT_FILE"
echo "Generated '$OUTPUT_FILE'."
# Now that the article is generated, we append a row in index.html & notes.html
add_article_to_index_and_notes "$DATE" "$TITLE" "$CATEGORY" "$FILENAME"
done
# ----------------------------------------------------------------------
# Clean up
# ----------------------------------------------------------------------
rm -f "$TMP_CONTENT"
echo "Article generation completed successfully."