home · contact · privacy
1e492ebc47f8b4e7f1f2eb25e8598c0d985fd5ab
[redo-blog] / processor / feed.xml.do
1 #!/bin/sh
2
3 # Pull in global dependencies.
4 . ./helpers.sh
5 url_file=url.meta
6 author_file=author.meta
7 uuid_file=uuid.meta
8 title_file=title.meta
9 redo-ifchange "$url_file"
10 redo-ifchange "$author_file"
11 redo-ifchange "$uuid_file"
12 redo-ifchange "$title_file"
13
14 # Build some variables. XML-escape even file contents that should not contain
15 # dangerous characters, just to avoid any XML trouble.
16 base_url=`cat "$url_file" | head -1`
17 url_protocol=`echo $base_url | cut -d ':' -f 1`
18 url_basepath=`echo $base_url | cut -d '/' -f 3-`
19 url_basepath_escaped=`escape_url "$url_basepath"`
20 basepath="$url_protocol""://""$url_basepath_escaped"
21 title=`read_and_escape_file "$title_file" | head -1`
22 author=`read_and_escape_file "$author_file" | head -1`
23 uuid=`read_and_escape_file "$uuid_file" | head -1`
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 # Iterate through most recent entries (go by lastmod date of source files) to
37 # build feed head "updated" element, and individual entries.
38 # FIXME: This ls parsing is a bad way to loop through the sorted files. Besides,
39 # $'\0' is a bashism.
40 first_run=0
41 files=`ls -1t *.rst *.md | head -10 | tr '\n' $'\0'`
42 oldIFS="$IFS"
43 IFS=$'\0'
44 for file in $files; do
45   lastmod=`stat -c%y "$file"`
46   lastmod_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "$lastmod"`
47   if [ "$first_run" -lt "1" ]; then
48     IFS="$oldIFS"
49     printf "<updated>%s</updated>\n\n" "$lastmod_rfc3339" 
50     first_run=1
51   fi
52
53   # Build some variables and dependencies.
54   intermediate_file="${file%.*}.intermediate"
55   htmlfile=`escape_url "${file%.*}.html"`
56   uuid_file="${file%.*}.uuid"
57   redo-ifchange "$intermediate_file"
58   redo-ifchange "$uuid_file"
59   title=`read_and_escape_file "$intermediate_file" | head -1`
60   uuid=`read_and_escape_file "$uuid_file" | head -1`
61   body=`read_and_escape_file "$intermediate_file" | sed 1d`
62   published=`stat -c%y "$uuid_file"`
63   published_rfc3339=`date -u "+%Y-%m-%dT%TZ" -d "$published"`
64
65   # Write entry.
66   printf "<entry>\n"
67   printf "<title type=\"html\">%s</title>\n" "$title"
68   printf "<id>urn:uuid:%s</id>\n" "$uuid" 
69   printf "<updated>%s</updated>\n" "$lastmod_rfc3339" 
70   printf "<published>%s</published>\n" "$published_rfc3339" 
71   printf "<link href=\"%s%s\" />\n" "$basepath" "$htmlfile"
72   printf "<content type=\"html\">\n%s\n</content>\n" "$body"
73   printf "</entry>\n\n"
74 done
75
76 printf "</feed>"