home · contact · privacy
Move snippet generation for index/feed into separate .do files.
[redo-blog] / processor / feed.xml.do
1 #!/bin/sh
2
3 # Pull in global dependencies.
4 . ./helpers.sh
5 author_file=author.meta
6 uuid_file=uuid.meta
7 title_file=title.meta
8 redo-ifchange "$url_file"
9 redo-ifchange "$author_file"
10 redo-ifchange "$uuid_file"
11 redo-ifchange "$title_file"
12
13 # Build some variables. XML-escape even file contents that should not contain
14 # dangerous characters, just to avoid any XML trouble.
15 srcdir=`pwd`
16 basepath=$(get_basepath)
17 title=`read_and_escape_file "$title_file" | head -1`
18 author=`read_and_escape_file "$author_file" | head -1`
19 uuid=`read_and_escape_file "$uuid_file" | head -1`
20
21 # Write majority of feed head.
22 cat << EOF
23 <?xml version="1.0" encoding="utf-8"?>
24 <feed xmlns="http://www.w3.org/2005/Atom">
25 EOF
26 printf "<link href=\"%s\" />\n" "$basepath"
27 printf "<link href=\"%sfeed.xml\" rel=\"self\" />\n" "$basepath"
28 printf "<title type=\"html\">%s</title>\n" "$title"
29 printf "<author><name>%s</name></author>\n" "$author"
30 printf "<id>urn:uuid:%s</id>\n" "$uuid"
31
32 # Generate feed entry snippets.
33 mkdir -p feed_snippets
34 for file in ./*.rst ./*.md; do
35   if [ -e "$file" ]; then
36     uuid_file="${file%.*}.uuid"
37     redo-ifchange "$uuid_file"
38     published=`stat -c%y "${uuid_file}"`
39     published_unix=$(date -u "+%s%N" -d "${published}")
40     snippet_file="${file%.*}.feed_snippet"
41     redo-ifchange "$snippet_file"
42     ln -s "$srcdir/$snippet_file" "./feed_snippets/${published_unix}"
43   fi
44 done
45
46 # Derive feed modification date from snippets.
47 mod_dates=$(grep -hE "^<updated>" ./feed_snippets/* | sed -E 's/<.?updated>//g')
48 last_mod_unix=0
49 for date in $mod_dates; do
50   date_unix=$(date -u "+%s" -d "${date}")
51   if [ "$date_unix" -gt "$last_mod_unix" ]; then
52     last_mod_unix=$date_unix
53   fi
54 done
55 lastmod_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "@${last_mod_unix}"`
56 printf "<updated>%s</updated>\n\n" "$lastmod_rfc3339"
57
58 # Write feed entries.
59 for file in ./feed_snippets/*; do
60   cat "${file}"
61   printf "\n"
62 done
63 rm -rf feed_snippets
64
65 # Write feed foot.
66 printf "</feed>"