summaryrefslogtreecommitdiff
path: root/apps/blagh/app.rc
blob: 2dbc101c3cd8c06045ee0b93cf0338f2551814ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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=`{/bin/date +%F|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 */[0-9][0-9][0-9][0-9]/ */*/[0-9][0-9]/ */*/*/[0-9][0-9]/) {
            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" />'
        }
        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^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>'

    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!'}
        echo '<article class="post">'
        sed '1s!.*![&]('^$l(2)^') <span class="date">'^$l(1)^'</span>!' < $p/index.md | $formatter
        echo '</article>'; echo
    }
    # 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 '|'
    eval ls -F $*^/.`{req_path_date} >[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 req_path_date {
    if (! ~ $blagh_uri '/')
        date_path=`{echo $req_path | sed 's/\'$blagh_uri'/'}
    if not
        date_path=$req_path

    switch($req_path){
        case */[0-9][0-9][0-9][0-9]/
            echo $date_path[0-9][0-9]/[0-9][0-9]/
        case */[0-9][0-9][0-9][0-9]/[0-9][0-9]/
            echo $date_path[0-9][0-9]/
        case */[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/
            echo $date_path
        case *
            echo /[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/
    }
}

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'
}