blob: 14642afeafaa17963c8e8a1d1050874d3cf43f5f (
plain)
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
|
# Generate a HTML or Markdown link from given Gemtext link.
generate::make_link () {
local -r what="$1"; shift
local -r line="${1/=> }"; shift
local link
local descr
while read -r token; do
if [ -z "$link" ]; then
link="$token"
elif [ -z "$descr" ]; then
descr="$token"
else
descr="$descr $token"
fi
done < <(echo "$line" | tr ' ' '\n')
if $GREP -E -q "$IMAGE_PATTERN" <<< "$link"; then
if [[ "$what" == md ]]; then
md::make_img "$link" "$descr"
else
html::make_img "$link" "$(html::encode "$descr")"
fi
return
fi
if [[ "$what" == md ]]; then
md::make_link "$link" "$descr"
else
html::make_link "$link" "$(html::encode "$descr")"
fi
}
# Add other docs (e.g. images, videos) from Gemtext to output format.
generate::fromgmi_add_docs () {
local -r src="$1"; shift
local -r format="$1"; shift
local -r dest=${src/gemtext/$format}
local -r dest_dir=$(dirname "$dest")
if [[ ! -d "$dest_dir" ]]; then
mkdir -p "$dest_dir"
fi
cp "$src" "$dest"
git::add "$format" "$dest"
}
# Remove docs from output format which aren't present in Gemtext anymore.
generate::fromgmi_cleanup_docs () {
local -r src="$1"; shift
local -r format="$1"; shift
local dest=${src/.$format/.gmi}
dest=${dest/$format/gemtext}
if [[ ! -f "$dest" ]]; then
git::rm "$format" "$src"
fi
}
# Convert the Gemtext Atom feed to a HTML Atom feed.
generate::convert_gmi_atom_to_html_atom () {
local -r format="$1"; shift
if [[ "$format" != html ]]; then
return
fi
log INFO 'Converting Gemtext Atom feed to HTML Atom feed'
$SED 's|.gmi|.html|g; s|gemini://|https://|g' \
< $CONTENT_BASE_DIR/gemtext/gemfeed/atom.xml \
> $CONTENT_BASE_DIR/html/gemfeed/atom.xml
git::add "$format" "$CONTENT_BASE_DIR/html/gemfeed/atom.xml"
}
# Internal helper function for generate::fromgmi
generate::_fromgmi () {
local -r src="$1"; shift
local -r format="$1"; shift
local dest=${src/gemtext/$format}
dest=${dest/.gmi/.$format}
local dest_dir=$(dirname "$dest")
if [[ ! -d "$dest_dir" ]]; then
mkdir -p "$dest_dir"
fi
if [[ "$format" == html ]]; then
cat "$HTML_HEADER" > "$dest.tmp"
html::fromgmi < "$src" >> "$dest.tmp"
cat "$HTML_FOOTER" >> "$dest.tmp"
elif [[ "$format" == md ]]; then
md::fromgmi < "$src" >> "$dest.tmp"
fi
local title=$($SED -n '/^# / { s/# //; p; q; }' "$src" | tr '"' "'")
if [[ -z "$title" ]]; then
title=$SUBTITLE
fi
if [[ "$format" == html ]]; then
local stylesheet="$(basename "$HTML_CSS_STYLE")"
local stylesheet_override="${stylesheet/.css/-override.css}"
if [[ "$CONTENT_BASE_DIR/html" != "$(dirname "$dest")" ]]; then
stylesheet="../$stylesheet"
fi
$SED -i "s|%%TITLE%%|$title|g;
s|%%DOMAIN%%|$DOMAIN|g;
s|%%STYLESHEET%%|$stylesheet|g;
s|%%STYLESHEET_OVERRIDE%%|$stylesheet_override|g;" "$dest.tmp"
mv "$dest.tmp" "$dest"
fi
git::add "$format" "$dest"
}
# Generate a given output format from a Gemtext file.
generate::fromgmi () {
local -i num_gmi_files=0
local -i num_doc_files=0
log INFO "Generating $* from Gemtext"
while read -r src; do
num_gmi_files=$(( num_gmi_files + 1 ))
for format in "$@"; do
generate::_fromgmi "$src" "$format" &
done
done < <(find "$CONTENT_BASE_DIR/gemtext" -type f -name \*.gmi)
wait
log INFO "Converted $num_gmi_files Gemtext files"
# Add HTML extras (will be cleaned up further below)
cp $HTML_CSS_STYLE $CONTENT_BASE_DIR/gemtext/style.css
for section in . gemfeed notes; do
if [[ ! -d "$CONTENT_BASE_DIR/gemtext/$section" ]]; then
continue
fi
local override_source="./htmlextras/style-${section}-override.css"
local override_dest="$CONTENT_BASE_DIR/gemtext/$section/style-override.css"
if [ ! -f "$override_source" ]; then
touch "$override_dest" # Empty override
continue
fi
cp "$override_source" "$override_dest"
done
cp "$HTML_WEBFONT_TEXT" $CONTENT_BASE_DIR/gemtext/text.ttf
cp "$HTML_WEBFONT_CODE" $CONTENT_BASE_DIR/gemtext/code.ttf
cp "$HTML_WEBFONT_HANDNOTES" $CONTENT_BASE_DIR/gemtext/handnotes.ttf
cp "$HTML_WEBFONT_TYPEWRITER" $CONTENT_BASE_DIR/gemtext/typewriter.ttf
# Add non-.gmi files to html dir.
log VERBOSE "Adding other docs to $*"
while read -r src; do
num_doc_files=$(( num_doc_files + 1 ))
for format in "$@"; do
generate::fromgmi_add_docs "$src" "$format" &
done
done < <(find "$CONTENT_BASE_DIR/gemtext" -type f | $GREP -E -v '(\.git.*|\.gmi|atom.xml|\.tmp|htmlextras)$')
wait
log INFO "Added $num_doc_files other documents to each of $*"
# Add atom feed for HTML
generate::convert_gmi_atom_to_html_atom 'html' &
# Remove obsolete files from ./html/.
# Note: The _config.yml is the config file for GitHub pages (md format).
# Anoter note: The CNAME file is required by GitHub pages as well for custom domains.
for format in "$@"; do
find "$CONTENT_BASE_DIR/$format" -type f |
$GREP -E -v '(\.git.*|_config.yml|CNAME|.domains|robots.txt|static)$'|
while read -r src; do
generate::fromgmi_cleanup_docs "$src" "$format"
done &
done
wait
# Cleanup HTML extras
rm $CONTENT_BASE_DIR/gemtext/{style.css,*.ttf}
if [[ -z "$GIT_COMMIT_MESSAGE" ]]; then
GIT_COMMIT_MESSAGE='Publishing new version'
fi
git::commit gemtext "$GIT_COMMIT_MESSAGE"
git::commit meta "$GIT_COMMIT_MESSAGE"
for format in "$@"; do
git::commit "$format" "$GIT_COMMIT_MESSAGE"
log INFO "$format can be found in $CONTENT_BASE_DIR/$format now"
done
}
|