summaryrefslogtreecommitdiff
path: root/build-page.sh
blob: edbcbffbc84d01e7b7c94d330deaa19ba6e5f57c (plain)
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
#!/bin/bash

INPUT=$1
TEMPLATE=$2

# we build the OUTPUT dir by:
# - taking the INPUT value and replacing `content` with `dist`
# - replacing `<filename>.txt` with `<filename>.html`
#
# refs:
# - <https://www.thegeekstuff.com/2010/06/bash-array-tutorial/>
# - <https://zaiste.net/posts/how-to-join-elements-of-array-bash/>

function join { local IFS="$1"; shift; echo "$*"; }

# create an array from INPUT_ARRAY
# './content/worklog/<filename>' => ['.', 'content', 'worklog', <filename>]
IFS='/' read -ra INPUT_ARRAY <<< "$INPUT"

ARR_LEN=${#INPUT_ARRAY[@]}

# select last item from array
INPUT_FILENAME=${INPUT_ARRAY[$ARR_LEN -1]}

# split `<filename>.txt`, get an array, [<filename>, 'txt']
IFS='.' read -ra FILENAME_ARRAY <<< "$INPUT_FILENAME"

# replace array's item `txt` w/ `html`
OUTPUT_FILENAME_ARRAY=$(echo ${FILENAME_ARRAY[@]/txt/html})
# join array => `<filename>.html`
OUTPUT_FILENAME=$(join . ${OUTPUT_FILENAME_ARRAY[@]})

# make new array w/ INPUT_ARRAY minus <filename> and OUTPUT_FILENAME
OUTPUT_ARRAY=$(echo ${INPUT_ARRAY[@]:0:$ARR_LEN -1} $OUTPUT_FILENAME)
# replace `content` w/ `dist` in array
OUTPUT_ARRAY=$(echo ${OUTPUT_ARRAY[@]/content/dist})

# join final array into output string-path =>
OUTPUT_PATH=$(join / ${OUTPUT_ARRAY[@]})

#-- all the above code could probably be written simpler and more concise!

COMMIT_DATE=$(git log -n 1 --pretty=format:%cd --date=format:'%Y-%m-%dT%H:%M:%S' -- $INPUT)
COMMIT_HASH=$(git log -n 1 --pretty=format:%h -- $INPUT)

pandoc $INPUT --template $TEMPLATE -o $OUTPUT_PATH -V commit_date=$COMMIT_DATE -V commit_hash=$COMMIT_HASH

printf "'$1' converted to html and saved in '$OUTPUT', with commit-hash $COMMIT_HASH and commit-date $COMMIT_DATE."