adding a removing multiple lines working
[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 [ ! -z "$EDITOR" ] && EDITOR="vim"
18
19 init() {
20     read -p "Initialize blog here? [y/n] " ask
21     [ "$ask" != "y" ] && exit 0
22
23     mkdir -p "$data_dir/drafts" "$data_dir/published" "$data_dir/html" "$data_dir/templates" 
24
25     echo '<p id="{{TITLE}}">{{TITLE}}</p>' >> "$data_dir/templates/$index_template"
26     echo -e '<div id="{{TITLE}}">\n<h2>{{TITLE}}</h2>\n<p>{{DATE}}</p></div>' >> "$data_dir/templates/$rolling_template"
27     echo -e '<item>\n<title>{{TITLE}}</title>\n<link></link>\n<description><\description>\n<\item>' \
28         >> "$data_dir/templates/$rss_template"
29
30     echo "Created blog files"
31 }
32
33 refresh() {
34
35     read -p "Are you sure you want to refresh? [y/n] " ask
36     [ "$ask" != "y" ] && exit 0
37
38     # delete everything between tokens (remove dupe code)
39     echo -e "$blog_index_file\n$rolling_file\n$rss_file" | xargs sed -i "/<!-- BLOG START -->/,/<!-- BLOG END -->/{/<!-- BLOG START -->/!{/<!-- BLOG END -->/!d}}"
40
41     # deletes all html files and republishes all published files
42
43     echo "Refreshed."
44 }
45
46 new() {
47     [ -z "$1" ] && echo "Please give your blog post a name (you should put it inside quotations)" && exit 1 
48     sanitized=`echo -n "$1" | sed -e 's/[^A-Za-z0-9 _-]//g'| sed -e 's/ /-/g'`
49     $EDITOR "$data_dir/drafts/$sanitized.draft.html"
50 }
51
52 sub() {
53     cat - |\
54         sed "1i <!-- ID:$1 START -->" |\
55         sed  "\$a <!-- ID:$1 END -->" |\
56         sed "s|{{TITLE}}|$1|g;
57             s|{{DATE}}|`date +'%a, %b %d %H:%M'`|g;
58             s|{{URL}}|$website_url/$1|g" |\
59         sed "/{{BODY}}/r $data_dir/drafts/$1" |\
60         sed "/{{BODY}}/d" 
61 }
62
63 # $1 is directory
64 choose() { # working on abstraction
65     options=`ls -1 "$1" | sed 's/\.draft\.html$//;s/\.html$//'` 
66     [ -z "$options" ] && echo "No drafts to publish" && exit 0
67     echo "$options" | nl
68     read -p 'Choose an entry by number > ' choice
69     chosen=`ls -1 "$1" | sed -n "$choice p"`
70     [ -z "$chosen" ] && echo "Invalid choice" && exit 1
71 }
72
73 publish() {
74     
75     choose "$data_dir/drafts"
76     to_publish=${chosen%.draft.html}
77
78     cat $blog_template | sub "$to_publish" \
79        > "$data_dir/html/$to_publish.html" 
80
81     temp_index="$(mktemp)" # probably bad idea
82     cat "$data_dir/templates/$index_template" | sub "$to_publish" >> $temp_index
83     temp_rolling="$(mktemp)" 
84     cat "$data_dir/templates/$rolling_template" | sub "$to_publish" >> $temp_rolling
85     temp_rss="$(mktemp)" 
86     cat "$data_dir/templates/$rss_template" | sub "$to_publish" >> $temp_rss
87
88     # Add new entry to blog index (do something about indent??)
89     sed -i "/<!-- BLOG START -->/r $temp_index" "$blog_index_file"
90     sed -i "/<!-- BLOG START -->/r $temp_rolling" "$rolling_file"
91     sed -i "/<!-- BLOG START -->/r $temp_rss" "$rss_file"
92
93     mv "$data_dir/drafts/$to_publish.draft.html" "$data_dir/published/"
94
95     echo "Successfully published $to_publish"
96
97 }
98
99 delete() {
100     choose "$data_dir/published"
101     to_delete=${chosen%.draft.html}
102
103     mv "$data_dir/published/$to_delete.draft.html" "$data_dir/drafts/" &&\
104         rm "$data_dir/html/$to_delete.html"
105
106     # remove entries from files 
107     echo -e "$blog_index_file\n$rolling_file\n$rss_file" | xargs sed -i "/<!-- ID:$to_delete START -->/,/<!-- ID:$to_delete END -->/ d"
108
109     echo "Successfully deleted $to_delete"
110 }
111
112 # check to see if all required files are present
113 [ -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; }
114
115 # possibly also check to see if index and rolling have the proper headers
116
117
118 # check if blog dir exists
119 [ ! -d $data_dir ]  && init && exit
120
121 case $1 in
122     n|new) new "$2";;
123     p|publish) publish;;
124     d|delete) delete;;
125     r|refresh) refresh;;
126     h|*) echo "helper";;
127 esac
128