pyratelog

personal blog
git clone git://git.pyratebeard.net/pyratelog.git
Log | Files | Refs | README

pyratelog.sh (1558B)


      1 #!/bin/bash
      2 
      3 function initialise() {
      4 	# find all markdown files in 'entry' directory
      5 	find_md=$(find entry/ -type f -name "*.md" | sort)
      6 
      7 	# for each markdown file
      8 	for md in ${find_md} ; do
      9 
     10 		# get the title and date from the filename
     11 		input=$(echo ${md} | cut -f2 -d '/' | cut -f1 -d '.')
     12 
     13 		# cut the date and turn into epoch time
     14 		input_date=$(echo ${input} | cut -f1 -d '-' )
     15 		ls -l /bin/date | grep busybox >/dev/null && \
     16 			# busybox `date` works differently
     17 			index_date=$(date -d ${input_date} -D "%Y%m%d" +%s) || \
     18 			index_date=$(date -d ${input_date} +%s)
     19 
     20 		# cut the title and replace underscores with spaces for menu
     21 		input_title=$(echo ${input} | cut -f2 -d '-' | sed 's/_/\ /g')
     22 		index_title=$(echo ${input_title})
     23 
     24 		# create menu link for index page
     25 		input_link="<li><a class='index' href='entry/${input}.html'><b>${index_date}</b> ${index_title}</a></li>"
     26 
     27 		# if link already exists we can stop
     28 		grep ${input} index.html >/dev/null && continue
     29 
     30 		# for new files we turn the markdown into html
     31 		# using a template file
     32 		pandoc -s \
     33 			--template=./post_template.html \
     34 			--metadata title="${input_title}" \
     35 			-f markdown \
     36 			-t html \
     37 			-o entry/${input}.html \
     38 			entry/${input}.md
     39 		
     40 		# add the link to the top of the menu
     41 		sed "/<ul class='index'>/a ${input_link}" index.html > index.html.bak
     42 
     43 		# replace old index page
     44 		cat index.html.bak >index.html
     45 
     46 		# tidy up
     47 		rm -f index.html.bak
     48 	done
     49 }
     50 
     51 # watch the entry directory for new files
     52 while : ; do
     53 	ls ./entry/*.md >/dev/null 2>&1 && initialise
     54 	sleep 30
     55 done