shithub: news2atom

ref: fb5ab9787a06b5ab0002147c94957a65782607f9
dir: /news2atom/

View raw version
#!/bin/rc
#
# news2atom converter
#
# place files named: yyyy-mm-dd-name
# the owner of the file is the author of the article,
# the last modified date the <updated> value.
#
# sample entry:
#
#     First Line: Title Of Post
#     
#     First paragraph contains the summary of
#     the whole entry.
#     
#     The rest of the file is full text.
#
#
# usage: news2atom -d domain -u selfurl -t title [ -n newspath ] [ -s stylesheet ]

rfork e

flagfmt='d:domain domain, u:self selfurl, n:newspath newspath, t:title title, s:stylesheet stylesheet, m:max maxentries'
if(! ifs=() eval `{aux/getflags $*}){
	echo 'usage: news2atom -d domain -u selfurl -t title [ -n newspath ] [ -s stylesheet ] [ -m maxentries ]' >[1=2]
	exit usage
}

~ $#title 0 && { echo missing arg: title; exit usage }
~ $#self 0 && { echo missing arg: selfurl; exit usage }
~ $#newspath 0 && newspath=/lib/news
~ $#max 0 && max=30

mdate=`{date -ut}
nl='
'
n=()

echo '<?xml version="1.0" encoding="utf-8"?>'
~ $#stylesheet 0 || echo '<?xml-stylesheet type="text/xsl" href="'$"stylesheet'"?>'
echo '<feed xmlns="http://www.w3.org/2005/Atom">
<title>'$title'</title>
<updated>'$mdate'</updated>
<id>http://'$"domain'/</id>
<link rel="self" href="'$"self'"/>'

for(f in `{ls -t $newspath}){
	if(~ $#n $max){
		echo '</feed>'
		exit
	}
	n=($n 1)
	fname=`{basename $f}
	author=`{ls -l $f}
	author=$author(4)
	mt=`{mtime $f}
	pdate=`{echo $fname | sed 's/^([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9])-.*$/\1/'}
	mdate=`{date -ut $mt(1)}
	summary=`{sed -n '3,/^$/p' $f}
	fulltext=`'
'{sed '1,2d;3,/^$/d' $f | sed 's/^$/<\/p><p>/g'}
	taguri=`{echo 'tag:'$domain','$pdate':'$fname}
	title=`{sed 1q $f}
	echo '<entry>
	<title>'$"title'</title>
	<author><name>'$"author'</name></author>
	<updated>'$"mdate'</updated>
	<summary type="html"><![CDATA[<p>'$"summary'</p>]]></summary>
	<id>'$"taguri'</id>
	<content type="html"><![CDATA[<p><b>'$"summary'</b></p><p>'$"fulltext'</p>]]></content>
</entry>'
}
echo '</feed>'