This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjbh.sh
306 lines (298 loc) · 7.74 KB
/
jbh.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#/bin/bash
#----------------------------------------
# JBH - Jekyll Blog Helper
_version="1.2.1"
# http://github.com/alanbarber/jbh
#----------------------------------------
# Settings
# Note: Always set leading and trailing slashes in path! Paths are relative.
_sitePath="/_site/"
_postPath="/_posts/"
_draftPath="/_drafts/"
_assetPath="/assets/"
_excerptSeparator="/--more--/"
# Remote Server Settings
_publishUseRsync="true"
_publishUser="username"
_publishServer="server.tld"
_publishPath="/home/username/public_html/"
########################################
# DO NOT EDIT BELOW THIS LINE
########################################
# Helper Function
function fnConvertTitleToFilenameFormat {
# lowercase, remove non alphanumerics and non spaces, convert spaces to dash
local _title=$(echo $1 | sed -e 's/\(.*\)/\L\1/')
_title=$(echo $_title | sed -e 's/[[:punct:]]//g')
_title=$(echo $_title | sed -e 's/\s/-/g')
echo "$_title"
}
function fnGetFileNameFromDateAndTitle {
local _title=$(fnConvertTitleToFilenameFormat "$2")
local _date=$(date -d "$1" +%Y-%m-%d)
echo "$_date-$_title.md"
}
function fnGetAssetDirectoryFromDateAndTitle {
if [[ "$_assetPath" != "" ]]; then
local _title=$(fnConvertTitleToFilenameFormat "$2")
echo "$_assetPath$(date -d $1 +%Y)/$(date -d $1 +%m)/$(date -d $1 +%d)/$_title"
else
echo ""
fi
}
# Feature Functions
# Version
function fnVersion {
echo "Jekyll Blog Helper $_version"
echo "Report bugs to <github.com/alanbarber/jbh>"
echo ""
}
# Help Info
function fnHelpInfo {
echo "Usage: jbh.sh [OPTIONS]..."
echo "Jekyll Blog Helper $_version - A command line blog management tool"
echo ""
echo "Options:"
echo ""
echo " -b, --build runs a jekyll build"
echo " -c, --clean cleans out the site output directory"
echo " -h, --help displays help info for the script"
echo " -l, --list lists all posts or drafts"
echo " -m, --move moves a draft to post or post to draft status"
echo " -n, --new creates a new post or draft"
echo " -p, --publish copies site via rcp/rsync to remote server"
echo " -s, --serve runs the jekyll server"
echo " -v, --version displays version of the script"
echo ""
echo "Modifiers:"
echo ""
echo " --build:"
echo " -d, --draft includes drafts in jekyll build"
echo " --list:"
echo " -d, --draft lists draft posts"
echo " -p, --post lists posts"
echo " --new:"
echo " -d, --draft creates a new draft post"
echo " --move:"
echo " -d, --draft moves a draft to post"
echo " -p, --post moves a post to draft"
echo " --serve:"
echo " -d, --draft includes draft in jekyll server"
echo ""
echo "Examples:"
echo ""
echo " jbh.sh --new \"Blog title\""
echo " Creates a new post with the given title"
echo ""
echo " jbh.sh --new \"Blog title\" \"1/1/2015\""
echo " Create a new post on a specific date"
echo ""
echo " jbh.sh --new --draft \"Blog title\""
echo " Creates a new draft post with the given title"
echo ""
echo " jbh.sh --build"
echo " Builds the site"
echo ""
echo " jbh.sh --publish"
echo " Runs rcp/rsync to copy built site to a remote server"
echo " * NOTE: Server settings are stored at top of script"
echo ""
echo " jbh.sh --list \"*2015*\""
echo " Lists all posts that have '2015' in the file name"
echo ""
echo " jbh.sh --move --draft \"2015-01-01-blog-title.md\""
echo " Moves the matching file from draft to post folder"
echo ""
echo "Report bugs to <github.com/alanbarber/jbh>"
echo ""
}
# Build
function fnBuild {
echo "Running Jekyll build..."
if [[ "$1" == "-d" || "$1" == "--draft" ]]; then
echo "Including drafts..."
jekyll build --destination ".$_sitePath" --drafts
else
jekyll build --destination ".$_sitePath"
fi
}
# Clean
function fnClean {
echo "Cleaning site directory '$_sitePath'..."
# we remove and recreate the dir to make sure all files
# including hidden dot files (.htaccess, etc) are gone
rm -rf .$_sitePath
mkdir -p .$_sitePath
}
# List
function fnList {
if [[ "$1" == "-d" || "$1" == "--draft" ]]; then
echo "Listing draft posts..."
local _listPath="$_draftPath"
local _listSearch="$2"
elif [[ "$1" == "-p" || "$1" == "--post" ]]; then
echo "Listing posts..."
local _listPath="$_postPath"
local _listSearch="$2"
else
echo "Listing posts..."
local _listPath="$_postPath"
local _listSearch="$1"
fi
ls -Ax1 .$_listPath$_listSearch
}
# Move Posts
function fnMove {
local _moveFile="$2"
if [[ ("$1" == "-d" || "$1" == "--draft") && "$_moveFile" != "" ]]; then
echo "Moving draft to post..."
if [ -e ".$_draftPath$_moveFile" ]; then
mv -f .$_draftPath$_moveFile .$_postPath$_moveFile
else
echo " Error: Unable to find draft '$_moveFile'"
exit 1
fi
elif [[ ("$1" == "-p" || "$1" == "--post") && "$_moveFile" != "" ]]; then
echo "Moving post to draft..."
if [[ -e ".$_postPath$_moveFile" || -e "" ]]; then
mv -f .$_postPath$_moveFile .$_draftPath$_moveFile
else
echo " Error: Unable to file post '$_moveFile'"
exit 1
fi
else
echo " Error: No file specified to move"
echo ""
exit 1
fi
}
# New Post
function fnNew {
echo "Creating new post..."
if [[ "$1" == "-d" || "$1" == "--draft" ]]; then
echo " Making post a draft..."
local _title="$2"
if [ "$3" == "" ]; then
local _date=$(date +%Y-%m-%d)
else
local _date=$(date -d $3 +%Y-%m-%d)
fi
local _path="$_draftPath"
else
_title="$1"
if [ "$2" == "" ]; then
local _date=$(date +%Y-%m-%d)
else
local _date=$(date -d $2 +%Y-%m-%d)
fi
local _path="$_postPath"
fi
# setup vars
local _fileName=$(fnGetFileNameFromDateAndTitle "$_date" "$_title")
local _outputFile="$_path$_fileName"
local _assetDir=$(fnGetAssetDirectoryFromDateAndTitle "$_date" "$_title")
# Create file and write Jekyll header info
echo " Creating file '$_outputFile'..."
echo " Title: $_title"
echo " Date: $_date"
echo "---" > ".$_outputFile"
echo "layout: post" >> ".$_outputFile"
echo "title: \"$_title\"" >> ".$_outputFile"
echo "date: $_date" >> ".$_outputFile"
echo "categories: []" >> ".$_outputFile"
echo "tags: []" >> ".$_outputFile"
echo "---" >> ".$_outputFile"
# Create asset directory for post and add markdown link reference to bottom
if [[ "_assetDir" != "" ]]; then
echo -e "\n\n$_excerptSeparator\n\n[assets]: $_assetDir" >> ".$_outputFile"
echo " Creating asset folder '$_assetDir'"
mkdir -p ".$_assetDir"
fi
}
# Publish
function fnPublish {
echo "Publishing site to remote server..."
# call rsync using settings
if [[ "$_publishPath" == "" || "$_publishUser" == "" || "$_publishServer" == "" ]]; then
echo " Error: Unable to determine settings to publish to remote server!"
echo ""
exit 1
else
if [[ "$_publishUseRsync" == "" || "$_publishUseRsync" == "0" || "$_publishUseRsync" == "false" ]]; then
scp -r .$_sitePath* $_publishUser@$_publishServer:$_publishPath
else
rsync --compress --recursive --checksum --itemize-changes --delete .$_sitePath $_publishUser@$_publishServer:$_publishPath
fi
fi
}
# Server
function fnServe {
echo "Running Jekyll server..."
if [[ "$1" == "-d" || "$1" == "--draft" ]]; then
echo "Including drafts..."
jekyll serve --destination ".$_sitePath" --drafts
else
jekyll serve --destination ".$_sitePath"
fi
}
# Process command line# Parse options
case "$1" in
-b)
fnBuild "$2"
;;
--build)
fnBuild "$2"
;;
-c)
fnClean
;;
--clean)
fnClean
;;
--clear)
fnClean
;;
-l)
fnList "$2" "$3"
;;
--list)
fnList "$2" "$3"
;;
-m)
fnMove "$2" "$3"
;;
--move)
fnMove "$2" "$3"
;;
-n)
fnNew "$2" "$3" "$4"
;;
--new)
fnNew "$2" "$3" "$4"
;;
-p)
fnPublish
;;
--publish)
fnPublish
;;
-s)
fnServe "$2"
;;
--serve)
fnServe "$2"
;;
--server)
fnServe "$2"
;;
-v)
fnVersion
;;
--version)
fnVersion
;;
*)
fnHelpInfo
;;
esac
exit 0