changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > werc / apps/blagh/app.rc

changeset 624: 5d1d08fd3787
parent: 3b0612a53aad
child: fb460a671d19
author: Uriel <u@berlinblue.org>
date: Sat, 17 Apr 2010 01:06:16 +0000
permissions: -rw-r--r--
description: Remove direct links to feeds, they can be added in a user's config easily enough.
1 fn conf_enable_blog {
2  blagh_uri=$conf_wd
3  blagh_dirs=$*
4  if(~ $#blagh_dirs 0)
5  blagh_dirs=( . )
6  conf_enable_app blagh
7 
8  if(~ $"conf_blog_editors '')
9  conf_blog_editors=blog-editors
10 
11  if(~ $"conf_max_posts_per_page '')
12  conf_max_posts_per_page=32
13 }
14 
15 fn blagh_init {
16  if(~ $#blagh_dirs 0 && ~ $req_path */[bB]log/*) {
17  blagh_uri=`{echo $req_path | sed 's,(/[bB]log/).*,\1,'}
18  blagh_dirs=( . )
19  }
20 
21  # Should not match sub-dirs!
22  if(! ~ $#blagh_dirs 0) {
23  # && test -d / `{echo '-a -d '^$blagh_root^$blagh_dirs}
24  blagh_url=$base_url^$blagh_uri
25  blagh_root=$sitedir^$blagh_uri
26  if(check_user $conf_blog_editors) {
27  editor_mode=on
28  if(~ $"post_arg_date '')
29  post_date=`{/bin/date +%F|sed 's,-,/,g'}
30  if not
31  post_date=$post_arg_date
32  ll_add handlers_bar_left echo '<a href="'$blagh_uri'new_post">Make a new post</a>'
33  }
34 
35  if(~ $req_path $blagh_uri) {
36  handler_body_main=blagh_body
37  u=$blagh_uri'index'
38  extraHeaders=$"extraHeaders ^ \
39 '<link rel="alternate" type="application/atom+xml" title="ATOM" href="'$"u'.atom" />
40 <link rel="alternate" type="application/rss+xml" title="RSS" href="'$"u'.rss" />'
41  }
42  if not if(~ $req_path $blagh_uri^index.atom)
43  blagh_setup_feed_handlers atom.tpl 'application/atom+xml'
44 
45  if not if(~ $req_path $blagh_uri^index.rss)
46  blagh_setup_feed_handlers rss20.tpl 'text/xml; charset=utf-8'
47 
48  if not if(~ $req_path $blagh_uri^new_post && ! ~ $#editor_mode 0) {
49  handler_body_main=( tpl_handler `{get_lib_file blagh/new_post.tpl apps/blagh/new_post.tpl} )
50  if(~ $REQUEST_METHOD POST) {
51  if(mkbpost $"post_arg_body $"post_date $"post_arg_title $post_arg_id)
52  post_redirect $blagh_uri
53  if not
54  notify_errors=$status
55  }
56  }
57 
58  }
59 }
60 
61 fn blagh_setup_feed_handlers {
62  handler_body_main=NOT_USED_by_blagh_feeds
63  res_tail=()
64  http_content_type=$2
65  headers=()
66  master_template=apps/blagh/$1 # Should we allow tempalte override?
67 }
68 
69 fn blagh_body {
70  if (! ~ $"blogTitle '')
71  echo '<h1>'$"blogTitle'</h1>'
72 
73  # Direct links to feeds are disabled because they are not very useful, add clutter and might waste pagerank.
74  # An user can add this on their own using handlers_body_head anyway.
75  #echo '<div style="text-align:right">(<a href="index.rss">RSS Feed</a>|<a href="index.atom">Atom Feed</a>)</div>'
76 
77  { # XXX Not sure why this fixes issues with blog setup, probably bug in fltr_cache!
78  for(p in `{get_post_list $blagh_root^$blagh_dirs}) {
79  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!'}
80  sed '1s!.*![&]('^$l(2)^') ('^$l(1)^')!' < $p/index.md
81  echo # Needed extra \n so markdown doesn't mess up the formatting, probably can be done in sed.
82  }
83  # XXX BUG! Markdown [references] break because multiple markdown documents are merged. Should format each blog post independently.
84  # TODO: use fltr_cache directly, that can fix the previous bug plus provide a perf boost by caching title generation.
85  } | $formatter
86 }
87 
88 fn get_post_list {
89  # /./->/|/ done to sort -t| and order by date
90  # Note: $paths in blagh_dirs should not contain '/./' or '|'
91  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'
92 }
93 
94 fn mkbpost {
95  bptext=$1
96  bpdate=$2
97  bptitle=$3
98  bpid=$4
99  _status=()
100  if(~ $"bptext '')
101  _status=($_status 'You need to provide a post body.')
102  if(! ~ $"bpdate [0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9])
103  _status=($_status 'Invalid date: '''^$"bpdate^'''') # XXX Should make semantic check.
104 
105  if(~ $#_status 0) {
106  umask 002 # Let group write
107  if(! ~ $"bpid '')
108  bpid=`{echo -n '-'^$bpid | sed 's/'$forbidden_uri_chars'+/_/g; 1q'}
109 
110  ddir=$blagh_root^$bpdate^'/'
111  n=`{ls $ddir >[2]/dev/null |wc -l}
112 
113  mkdir -p $ddir/$"n^$"bpid/
114  {
115  if(! ~ $"bptitle '') {
116  echo $bptitle
117  echo '========================================='
118  }
119  # TODO: Enable metadata
120  #echo '* Posted:' `{date}
121  #if(! ~ $#logged_user 0)
122  # echo '* Author: '$logged_user
123  echo
124  echo $bptext
125  }> $ddir/$"n^$"bpid/index.md
126 
127  # Experimental support for http://pubsubhubbub.googlecode.com/
128  if(! ~ $"conf_blog_pubsubdub_hub '') {
129  ifs='' { p=`{echo $req_url|sed 's/new_post$/index.atom/'|url_encode } }
130  dprint hget -p 'hub.mode=publish&hub.url='^$"p $conf_blog_pubsubdub_hub
131  hget -d -h -p 'hub.mode=publish&hub.url='^$"p $conf_blog_pubsubdub_hub >[1=2] &
132  }
133  }
134  status=$_status
135 }
136 
137 fn strip_title_from_md_file {
138  sed '1N; /^.*\n===*$/N; /.*\n===*\n$/d'
139 }