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

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

cp -vf index.template.html index.html
rm -vf blog/*

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

for article_path in blog_src/*.md
do
    article_dst_path=$(echo "$article_path" | sed 's/blog_src/blog/' | sed 's/\.md/.html/')

    # Convert markdown article to html
    tmp_file=$(mktemp)
    pandoc -f markdown -t html < "$article_path" > "$tmp_file"
    # Put the blog content in a wrapper template
    sed "/<!--BLOG-->/ r $tmp_file" < blog.template.html > "$article_dst_path"

    # Add blog artcle link to the index.html
    title=$(grep '^# .*$' -m 1 "$article_path" | cut -c 2-)
    [ -z "$title" ] && echo "Article $article_path doesn't have a title" && title=$article_path
    sed -i'' "/<!--BLOGINDEX-->/ a\
        <li><a href=\"$article_dst_path\">$title</a></li>" index.html

    echo "Generated article at $article_dst_path"
done

for util_path in utils/*
do
    title=$(basename "$util_path" | tr '_' ' ')
    sed -i'' "/<!--UTILSINDEX-->/ a\
        <li><a href=\"$util_path\">$title</a></li>" index.html
done