Blog IndexPosts by TagHome

Org-Mode Tag Index (for blogging!)

Posted <2015-09-08 Tue 02:26> by Aaron S. Jackson.

Well I decided to create a very disgusting bash script to create the pages listing the tags. It actually works quite well, but solution is not ideal. Ideally I would have created it in Elisp, but I am not a great lisp programmer and I can hack things together much more quickly in bash.

Here it is…

#!/bin/bash

mkdir -p ~/public_html/blog/tags
rm -f ~/public_html/blog/tags/*

cd ~/public_html/blog/org

listing=`grep ^\#\+FILETAGS\: *.org | sort -r` # get a files and their tags

# get a list of all the posts
posts=`echo "$listing" | awk '{ print $1 }'`
posts=`echo "$posts" | rev | cut -c 13- | rev`

# get the tags for each post
posts_tags=`echo "$listing" | awk '{ print $2 }'`

# create a list of all tags
all_tags=`echo "$posts_tags" | tr ":" "\n" | grep -v '^$'`
all_tags=`echo "$all_tags" | sort | uniq | sort`

echo "#+TITLE: Tags" > ~/public_html/blog/tags/index.org

# create a tag file for each tag
while read -r tag; do
    count=`grep ^\#\+FILETAGS\:.*\:$tag\: *.org | wc -l`
    echo "$tag appeared $count times"

    echo "#+TITLE: Tag: $tag" > ~/public_html/blog/tags/$tag.org
    echo "- [[./$tag.html][$tag]] ($count)" >> ~/public_html/blog/tags/index.org
done <<< "$all_tags"


# populate the tag files
while read -r file; do
    # get the file tags and pop it off from the "stack"
    tags=`echo "$posts_tags" | head -n1 | tr ':' '\n' | grep -v '^$'`
    posts_tags=`echo "$posts_tags" | tail -n +2`

    # get the post title and date
    title=`cat $file | grep ^\#\+TITLE\: | cut -c 10-`
    date=`cat $file | grep ^\#\+DATE\: | cut -c 10- | rev | cut -c 2- | rev`
    date=`date -d"$date" +"%d %B %Y %H:%M"`

    # for each tag, add this file to that tag file...
    while read -r tag; do
    echo "- [[../$file][$date - $title]]" >> ~/public_html/blog/tags/$tag.org
    done <<< "$tags"
done <<< "$posts"

Wanting to leave a comment?

Comments and feedback are welcome by email (aaron@nospam-aaronsplace.co.uk).

Tags: emacs hacks

Blog IndexPosts by TagHome

Copyright 2007-2024 Aaron S. Jackson (compiled: Mon 30 Sep 12:34:18 BST 2024)