# Retrieve meta data of a given blog post. Generate new meta info if not yet exists.
atomfeed::meta () {
local -r gmi_file_path="$1"; shift
local -r meta_file=$($SED 's|gemtext|meta|; s|.gmi$|.meta|;' <<< "$gmi_file_path")
log VERBOSE "Generating meta info for post $gmi_file_path"
local -r meta_dir=$(dirname "$meta_file")
if [[ ! -d "$meta_dir" ]]; then
mkdir -p "$meta_dir"
fi
if [ ! -f "$meta_file" ]; then
# Extract first heading as post title.
local title=$($SED -n '/^# / { s/# //; p; q; }' "$gmi_file_path" | tr '"' "'")
# Extract first paragraph from Gemtext
local summary=$($SED -n '/^[A-Z]/ { p; q; }' "$gmi_file_path" | tr '"' "'")
# Extract the date from the file name.
local filename_date=$(basename "$gmi_file_path" | cut -d- -f1,2,3)
local date=$($DATE --iso-8601=seconds --date "$filename_date $($DATE +%H:%M:%S)")
cat < "${cache_file_path}.info"
}
# Retrieve the core content as XHTML of the blog post.
atomfeed::content () {
local -r gmi_file_path="$1"; shift
local -r cache_file_path="${gmi_file_path/gemtext/cache}.atomcache"
atomfeed::_from_cache "$gmi_file_path" "$cache_file_path" ||
atomfeed::_make_cache "$gmi_file_path" "$cache_file_path"
}
# Generate an atom.xml feed file.
atomfeed::generate () {
local -r gemfeed_dir="$CONTENT_BASE_DIR/gemtext/gemfeed"
local -r atom_file="$gemfeed_dir/atom.xml"
local -r now=$($DATE --iso-8601=seconds)
log INFO "Generating Atom feed to $atom_file"
log INFO 'This may takes a while with an empty cache....'
assert::not_empty now "$now"
cat < "$atom_file.tmp"
$now$DOMAIN feed$SUBTITLEgemini://$DOMAIN/
ATOMHEADER
while read -r gmi_file; do
# Load cached meta information about the post.
source <(atomfeed::meta "$gemfeed_dir/$gmi_file")
# Get HTML content for the feed
local content="$(atomfeed::content "$gemfeed_dir/$gmi_file")"
assert::not_empty meta_title "$meta_title"
assert::not_empty meta_date "$meta_date"
assert::not_empty meta_author "$meta_author"
assert::not_empty meta_email "$meta_email"
assert::not_empty meta_summary "$meta_summary"
assert::not_empty content "$content"
cat <> "$atom_file.tmp"
$meta_titlegemini://$DOMAIN/gemfeed/$gmi_file$meta_date$meta_author$meta_email$meta_summary
$content
ATOMENTRY
done < <(gemfeed::get_posts | head -n $ATOM_MAX_ENTRIES)
cat <> "$atom_file.tmp"
ATOMFOOTER
# Delete the 3rd line of the atom feeds (global feed update timestamp)
if ! diff -u <($SED 3d "$atom_file") <($SED 3d "$atom_file.tmp"); then
log INFO 'Feed got something new!'
mv "$atom_file.tmp" "$atom_file"
else
log INFO 'Nothing really new in the feed'
rm "$atom_file.tmp"
fi
}