forked from startup-systems/static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.sh
executable file
·37 lines (27 loc) · 982 Bytes
/
generate.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
#!/bin/bash
set -ex
# assign variables from command line args
output_dir=$2
input_path=$1
# check if directory exists (from https://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script)
if [ ! -d "$output_dir" ]; then
mkdir -p "$output_dir"
fi
if [ ! "${input_path: -1}" = "/" ]; then
input_path="$input_path/"
fi
# for each text file in input directory
for file in "$input_path"*.txt
do
file="$file"
title=$(head -n 1 "$file")
body=$(tail -n +3 "$file")
filename=$(basename "$file") # strip file name of directory and extension
filename="${filename%.*}"
#create the output file
touch "$output_dir/$filename.html"
cat template.html >> "$output_dir/$filename.html"
#now, make substitutions into file (syntax for sed taken from https://unix.stackexchange.com/questions/159367/using-sed-to-find-and-replace
sed -i -e "s~{{title}}~$title~g" "$output_dir/$filename.html"
sed -i -e "s~{{body}}~$body~g" "$output_dir/$filename.html"
done