From 3ebcd66cd17741ec7dc7793f9f0121e2ee025fdf Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Tue, 24 Nov 2020 12:10:40 +0100 Subject: Fixing templating (without ejs) --- generate | 70 +++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 20 deletions(-) (limited to 'generate') diff --git a/generate b/generate index 725d38e..d13dac6 100755 --- a/generate +++ b/generate @@ -1,42 +1,72 @@ #!/bin/sh -# Put your posts in a blog_src directory -# Depends on pandoc for markdown to html convertion +base_template () { + body_file=$(mktemp) + cat > "$body_file" + sed '//r '"$body_file" base.template.html +} -blog_dir=blog +base_template_link () { + link_href="$1" + case $link_href in + *.css) + link_rel="stylesheet" + link_type="text/css" + ;; + *.png) + link_rel="icon" + link_type="image/png" + ;; + *) + echo Invalid link file + exit 1 + ;; + esac + sed "//i\ + " +} -[ ! -d "$blog_dir" ] || [ -z "$(ls $blog_dir)" ] && exit 1 -mkdir -p "$blog_dir" +# Setup +blog_path=blog +mkdir -p "$blog_path" +base_template < index.template.html | + base_template_link 'favicon.png' | + base_template_link 'style.css' > index.html +base_template < school.template.html | + base_template_link 'favicon.png' | + base_template_link 'style.css' > school.html -for post_path in "$blog_dir"/*.md +# Generate blog posts +for post_path in "$(find "$blog_path" -name '*.md')" do - post_dst_path=$(echo "$post_path" | sed 's/\.md/.build.html/') + post_dst_path=$(echo "$post_path" | sed 's_.md_.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 "// r $tmp_file" < blog.template.html > "$post_dst_path" + pandoc -f markdown -t html < "$post_path" | + base_template | + base_template_link '../favicon.png' | + base_template_link '../style.css' > "$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'' "// a\ + sed -i "// a\
  • $title
  • " index.html echo "Generated post at $post_dst_path" done +# Generate utilities for util_path in utils/* do title=$(basename "$util_path" | tr '_' ' ') - - ejs base.ejs -i "{\"pageFilename\": \"$post_path\" }" > "$post_dst_path" - - sed -i'' "// a\ + sed '$ a ' < "$util_path/index.template.html" | + base_template | + base_template_link '../../favicon.png' | + base_template_link '../../style.css' | + base_template_link 'style.css' > "$util_path/index.html" + sed -i'' "// a\
  • $title
  • " index.html + + echo "Generated utility at $util_path" done -- cgit