blob: 1c22b584900b24cd3346d9e29384ddc758a25879 (
plain) (
blame)
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
|
#!/bin/bash
# duz: static site generator
# Copyright Adam House 2024
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
# Default values
base_url="https://www.example.com"
blog_dir_name="blog"
author_name="John Doe"
blog_title="Example"
index_title="Blog"
usage() {
echo "Usage: $0 [-h] [-u base_url] [-d blog_dir_name] [-a author_name] [-t blog_title] [-i index_title] [dir]"
echo " -h Show this help message and exit."
echo " -u Specify the base URL (default: https://www.example.com)."
echo " -d Specify the blog directory name (default: 'blog')."
echo " -a Specify the author's name (default: 'John Doe')."
echo " -t Specify the blog title (default: 'Example')."
echo " -i Specify the name of the blog according to the index page (default: 'Blog')."
echo " dir Specify the base directory to process (default: current directory)."
exit 1
}
# Parse options
while getopts ":hu:d:a:t:i:" opt; do
case ${opt} in
h )
usage
;;
u )
base_url=$OPTARG
;;
d )
blog_dir_name=$OPTARG
;;
a )
author_name=$OPTARG
;;
t )
blog_title=$OPTARG
;;
i )
index_title=$OPTARG
;;
\? )
echo "Invalid option: $OPTARG" 1>&2
usage
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
usage
;;
esac
done
shift $((OPTIND -1))
dir="${1:-.}"
src_dir="$dir/src"
if [ ! -d "$src_dir" ]; then
echo "no src directory"
exit 1
fi
dst_dir="$dir/dst"
rm -rf "$dst_dir"
mkdir "$dst_dir"
src_words_dir="$src_dir/$blog_dir_name"
dst_words_dir="$dst_dir/$blog_dir_name"
feed_path="$dst_words_dir/atom.xml"
index_page="$dst_words_dir/index.html"
if [[ -d "$src_words_dir" ]]; then
mkdir "$dst_words_dir"
# Start the feed
echo '<?xml version="1.0" encoding="utf-8"?>' > $feed_path
echo '<feed xmlns="http://www.w3.org/2005/Atom">' >> $feed_path
echo "<title>$blog_title</title>" >> $feed_path
echo "<link href=\"$base_url/$blog_dir_name/atom.xml\" rel=\"self\" type=\"application/atom+xml\"/>" >> $feed_path
echo "<link href=\"$base_url/$blog_dir_name/\"/>" >> $feed_path
echo "<updated>$(date +"%Y-%m-%dT%H:%M:%SZ")</updated>" >> $feed_path
echo "<author><name>$author_name</name></author>" >> $feed_path
echo '<generator>duz</generator>' >> $feed_path
echo "<id>$base_url/$blog_dir_name/</id>" >> $feed_path
# Start the index page
echo "<h1>$index_title <span id=\"feed\"><a href=\"/$blog_dir_name/atom.xml\"><i>(feed)</i></a></span></h1><ul>" >> $index_page
# Generate entries for the feed and index page
find "$src_words_dir" -name '*.dj' | while read file; do
# second line is the date; use this for sorting in reverse order
second_line=$(sed -n '2{p;q;}' "$file")
if ! [[ $second_line =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
echo "file in atom feed $file has malformed front matter"
exit 1
fi
echo "$second_line $file"
done | sort -r | cut -d' ' -f2- | while read file; do
title=$(sed -n '1p' "$file") # First line for title
date=$(sed -n '2p' "$file") # Second line for date
content=$(sed -n '/+++/,$p' "$file" | sed '1d' | dmos) # Everything after '+++'
html_filename=$(basename "$file" | sed 's/\.dj$/.html/')
echo '<entry>' >> $feed_path
echo "<title>${title}</title>" >> $feed_path
echo "<link href=\"$base_url/$blog_dir_name/$html_filename\"/>" >> $feed_path
echo "<id>$base_url/$blog_dir_name/$html_filename</id>" >> $feed_path
echo "<published>${date}T00:00:00Z</published>" >> $feed_path
echo "<updated>${date}T00:00:00Z</updated>" >> $feed_path
echo "<content type=\"html\">$(echo $content | sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/\"/\"/g;')</content>" >> $feed_path
echo '</entry>' >> $feed_path
echo "<li><p><a href=\"/$blog_dir_name/$html_filename\"><strong>${date}</strong></a> – ${title}</p></li>" >> $index_page
done
echo '</feed>' >> $feed_path
echo '</ul>' >> $index_page
fi
# Copy src to dst and process files, excluding _header.html and _footer.html
find "$dir/src" -type f | while read -r file_path; do
file="${file_path#$dir/src/}"
if [[ "$file" == "_header.html" ]] || [[ "$file" == "_footer.html" ]]; then
continue
fi
mkdir -p "$dst_dir/$(dirname "$file")"
if [[ "$file" == *.dj ]]; then
# Process .dj files with dmos and handle errors
if grep -q '+++' "$file_path"; then
# Delimiter exists, process accordingly
sed -n '/+++/,$p' "$file_path" | sed '1d' |
dmos --toc > "$dst_dir/${file%.dj}.html"
elif [[ "$file" == index.dj ]]; then
# No delimiter found, and we are on top index, so no TOC
dmos "$file_path" > "$dst_dir/${file%.dj}.html"
else
# No delimiter found, process the whole file
if ! dmos --toc "$file_path" > "$dst_dir/${file%.dj}.html"; then
echo "Error processing $file with dmos"
exit 1
fi
fi
else
# Copy file over
cp "$file_path" "$dst_dir/$file"
fi
done
# prepend header and append footer to final files
for html_file in $(find "$dst_dir" -type f -name "*.html"); do
[ -f "$dir/src/_header.html" ] && cat "$dir/src/_header.html" "$html_file" > "$html_file.tmp" && mv "$html_file.tmp" "$html_file"
[ -f "$dir/src/_footer.html" ] && cat "$dir/src/_footer.html" >> "$html_file"
done
exit 0
|