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