small improves
[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     read -p "Initialize blog? [y/n] " ask
16     [ "$ask" != "y" ] && exit 0
17
18     mkdir -p "$data_dir/drafts" &&\
19     mkdir -p "$data_dir/published" &&\
20     mkdir -p "$data_dir/html" 
21
22     echo "Created blog files"
23 }
24
25 refresh() {
26     start_token="<!-- BLOG START -->"
27     end_token="<!-- BLOG END -->"
28
29     read -p "Are you sure you want to refresh? [y/n] " ask
30     [ "$ask" != "y" ] && exit 0
31
32     # delete everything between tokens
33     sed -i "/$start_token/,/$end_token/{/$start_token/!{/$end_token/!d}}" "$blog_index_file"
34
35     # deletes all html files and republishes all published files
36 }
37
38 new() {
39     [ -z "$1" ] && echo "Please give your blog post a name (you should put it inside quotations)" && exit 1 
40
41     # sanitize input
42     sanitized=`echo -n "$1" | sed -e 's/[^A-Za-z0-9 _-]//g'| sed -e 's/ /-/g'`
43
44     # open in editor
45     $EDITOR "$data_dir/drafts/$sanitized.draft.html"
46 }
47
48 publish() {
49     
50     drafts=`ls -1 "$data_dir/drafts" | sed -e 's/\.draft\.html$//'`
51     [ -z "$drafts" ] && echo "No drafts to publish" && exit 0
52
53     echo "Select which draft to publish"
54     echo "$drafts" | nl 
55
56     read -p '> ' choice
57     to_publish=`ls -1 "$data_dir/drafts/" | sed -n "$choice p"`
58     [ -z "$to_publish" ] && echo "Invalid choice" && exit 1
59
60     cat $template_file |\
61         sed -e "s/{{TITLE}}/$to_publish/g;
62             s/{{DATE}}/`date +'%a, %b %d %H:%M'`/g" |\
63         sed -e "/{{BODY}}/r $data_dir/drafts/$to_publish" |\
64         sed -e "/{{BODY}}/d" \
65        > "$data_dir/html/${to_publish%.draft.html}.html" 
66
67     mv "$data_dir/drafts/$to_publish" "$data_dir/published/"
68
69     # Add new entry to blog index (do something about indent??)
70     sed -i "/<!-- BLOG START -->/ a <h3>$to_publish<\\/h3>" "$blog_index_file"
71
72 }
73
74
75 delete() {
76     published=`ls -1 "$data_dir/published" | sed -e 's/\.draft\.html$//'`
77     [ -z "$published" ] && echo "No posts to delete" && exit 0
78
79     echo "Select which post to delete"
80     echo "$published" | nl 
81
82     read -p '> ' choice
83     to_delete=`ls -1 "$data_dir/published/" | sed -n "$choice p"`
84     [ -z "$to_delete" ] && echo "Invalid choice" && exit 1
85
86     mv "$data_dir/published/$to_delete" "$data_dir/drafts/" &&\
87         rm "$data_dir/html/${to_delete%.draft.html}.html"
88
89     # remove entry from blog index
90 }
91
92 # check to see if all required files are present
93 [ ! -f $blog_index_file ] && echo "missing $blog_index_file" && exit 1
94 [ ! -f $rolling_file ] && echo "missing $rolling_file" && exit 1
95 [ ! -f $template_file ] && echo "missing $template_file" && exit 1
96 [ ! -f $rss_file ] && echo "missing $rss_file" && exit 1
97
98 # possibly also check to see if index and rolling have the proper headers
99
100
101 # check if blog dir exists
102 [ ! -d $data_dir ]  && init
103
104 case $1 in
105     i|init) init;;
106     n|new) new "$2";;
107     p|publish) publish;;
108     d|delete) delete;;
109     r|refresh) refresh;;
110     h|*) echo "helper";;
111 esac
112