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