aboutsummaryrefslogtreecommitdiff
path: root/generate
blob: 725d38e47c2c3252905f86d8461bead6b53706a7 (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
#!/bin/sh

# Put your posts in a blog_src directory
# Depends on pandoc for markdown to html convertion

blog_dir=blog

[ ! -d "$blog_dir" ] || [ -z "$(ls $blog_dir)" ] && exit 1
mkdir -p "$blog_dir"

for post_path in "$blog_dir"/*.md
do
    post_dst_path=$(echo "$post_path" | sed 's/\.md/.build.html/')

    # Convert markdown post to html
    tmp_file=$(mktemp)
    pandoc -f markdown -t html < "$post_path" > "$tmp_file"

    ejs base.ejs -i "{\"pageFilename\": \"$post_path\" }"  > "$post_dst_path"

    # Put the blog content in a wrapper template
    # sed "/<!--BLOG-->/ r $tmp_file" < blog.template.html > "$post_dst_path"

    # Add blog artcle link to the index.html
    title=$(grep '^# .*$' -m 1 "$post_path" | cut -c 2-)
    [ -z "$title" ] && echo "post $post_path doesn't have a title" && title=$post_path

    sed -i'' "/<!--BLOGINDEX-->/ a\
        <li><a href=\"$post_dst_path\">$title</a></li>" index.html

    echo "Generated post at $post_dst_path"
done

for util_path in utils/*
do
    title=$(basename "$util_path" | tr '_' ' ')

    ejs base.ejs -i "{\"pageFilename\": \"$post_path\" }"  > "$post_dst_path"

    sed -i'' "/<!--UTILSINDEX-->/ a\
        <li><a href=\"$util_path\">$title</a></li>" index.html
done