-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·67 lines (54 loc) · 1.83 KB
/
build.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
#!/bin/bash
# generate HTML from markdown,
# then replace all lines between START_CONTENT and END_CONTENT with this HTML
# in the file INDEX_FILE
# requires marked https://github.com/markedjs/marked
# npm install -g marked
# usage:
# ./build.sh content.md
# check marked is installed
if ! type "marked" 2> /dev/null > /dev/null; then
echo "marked not installed. https://www.npmjs.com/package/marked. please run"
echo "npm install -g marked"
exit 1
fi
if [ -z $1 ]; then
CONTENT_FILE="${PWD##*/}.md"
echo "transforming $CONTENT_FILE"
else
CONTENT_FILE="$1"
fi
if [ ! -f "${CONTENT_FILE}" ]; then
echo "${CONTENT_FILE} does not exist"
exit 1
fi
INDEX_FILE="index.html"
TEMP_FILE="index.html.temp"
START_CONTENT="<!-- !!start content!! -->"
END_CONTENT="<!-- !!end content!! -->"
original_html=$(cat $INDEX_FILE)
html=$(marked -i "$CONTENT_FILE")
# word count
num_words=$(cat "${CONTENT_FILE}" | wc -w)
read_speed=200
read_time=$(($num_words / $read_speed))
has_template=$(echo "${original_html}" | grep '{{.*}}')
if [ ! -z "${has_template}" ]; then
echo "{{template}}s found in file. They should be removed soon..."
fi
# get line numbers of START_CONTENT and END_CONTENT
start=$(echo "${original_html}" | grep -n "${START_CONTENT}" | cut -d : -f1)
end=$(echo "${original_html}" | grep -n "${END_CONTENT}" | cut -d : -f1)
if [ -z $start ] || [ -z $end ]; then
echo "could not find start/end content tags"
echo "start: <$start>, end: <$end>"
exit 1
fi
rm -f $TEMP_FILE
echo "${original_html}" | awk 'NR <= '"${start}"'' >> $TEMP_FILE
echo "<span class="words">${num_words} words, ${read_time} mins @ ${read_speed} <abbr title='words per minute'>wpm</abbr></span>" >> $TEMP_FILE
echo "${html}" >> $TEMP_FILE
echo "${original_html}" | awk 'NR >= '"${end}"'' >> $TEMP_FILE
cat $TEMP_FILE > $INDEX_FILE
rm -f $TEMP_FILE
echo "done! 🚀"