3 # pinosaur's blog script
5 blog_index_file="blogindex.html"
6 rolling_file="rolling.html"
7 template_file="template.html"
8 index_entry_template="index_entry.html"
12 [ ! -z "$EDITOR" ] && EDITOR="vim"
16 read -p "Initialize blog? [y/n] " ask
17 [ "$ask" != "y" ] && exit 0
19 mkdir -p "$data_dir/drafts" "$data_dir/published" "$data_dir/html" "$data_dir/templates"
21 echo '<p id="{{TITLE}}">{{TITLE}}</p>' >> "$data_dir/templates/$index_entry_template"
23 echo "Created blog files"
27 start_token="<!-- BLOG START -->"
28 end_token="<!-- BLOG END -->"
30 read -p "Are you sure you want to refresh? [y/n] " ask
31 [ "$ask" != "y" ] && exit 0
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"
38 # deletes all html files and republishes all published files
42 [ -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'| sed -e 's/ /-/g'`
48 $EDITOR "$data_dir/drafts/$sanitized.draft.html"
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" |\
61 drafts=`ls -1 "$data_dir/drafts" | sed -e 's/\.draft\.html$//'`
62 [ -z "$drafts" ] && echo "No drafts to publish" && exit 0
64 echo "Select which draft to publish"
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
72 cat $template_file | sub "$to_publish" \
73 > "$data_dir/html/$to_publish.html"
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 ^
80 mv "$data_dir/drafts/$to_publish.draft.html" "$data_dir/published/"
85 published=`ls -1 "$data_dir/published" | sed -e 's/\.draft\.html$//'`
86 [ -z "$published" ] && echo "No posts to delete" && exit 0
88 echo "Select which post to delete"
89 echo "$published" | nl
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
96 mv "$data_dir/published/$to_delete.draft.html" "$data_dir/drafts/" &&\
97 rm "$data_dir/html/$to_delete.html"
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"
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; }
107 # possibly also check to see if index and rolling have the proper headers
110 # check if blog dir exists
111 [ ! -d $data_dir ] && init