home · contact · privacy
Fix index.html generation as well.
[redo-blog] / processor / feed.xml.do
1 #!/bin/sh
2
3 build_entry () {
4   file="${1}"
5   uuidfile="${2}"
6   published="${3}"
7   intermediate_file="${file%.*}.intermediate"
8   htmlfile=`escape_url "${file%.*}.html"`
9   redo-ifchange "$uuid_file"
10   redo-ifchange "$intermediate_file"
11   lastmod=`stat -c%y "$file"`
12   lastmod_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "$lastmod"`
13   title=`read_and_escape_file "$intermediate_file" | head -1`
14   uuid=`read_and_escape_file "$uuid_file" | head -1`
15   body=`read_and_escape_file "$intermediate_file" | sed 1d`
16   published_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "${published}"`
17   printf "<entry>\n"
18   printf "<title type=\"html\">%s</title>\n" "$title"
19   printf "<id>urn:uuid:%s</id>\n" "$uuid"
20   printf "<updated>%s</updated>\n" "$lastmod_rfc3339"
21   printf "<published>%s</published>\n" "$published_rfc3339"
22   printf "<link href=\"%s%s\" />\n" "$basepath" "${htmlfile#\./}"
23   printf "<content type=\"html\">\n%s\n</content>\n" "$body"
24   printf "</entry>"
25 }
26
27 # Pull in global dependencies.
28 . ./helpers.sh
29 url_file=url.meta
30 author_file=author.meta
31 uuid_file=uuid.meta
32 title_file=title.meta
33 redo-ifchange "$url_file"
34 redo-ifchange "$author_file"
35 redo-ifchange "$uuid_file"
36 redo-ifchange "$title_file"
37
38 # Build some variables. XML-escape even file contents that should not contain
39 # dangerous characters, just to avoid any XML trouble.
40 base_url=`cat "$url_file" | head -1`
41 url_protocol=`echo $base_url | cut -d ':' -f 1`
42 url_basepath=`echo $base_url | cut -d '/' -f 3-`
43 url_basepath_escaped=`escape_url "$url_basepath"`
44 basepath="$url_protocol""://""$url_basepath_escaped"
45 title=`read_and_escape_file "$title_file" | head -1`
46 author=`read_and_escape_file "$author_file" | head -1`
47 uuid=`read_and_escape_file "$uuid_file" | head -1`
48
49 # Write majority of feed head.
50 cat << EOF
51 <?xml version="1.0" encoding="utf-8"?>
52 <feed xmlns="http://www.w3.org/2005/Atom">
53 EOF
54 printf "<link href=\"%s\" />\n" "$basepath"
55 printf "<link href=\"%sfeed.xml\" rel=\"self\" />\n" "$basepath"
56 printf "<title type=\"html\">%s</title>\n" "$title"
57 printf "<author><name>%s</name></author>\n" "$author"
58 printf "<id>urn:uuid:%s</id>\n" "$uuid"
59
60 # Generate feed entry snippets.
61 mkdir -p feed_snippets
62 for file in ./*.rst ./*.md; do
63   if [ -e "$file" ]; then
64     uuid_file="${file%.*}.uuid"
65     redo-ifchange "$uuid_file"
66     published=`stat -c%y "${uuid_file}"`
67     published_unix=$(date -u "+%s%N" -d "${published}")
68     entry=$(build_entry "${file}" "${uuid_file}" "${published}")
69     echo "${entry}" > ./feed_snippets/${published_unix}
70   fi
71 done
72
73 # Derive feed modification date from snippets.
74 mod_dates=$(grep -hE "^<updated>" ./feed_snippets/* | sed -E 's/<.?updated>//g')
75 last_mod_unix=0
76 for date in $mod_dates; do
77   date_unix=$(date -u "+%s" -d "${date}")
78   if [ "$date_unix" -gt "$last_mod_unix" ]; then
79     last_mod_unix=$date_unix
80   fi
81 done
82 lastmod_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "@${last_mod_unix}"`
83 printf "<updated>%s</updated>\n\n" "$lastmod_rfc3339"
84
85 # Write feed entries.
86 for file in ./feed_snippets/*; do
87   cat "${file}"
88   printf "\n"
89 done
90 rm -rf feed_snippets
91
92 # Write feed foot.
93 printf "</feed>"