pyratelog.sh (3018B)
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=./entry_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 # get list of tags 67 tag_list=$(awk '/^keywords: /{print substr($0,index($0,$2))}' ${md} | tr -d '[],') 68 for tag in ${tag_list} ; do 69 sed "/<ul class='index'>/a ${input_link}" tags/${tag}.html > tags/${tag}.html.bak 70 71 # replace old index page 72 cat tags/${tag}.html.bak > tags/${tag}.html 73 74 # tidy up 75 rm -f tags/${tag}.html.bak 76 done 77 78 # create rss item 79 rss_title="<title>${index_title}</title>" 80 rss_link="<link>https://log.pyratebeard.net/entry/${input}.html</link>" 81 sed "s/</\<\;/g; s/>/\>\;/g" entry/${input}.html > entry/${input}.html.rss 82 rss_description=$(sed -n '/body/,/\/body/p' entry/${input}.html.rss) 83 84 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 85 86 rm -f entry/${input}.html.rss 87 done 88 89 if [[ $(head -n 1 rss.xml) != "<?xml version='1.0' encoding='UTF-8'?>" ]] ; then 90 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 91 fi