5f85f3cb2e31af714815d95a6631dbeca60b66be
[pb.git] / pb
1 #!/bin/sh
2 # pinosaur's blog script
3
4 data_dir="blog"
5 website_url="https://www.youtube.com/watch?v=oHg5SJYRHA0/" # no ending slash
6 blog_index_file="blogindex.html"
7 rolling_file="rolling.html"
8 rss_file="rss.xml"
9 blog_template="template.html"
10 index_template="index_entry.html"
11 rolling_template="rolling_entry.html"
12 rss_template="rss_entry.html"
13
14 [ ! -z "$EDITOR" ] && EDITOR="vim"
15
16 init() {
17     read -p "Initialize blog here? [y/n] " ask
18     [ "$ask" != "y" ] && echo "Cancelled init" && exit 0
19
20     mkdir -p "$data_dir/drafts" "$data_dir/published" "$data_dir/html" "$data_dir/templates" "$data_dir/backups"
21     echo '<a href="{{URL}}">{{TITLE}}</a>' >> "$data_dir/templates/$index_template"
22     echo -e '<div>\n<h2>{{TITLE}}</h2>\n<p>{{DATE}}</p>\n<p>{{BODY}}</p>\n</div>' >> "$data_dir/templates/$rolling_template"
23     echo -e '<item>\n<title>{{TITLE}}</title>\n<link></link>\n<description><\description>\n<\item>' \
24         >> "$data_dir/templates/$rss_template"
25
26     echo "Successfully created blog files."
27 }
28 refresh() {
29     read -p "Are you sure you want to refresh? [y/n] " ask
30     [ "$ask" != "y" ] && echo "Aborting..." && exit 0
31
32     echo -e "$blog_index_file\n$rolling_file\n$rss_file" | xargs sed -i "/<!-- BLOG START -->/,/<!-- BLOG END -->/{/<!-- BLOG START -->/!{/<!-- BLOG END -->/!d}}"
33     echo "Successfully refreshed."
34 }
35
36 backup() {
37     backup_name=`mktemp --tmpdir="$data_dir/backups" -d "backup_$(date +'%b-%d')_XXX"`
38     echo "Creating backup, will be placed in $backup_name"
39     cp -r "$data_dir/drafts/" "$backup_name"
40     cp -r "$data_dir/published/" "$backup_name"
41 }
42
43 new() {
44     [ -z "$1" ] && echo "Please give your blog post a name (you should put it inside quotations)" && exit 1 
45     sanitized=`echo -n "$1" | sed -e 's/[^A-Za-z0-9 _-]//g'`
46     [ -f "$data_dir/drafts/$sanitized.draft.html" ] && echo "Blog of that name already exists." && exit 1
47     $EDITOR "$data_dir/drafts/$sanitized.draft.html"
48 }
49
50 sub() {
51     cat - |\
52         sed "1i <!-- ID:$1 START -->" |\
53         sed  "\$a <!-- ID:$1 END -->" |\
54         sed "s|{{TITLE}}|$1|g;
55             s|{{DATE}}|`date +'%a, %b %d %H:%M'`|g;
56             s|{{URL}}|$website_url/blog/html/$1.html|g" |\
57         sed "/{{BODY}}/r $data_dir/drafts/$1.draft.html" |\
58         sed "/{{BODY}}/d" 
59 }
60
61 # $1 is directory
62 choose() { 
63     options=`ls -1 "$1" | sed 's/\.draft\.html$//;s/\.html$//'` 
64     [ -z "$options" ] && echo "No drafts to publish" && exit 0
65     echo "$options" | nl
66     read -p 'Choose an entry by number > ' choice
67     chosen=`ls -1 "$1" | sed -n "$choice p"`
68     [ -z "$chosen" ] && echo "Invalid choice" && exit 1
69 }
70
71 publish() {
72     choose "$data_dir/drafts"
73     to_publish=${chosen%.draft.html}
74
75     cat $blog_template | sub "$to_publish" > "$data_dir/html/$to_publish.html" 
76
77     # make this part less horrendous
78     temp_index="$(mktemp)" 
79     cat "$data_dir/templates/$index_template" | sub "$to_publish" >> $temp_index
80     temp_rolling="$(mktemp)" 
81     cat "$data_dir/templates/$rolling_template" | sub "$to_publish" >> $temp_rolling
82     temp_rss="$(mktemp)" 
83     cat "$data_dir/templates/$rss_template" | sub "$to_publish" >> $temp_rss
84
85     # Add new entry to blog index (do something about indent??)
86     sed -i "/<!-- BLOG START -->/r $temp_index" "$blog_index_file"
87     sed -i "/<!-- BLOG START -->/r $temp_rolling" "$rolling_file"
88     sed -i "/<!-- BLOG START -->/r $temp_rss" "$rss_file"
89
90     mv "$data_dir/drafts/$to_publish.draft.html" "$data_dir/published/"
91     echo "Successfully published $to_publish"
92 }
93
94 delete() {
95     choose "$data_dir/published"
96     to_delete=${chosen%.draft.html}
97
98     mv "$data_dir/published/$to_delete.draft.html" "$data_dir/drafts/" &&\
99         rm "$data_dir/html/$to_delete.html"
100
101     # remove entries from files 
102     echo -e "$blog_index_file\n$rolling_file\n$rss_file" | xargs sed -i "/<!-- ID:$to_delete START -->/,/<!-- ID:$to_delete END -->/ d"
103
104     echo "Successfully deleted $to_delete"
105 }
106
107 # check to see if all required files are present
108 [ -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, $blog_template, $rolling_file and $rss_file in your home directory" && exit 1; }
109
110 # check if blog dir exists
111 [ ! -d $data_dir ]  && init && exit 0
112
113 case $1 in
114     n|new) new "$2";;
115     p|publish) publish;;
116     d|delete) delete;;
117     b|backup) backup;;
118     r|refresh) refresh;;
119     h|*) echo -e "=-=-=-=-=-=-= Pb =-=-=-=-=-=-=\nAvailable commands:\nn - new blog post\np - publish existing blog post\nd - deletes published post\nb - creates a backup";;
120 esac