summaryrefslogtreecommitdiff
path: root/publish-blog.sh
blob: c5a483f534f380d2b2a510f1b2fcd277a8350459 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
set -ex

if [ $# -gt 2 ]; then
  echo "./publish-blog.sh [isodate] mds/blog/blog-file.md"
  exit 1
fi

if [ $# -lt 1 ]; then
  echo "./publish-blog.sh [isodate] mds/blog/blog-file.md"
  exit 1
fi

# Setup Dates

isodate=$(date -I)
date=$(date +"%B %d, %Y")
if [ $# -eq 2 ]; then
  isodate=$1
  date=$(date --date="${isodate}" +"%B %d, %Y")
  in=$2
else
  in=$1
fi

# Get Page Title

title=$(head -n1 $in | sed -E 's/^##[[:space:]]*(.+)[[:space:]]*$/\1/g')

# File info
path=www/blog/${isodate}
out=${path}/index.html

# Make path

mkdir -p ${path}

# Compile md

./compile-md.sh blog "${title}" $in $out

# Update date

sed -E -i "s/BLOG_DATE/${date}/" $out

# Update index (if needed)

index=mds/blog/blog-index.md

## check if exists
exists=0
grep -q -c "$isodate - $title" $index || exists=$?

# Doesn't exist
if [ $exists -eq 1 ]; then
  marker="<!--NEXT-->"
  line="1. [${isodate} - ${title}](/blog/${isodate}/)"
  sed -E -i "/$marker/a $line" $index
  ./compile-md.sh page "Blog Index" $index www/blog/index.html
else
  echo "Already exists in index - not updating"
fi

echo "Done"