diff options
Diffstat (limited to 'apps/blagh')
-rw-r--r-- | apps/blagh/app.rc | 142 | ||||
-rw-r--r-- | apps/blagh/atom.tpl | 58 | ||||
-rwxr-xr-x | apps/blagh/convert.rc | 20 | ||||
-rw-r--r-- | apps/blagh/jsonfeed.tpl | 35 | ||||
-rw-r--r-- | apps/blagh/new_post.tpl | 11 | ||||
-rw-r--r-- | apps/blagh/rss20.tpl | 43 |
6 files changed, 309 insertions, 0 deletions
diff --git a/apps/blagh/app.rc b/apps/blagh/app.rc new file mode 100644 index 0000000..c63689d --- /dev/null +++ b/apps/blagh/app.rc @@ -0,0 +1,142 @@ +fn conf_enable_blog { + blagh_uri=$conf_wd + blagh_dirs=$* + if(~ $#blagh_dirs 0) + blagh_dirs=( . ) + conf_enable_app blagh + + if(~ $"conf_blog_editors '') + conf_blog_editors=blog-editors + + if(~ $"conf_max_posts_per_page '') + conf_max_posts_per_page=32 +} + +fn blagh_init { + if(~ $#blagh_dirs 0 && ~ $req_path */[bB]log/*) { + blagh_uri=`{echo $req_path | sed 's,(/[bB]log/).*,\1,'} + blagh_dirs=( . ) + } + + # Should not match sub-dirs! + if(! ~ $#blagh_dirs 0) { + # && test -d / `{echo '-a -d '^$blagh_root^$blagh_dirs} + blagh_url=$base_url^$blagh_uri + blagh_root=$sitedir^$blagh_uri + if(check_user $conf_blog_editors) { + editor_mode=on + if(~ $"post_arg_date '') + post_date=`{datei|sed 's,-,/,g'} + if not + post_date=$post_arg_date + ll_add handlers_bar_left echo '<a href="'$blagh_uri'new_post">Make a new post</a>' + } + + if(~ $req_path $blagh_uri) { + handler_body_main=blagh_body + u=$blagh_uri'index' + extraHeaders=$"extraHeaders ^ \ +'<link rel="alternate" type="application/atom+xml" title="ATOM" href="'$"u'.atom" /> +<link rel="alternate" type="application/rss+xml" title="RSS" href="'$"u'.rss" /> +<link rel="alternate" type="application/json" title="JSON" href="'$"blagh_uri'feed.json" />' + } + if not if(~ $req_path $blagh_uri^index.atom) + blagh_setup_feed_handlers atom.tpl 'application/atom+xml' + + if not if(~ $req_path $blagh_uri^index.rss) + blagh_setup_feed_handlers rss20.tpl 'text/xml; charset=utf-8' + + if not if(~ $req_path $blagh_uri^feed.json) + blagh_setup_feed_handlers jsonfeed.tpl 'application/json; charset=utf-8' + + if not if(~ $req_path $blagh_uri^new_post && ! ~ $#editor_mode 0) { + handler_body_main=( tpl_handler `{get_lib_file blagh/new_post.tpl apps/blagh/new_post.tpl} ) + if(~ $REQUEST_METHOD POST) { + if(mkbpost $"post_arg_body $"post_date $"post_arg_title $post_arg_id) + post_redirect $blagh_uri + if not + notify_errors=$status + } + } + + } +} + +fn blagh_setup_feed_handlers { + handler_body_main=NOT_USED_by_blagh_feeds + res_tail=() + http_content_type=$2 + headers=() + master_template=apps/blagh/$1 # Should we allow tempalte override? +} + +fn blagh_body { + if (! ~ $"blogTitle '') + echo '<h1>'$"blogTitle'</h1>' + + # Direct links to feeds are disabled because they are not very useful, add clutter and might waste pagerank. + # An user can add this on their own using handlers_body_head anyway. + #echo '<div style="text-align:right">(<a href="index.rss">RSS Feed</a>|<a href="index.atom">Atom Feed</a>)</div>' + + # XXX Not sure why this fixes issues with blog setup, probably bug in fltr_cache! + for(p in `{get_post_list $blagh_root^$blagh_dirs}) { + l=`{echo -n $p|sed 's!'$sitedir^'/?(.*)([0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9])(/[^/]+/)!\2 /\1\2\3!'} + sed '1s!.*^') ('^$l(1)^')!' < $p/index.md + echo # Needed extra \n so markdown doesn't mess up the formatting, probably can be done in sed. + } | $formatter + # XXX BUG! Markdown [references] break because multiple markdown documents are merged. Should format each blog post independently. + # TODO: use fltr_cache directly, that can fix the previous bug plus provide a perf boost by caching title generation. +} + +fn get_post_list { + # /./->/|/ done to sort -t| and order by date + # Note: $paths in blagh_dirs should not contain '/./' or '|' + ls -F $*^/./[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/ >[2]/dev/null | sed -n '/'^$forbidden_uri_chars^'/d; s,/\./,/|/,; /\/$/p' | sort -r '-t|' +1 | sed -e 's,/+\|/+,/,' -e $conf_max_posts_per_page^'q' +} + +fn mkbpost { + bptext=$1 + bpdate=$2 + bptitle=$3 + bpid=$4 + _status=() + if(~ $"bptext '') + _status=($_status 'You need to provide a post body.') + if(! ~ $"bpdate [0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]) + _status=($_status 'Invalid date: '''^$"bpdate^'''') # XXX Should make semantic check. + + if(~ $#_status 0) { + umask 002 # Let group write + if(! ~ $"bpid '') + bpid=`{echo -n '-'^$bpid | sed 's/'$forbidden_uri_chars'+/_/g; 1q'} + + ddir=$blagh_root^$bpdate^'/' + n=`{ls $ddir >[2]/dev/null |wc -l} + + mkdir -p $ddir/$"n^$"bpid/ + { + if(! ~ $"bptitle '') { + echo $bptitle + echo '=========================================' + } + # TODO: Enable metadata + #echo '* Posted:' `{date} + #if(! ~ $#logged_user 0) + # echo '* Author: '$logged_user + echo + echo $bptext + }> $ddir/$"n^$"bpid/index.md + + # Experimental support for http://pubsubhubbub.googlecode.com/ + if(! ~ $"conf_blog_pubsubdub_hub '') { + ifs='' { p=`{echo $req_url|sed 's/new_post$/index.atom/'|url_encode } } + dprint hget -p 'hub.mode=publish&hub.url='^$"p $conf_blog_pubsubdub_hub + hget -d -h -p 'hub.mode=publish&hub.url='^$"p $conf_blog_pubsubdub_hub >[1=2] & + } + } + status=$_status +} + +fn strip_title_from_md_file { + sed '1N; /^.*\n===*$/N; /.*\n===*\n$/d' +} diff --git a/apps/blagh/atom.tpl b/apps/blagh/atom.tpl new file mode 100644 index 0000000..97c665f --- /dev/null +++ b/apps/blagh/atom.tpl @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="utf-8"?> + +%{ +# See for more info:http://www.tbray.org/ongoing/When/200x/2005/07/27/Atomic-RSS +fn statpost { + f = $1 + + post_uri=$base_url^`{cleanname `{echo $f | sed -e 's!^'$sitedir'!!'}}^'/' + title=`{read $f/index.md} + by=`{ls -m $f | sed 's/^\[//g; s/].*$//g' >[2]/dev/null} + ifs=() { summary=`{cat $f/index.md | strip_title_from_md_file | ifs=$difs {$formatter} } } +} +# rfc3339 date when feed was last updated. +fupdated = `{ndate -a `{date `{mtime `{ls $blagh_root$blagh_dirs/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/[0-9] | tail -1} | awk '{print $1}'}}} +%} + +<feed xmlns="http://www.w3.org/2005/Atom" + xmlns:thr="http://purl.org/syndication/thread/1.0"> + +% if(! ~ $"conf_blog_pubsubdub_hub '') { +% echo '<link rel="hub" href="'$conf_blog_pubsubdub_hub'" />' +% } + + <link rel="self" href="%($base_url^$req_path%)"/> + <id>%($base_url^$req_path%)</id> + <icon><![CDATA[/favicon.ico]]></icon> + + <title><![CDATA[%($siteTitle%)]]></title> + <subtitle><![CDATA[%($siteSubTitle%)]]></subtitle> + + <updated>%($fupdated%)</updated> + <link href="."/> + +% for(f in `{get_post_list $blagh_root$blagh_dirs}) { +% statpost $f + + <entry> +% # Maybe we should be smarter, see: http://diveintomark.org/archives/2004/05/28/howto-atom-id, example: <id>tag:intertwingly.net,2004:2899</id> + <id>%($post_uri%)</id> + <link href="%($post_uri%)"/> + <title><![CDATA[%($title%)]]></title> +% # <link rel="replies" href="2899.atom" thr:count="0"/> + <author><name><![CDATA[%($by%)]]></name></author> + + <content type="xhtml"><div xmlns="http://www.w3.org/1999/xhtml"> + <![CDATA[%($summary%)]]> + </div></content> + +% # rfc3339 date when entry was last updated. +% eupdated=`{ndate -a `{date `{mtime $f | awk '{print $1}'}}} + <updated>%($eupdated%)</updated> + </entry> + +% } + +</feed> + +% exit diff --git a/apps/blagh/convert.rc b/apps/blagh/convert.rc new file mode 100755 index 0000000..0640805 --- /dev/null +++ b/apps/blagh/convert.rc @@ -0,0 +1,20 @@ +#!/usr/bin/env rc + +path=($PLAN9/bin/ $path) + +for(p in *.md) { + echo + echo '=========================' + echo p $p + pp=`{echo $p | sed 's/^([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])[\-_](.*).md$/\1 \2 \3 \4/' } + echo pp $pp + + d=$pp(1)^'/'^$pp(2)^'/'^$pp(3)^'/'^$pp(4)^'/' + + mkdir -p $d + echo $pp(4) | sed -e 's/^[0-9]_//; s/_/ /g;' > $d/index.md + echo '=================================' >> $d/index.md + echo >> $d/index.md + cat $p >> $d/index.md + +} diff --git a/apps/blagh/jsonfeed.tpl b/apps/blagh/jsonfeed.tpl new file mode 100644 index 0000000..fd97ed4 --- /dev/null +++ b/apps/blagh/jsonfeed.tpl @@ -0,0 +1,35 @@ +{ +"version": "https://jsonfeed.org/version/1", +"title": "%($siteTitle%)", +"home_page_url": "%($"base_url%)", +"feed_url": "%($"base_url^$"req_path%)", +"items": [ +%{ +fn statpost { + f = $1 + post_uri=$base_url^`{cleanname `{echo $f | sed -e 's!^'$sitedir'!!'}}^'/' + title=`{read $f/index.md} + #ifs=() { summary=`{cat $f/index.md | crop_text 1024 ... | $formatter } } + ifs=() { summary=`{cat $f/index.md | strip_title_from_md_file | ifs=$difs {$formatter| sed 's/"/\\"/g' | tr -d '\012' } } } +} +%} +% #for(f in `{get_post_list $blagh_root$blagh_dirs}) { +% +% postlist=`{get_post_list $blagh_root$blagh_dirs} +% postcount=0 +% for(f in $postlist) { +% statpost $f + { + "id": "%($post_uri%)", + "url": "%($post_uri%)", + "title": "%($title%)", + "content_html": "%($summary%)" + } +% postcount = `{echo $postcount 1+p | dc} +% if (! ~ $#postlist $postcount) { echo , } +% } +] +} + +% exit + diff --git a/apps/blagh/new_post.tpl b/apps/blagh/new_post.tpl new file mode 100644 index 0000000..bd521c4 --- /dev/null +++ b/apps/blagh/new_post.tpl @@ -0,0 +1,11 @@ +<div> +% notices_handler +<form method="POST"><fieldset> + <legend>Submit a new blog post</legend> + <textarea cols="94" rows=16" name="body">%($"post_arg_body%)</textarea><br /> + <label>Title: <input size="64" type="text" name="title" value="%($"post_arg_title%)" /></label> + <label>Id: <input size="8" type="text" name="id" value="%($"post_arg_id%)" /></label> + <label>Date: <input size="10" maxlength="10" type="text" name="date" value="%($"post_date%)" /></label> + <input type="submit" value="Post" /> +</fieldset></form> +</div> diff --git a/apps/blagh/rss20.tpl b/apps/blagh/rss20.tpl new file mode 100644 index 0000000..0cba818 --- /dev/null +++ b/apps/blagh/rss20.tpl @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="UTF-8"?> + +%{ +fn statpost { + f = $1 + post_uri = `{echo $f | sed 's,^'$sitedir',,'} + title=`{read $f/index.md} + post_uri=$base_url^`{cleanname `{echo $f | sed -e 's!^'$sitedir'!!'}}^'/' + by=`{ls -m $f | sed 's/^\[//g; s/].*$//g' >[2]/dev/null} + ifs=() {summary=`{ cat $f/index.md |strip_title_from_md_file| ifs=$difs {$formatter | escape_html} }} +} + +%} + +<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> + <channel> + <atom:link href="%($base_url^$req_path%)" rel="self" type="application/rss+xml" /> + <title><![CDATA[%($siteTitle%)]]></title> + <link>%($base_url^$req_path%)</link> + <description><![CDATA[%($blogDesc%)]]></description> + <language>en-us</language> + <generator><![CDATA[Tom Duff's rc, and Kris Maglione's clever hackery]]></generator> +%{ + # <webMaster>uriel99+rss@gmail.com (Uriel)</webMaster> + # rfc2822 last time channel content changed. + lbd=`{ndate -m `{date `{mtime `{ls $blagh_root$blagh_dirs/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/[0-9] | tail -1} | awk '{print $1}'}}} + echo '<lastBuildDate>'$"lbd'</lastBuildDate>' + # rfc2822 publication date for content in the channel. + pubdate=`{ndate -m} + for(f in `{get_post_list $blagh_root$blagh_dirs}){ + statpost $f +%} + <item> + <title><![CDATA[%($title%)]]></title> + <author><![CDATA[%($by%)@noreply.cat-v.org (%($by%))]]></author> + <link>%($post_uri%)</link> + <guid isPermaLink="true">%($post_uri%)</guid> + <pubDate>%($pubdate%)</pubDate> + <description> %($summary%) </description> + </item> +% } + </channel> +</rss> |