publish and delete
[pb.git] / pb
1 #!/bin/sh
2
3 # pinosaur's blog script
4
5 blog_index_file="blogindex.html"
6 rolling_file="rolling.html"
7 template_file="template.html"
8 rss_file="rss.xml"
9 data_dir="blog"
10
11 [ ! -z "$EDITOR" ] && EDITOR="vim"
12
13
14 init() {
15     echo "initing blog"
16     mkdir -p "$data_dir/drafts" &&\
17     mkdir -p "$data_dir/published" &&\
18     mkdir -p "$data_dir/html" 
19 }
20
21 refresh() {
22     echo 1
23     # add a confirmation of sorts here
24
25     # deletes all html files and republishes all published files
26 }
27
28 new() {
29     [ -z "$1" ] && echo "please supply a name" && exit 1 
30
31     # sanitize input
32     sanitized=`echo -n "$1" | sed -e 's/[^A-Za-z0-9 _-]//g'| sed -e 's/ /-/g'`
33
34     # open in editor
35     $EDITOR "$data_dir/drafts/$sanitized.draft.html"
36 }
37
38 publish() {
39     
40     drafts=`ls -1 "$data_dir/drafts" | sed -e 's/\.draft\.html$//'`
41     [ -z "$drafts" ] && echo "No drafts to publish" && exit 0
42
43     echo "Select which post to publish"
44     echo "$drafts" | nl 
45
46     read -p '> ' choice
47     to_publish=`ls -1 "$data_dir/drafts/" | sed -n "$choice p"`
48     [ -z "$to_publish" ] && echo "Invalid choice" && exit 1
49
50     cat $template_file |\
51         sed -e "s/{{TITLE}}/$to_publish/g;
52             s/{{DATE}}/`date +'%a, %b %d %H:%M'`/g" |\
53         sed -e "/{{BODY}}/r $data_dir/drafts/$to_publish" |\
54         sed -e "/{{BODY}}/d" \
55        > "$data_dir/html/${to_publish%.draft.html}.html" 
56
57     mv "$data_dir/drafts/$to_publish" "$data_dir/published/"
58
59     # Add new entry to blog index
60     #sed -e ""
61
62
63 }
64
65
66 delete() {
67     published=`ls -1 "$data_dir/published" | sed -e 's/\.draft\.html$//'`
68     [ -z "$published" ] && echo "No posts to delete" && exit 0
69
70     echo "Select which post to delete"
71     echo "$published" | nl 
72
73     read -p '> ' choice
74     to_delete=`ls -1 "$data_dir/published/" | sed -n "$choice p"`
75     [ -z "$to_delete" ] && echo "Invalid choice" && exit 1
76
77     mv "$data_dir/published/$to_delete" "$data_dir/drafts/" &&\
78         rm "$data_dir/html/${to_delete%.draft.html}.html"
79
80     # remove entry from blog index
81 }
82
83 # check to see if all required files are present
84 [ ! -f $blog_index_file ] && echo "missing $blog_index_file" && exit 1
85 [ ! -f $rolling_file ] && echo "missing $rolling_file" && exit 1
86 [ ! -f $template_file ] && echo "missing $template_file" && exit 1
87 [ ! -f $rss_file ] && echo "missing $rss_file" && exit 1
88
89 # possibly also check to see if index and rolling have the proper headers
90
91
92 # check if blog dir exists
93 [ ! -d $data_dir ]  && init
94
95 case $1 in
96     i|init) init;;
97     n|new) new "$2";;
98     p|publish) publish;;
99     d|delete) delete;;
100     r|refresh) echo "refresh";;
101     h|*) echo "helper" && exit 1;;
102 esac
103