pyratelog

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

pyratelog.sh (2675B)


      1 #!/bin/bash
      2 
      3 WEBDIR="/var/www/html"
      4 
      5 cd ${WEBDIR}
      6 
      7 # generate rss.xml file
      8 if [ -f rss.xml ] ; then
      9 	if [[ $(head -n 1 rss.xml) == "<?xml version='1.0' encoding='UTF-8'?>" ]] ; then
     10 		tail -n+7 rss.xml > rss.xml.bak
     11 		mv rss.xml.bak rss.xml
     12 	fi
     13 else
     14 	echo -e '</channel>\n</rss>' > rss.xml
     15 fi
     16 
     17 # find all markdown files in 'entry' directory
     18 find_md=$(find entry/ -type f -name "*.md" | sort)
     19 
     20 # for each markdown file
     21 for md in ${find_md} ; do
     22 
     23 	# get the title and date from the filename
     24 	input=$(echo ${md} | cut -f2 -d '/' | rev | cut -f2- -d '.' | rev)
     25 
     26 	# cut the date and turn into epoch time
     27 	input_date=$(echo ${input} | cut -f1 -d '-' )
     28 	if ls -l /bin/date | grep busybox >/dev/null ; then
     29 		# busybox `date` works differently
     30 		index_date=$(date -d ${input_date} -D "%Y%m%d" +%s)
     31 		rss_pubdate="<pubDate>$(date -d ${input_date} -D '%a, %d %b %Y 00:00:00' +%s)</pubDate>"
     32 	else
     33 		index_date=$(date -d ${input_date} +%s)
     34 		rss_pubdate="<pubDate>$(date -d ${input_date} +'%a, %d %b %Y 00:00:00')</pubDate>"
     35 	fi
     36 
     37 	# cut the title and replace underscores with spaces for menu
     38 	input_title=$(echo ${input} | cut -f2- -d '-' | sed 's/_/\ /g')
     39 	index_title=$(echo ${input_title})
     40 
     41 	# create menu link for index page
     42 	input_link="<li><a class='index' href='entry/${input}.html'><b>${index_date}</b> ${index_title}</a></li>"
     43 
     44 	# if link already exists we can skip
     45 	grep ${input} index.html >/dev/null && continue
     46 
     47 	# for new files we turn the markdown into html
     48 	# using a template file
     49 	pandoc -s \
     50 		--template=./post_template.html \
     51 		--metadata title="${input_title}" \
     52 		-f markdown \
     53 		-t html \
     54 		-o entry/${input}.html \
     55 		entry/${input}.md
     56 	
     57 	# add the link to the top of the menu
     58 	sed "/<ul class='index'>/a ${input_link}" index.html > index.html.bak
     59 
     60 	# replace old index page
     61 	cat index.html.bak > index.html
     62 
     63 	# tidy up
     64 	rm -f index.html.bak
     65 
     66 	# create rss item
     67 	rss_title="<title>${index_title}</title>"
     68 	rss_link="<link>https://log.pyratebeard.net/entry/${input}.html</link>"
     69 	sed "s/</\&lt\;/g; s/>/\&gt\;/g" entry/${input}.html > entry/${input}.html.rss
     70 	rss_description=$(sed -n '/body/,/\/body/p' entry/${input}.html.rss)
     71 
     72 	echo -e "\t<item>\n\t\t${rss_title}\n\t\t${rss_pubdate}\n\t\t${rss_link}\n\t\t<description>${rss_description}</description>\n\t</item>\n$(cat rss.xml)" > rss.xml
     73 
     74 	rm -f entry/${input}.html.rss
     75 done
     76 
     77 if [[ $(head -n 1 rss.xml) != "<?xml version='1.0' encoding='UTF-8'?>" ]] ; then
     78 	echo -e "<?xml version='1.0' encoding='UTF-8'?>\n<rss version='2.0'>\n<channel>\n\t<title>pyratelog</title>\n\t<link>https://log.pyratebeard.net</link>\n\t<description>another personal blog by another geek</description>\n$(cat rss.xml)" > rss.xml
     79 fi