home · contact · privacy
Re-write date handling, remove dependency on filesystem lastmod dates.
[redo-blog] / processor / feed.xml.do
1 #!/bin/sh
2
3 # Pull in global dependencies.
4 . ./helpers.sh
5 metadata_dir=metadata
6 author_file="$metadata_dir"/author
7 meta_file="$metadata_dir"/automatic_metadata
8 title_file="$metadata_dir"/title
9 url_file="$metadata_dir"/url
10 redo-ifchange "$url_file"
11 redo-ifchange "$author_file"
12 redo-ifchange "$meta_file"
13 redo-ifchange "$title_file"
14
15 # Build some variables. XML-escape even file contents that should not contain
16 # dangerous characters, just to avoid any XML trouble.
17 srcdir=$(pwd)
18 tmp_snippets_dir=.tmp_feed_snippets
19 basepath=$(get_basepath "${metadata_dir}/")
20 title=$(read_and_escape_file "$title_file" | head -1)
21 author=$(read_and_escape_file "$author_file" | head -1)
22 uuid=$(get_uuid_from_meta_file "$meta_file")
23 feed_gen_date=$(get_creation_date_from_meta_file_seconds "$meta_file")
24
25 # Write majority of feed head.
26 cat << EOF
27 <?xml version="1.0" encoding="utf-8"?>
28 <feed xmlns="http://www.w3.org/2005/Atom">
29 EOF
30 printf "<link href=\"%s\" />\n" "$basepath"
31 printf "<link href=\"%sfeed.xml\" rel=\"self\" />\n" "$basepath"
32 printf "<title type=\"html\">%s</title>\n" "$title"
33 printf "<author><name>%s</name></author>\n" "$author"
34 printf "<id>urn:uuid:%s</id>\n" "$uuid"
35
36 # Generate feed entry snippets.
37 mkdir -p "$tmp_snippets_dir"
38 for file in ./*.rst ./*.md; do
39   if [ -e "$file" ]; then
40     meta_file="${metadata_dir}/${file%.*}.automatic_metadata"
41     redo-ifchange "$meta_file"
42     published=$(get_creation_date_from_meta_file_nanoseconds "$meta_file")
43     snippet_file=./${metadata_dir}/"${file%.*}.feed_snippet"
44     redo-ifchange "$snippet_file"
45     ln -s "$srcdir/$snippet_file" "./${tmp_snippets_dir}/${published}"
46   fi
47 done
48
49 # Derive feed modification date from snippets. Fallback to blog creation date.
50 n_snippet_files=`ls -1 ./${metadata_dir}/*.feed_snippet 2>/dev/null | wc -l`
51 if [ $n_snippet_files != 0 ]
52 then
53   mod_dates=$(grep -hE "^<updated>" ./${metadata_dir}/*.feed_snippet | sed -E 's/<.?updated>//g')
54 fi
55 last_mod_unix=$feed_gen_date
56 for date in $mod_dates; do
57   date_unix=$(date -u "+%s" -d "${date}")
58   if [ "$date_unix" -gt "$last_mod_unix" ]; then
59     last_mod_unix=$date_unix
60   fi
61 done
62 last_mod_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "@${last_mod_unix}"`
63 printf "<updated>%s</updated>\n\n" "$last_mod_rfc3339"
64
65 # Write feed entries.
66 for file in ./${tmp_snippets_dir}/*; do
67   if [ -e "$file" ]; then
68     cat "${file}"
69     printf "\n"
70   fi
71 done
72 rm -rf "$tmp_snippets_dir"
73
74 # Write feed foot.
75 printf "</feed>"