home · contact · privacy
Unhide metadata directory, move helpers script up in tree.
[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 uuid_file="$metadata_dir"/uuid
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 "$uuid_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 basepath=$(get_basepath "${metadata_dir}/")
19 title=`read_and_escape_file "$title_file" | head -1`
20 author=`read_and_escape_file "$author_file" | head -1`
21 uuid=`read_and_escape_file "$uuid_file" | head -1`
22 tmp_snippets_dir=.tmp_feed_snippets
23
24 # Write majority of feed head.
25 cat << EOF
26 <?xml version="1.0" encoding="utf-8"?>
27 <feed xmlns="http://www.w3.org/2005/Atom">
28 EOF
29 printf "<link href=\"%s\" />\n" "$basepath"
30 printf "<link href=\"%sfeed.xml\" rel=\"self\" />\n" "$basepath"
31 printf "<title type=\"html\">%s</title>\n" "$title"
32 printf "<author><name>%s</name></author>\n" "$author"
33 printf "<id>urn:uuid:%s</id>\n" "$uuid"
34
35 # Generate feed entry snippets.
36 mkdir -p "$tmp_snippets_dir"
37 for file in ./*.rst ./*.md; do
38   if [ -e "$file" ]; then
39     uuid_file="${metadata_dir}/${file%.*}.uuid"
40     redo-ifchange "$uuid_file"
41     published=`stat -c%y "${uuid_file}"`
42     published_unix=$(date -u "+%s%N" -d "${published}")
43     snippet_file=./${metadata_dir}/"${file%.*}.feed_snippet"
44     redo-ifchange "$snippet_file"
45     ln -s "$srcdir/$snippet_file" "./${tmp_snippets_dir}/${published_unix}"
46   fi
47 done
48
49 # Derive feed modification date from snippets.
50 mod_dates=$(grep -hE "^<updated>" ./${metadata_dir}/*.feed_snippet | sed -E 's/<.?updated>//g')
51 last_mod_unix=0
52 for date in $mod_dates; do
53   date_unix=$(date -u "+%s" -d "${date}")
54   if [ "$date_unix" -gt "$last_mod_unix" ]; then
55     last_mod_unix=$date_unix
56   fi
57 done
58 lastmod_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "@${last_mod_unix}"`
59 printf "<updated>%s</updated>\n\n" "$lastmod_rfc3339"
60
61 # Write feed entries.
62 for file in ./${tmp_snippets_dir}/*; do
63   cat "${file}"
64   printf "\n"
65 done
66 rm -rf "$tmp_snippets_dir"
67
68 # Write feed foot.
69 printf "</feed>"