Publishing the recent articles on the homepage

Posted by pulkomandy on Sat Jul 28 18:37:35 2012  •  Comments (0)  • 

I decided to show the recent articles from all the categories on the hormapge. To do that, a small script in the /etc/cron.daily folder is enough.

This will delete all the symlinks in the main category, then recreate links to most recent articles in all categories.

The script will also create links to the comment files, so the comments will also be forwarded to the homepage.

Since there is no database, the website is static. So there is no real way to do queries. This is not really important, because :

  • You usually know when you publish a new article, so you can run the script by hand at that time,
  • You can also put the script in a cron job so that it works automagically. The only drawback is it will take up to 24 hours for your articles to go on the homepage (I do run the script daily).

I split the script in two files, because my sh skills are limited. It should be possible to do it in one file. If you do it, tell me so.

Part one, script to actually run (as a cron job for me, put it in /etc/cron.dayly)

cd /path/to/pulkocms/cat/
find -maxdepth 1 -type l -delete -name "*.{article,comment}"
find . -wholename "*/*.article" -type f | xargs -d\\n ls -t1 | head | xargs -d\\n -L1 /var/www/pulkocms/dolink ;

And here is the dolink script that creates links.

ln -s "$1" .
ln -s "`echo $1 | sed -e 's@\(.*\)article@\1comment@' | sed -e 's@ @%20@g'`" .

Some explanations. The first script will first delete all the symlinked articles on your home page/main category. It goes to depth one so you can have symlinks in other categories if you want (for multi-categories articles). It also delete the linked comment files.

Then, it looks in all categories for all article files. It pipes them to ls -t which sorts them by date. Head takes the 10 newest files and and send them (one at a time) to the dolink script.

dolink then just create a link to the found article in the main folder. It also link the comment file (which may not even exist yet). There's a subtle trick, the comment-file has its name url-escaped, so we have to fake that with sed. I chose to only replace spaces, but you can do more if needed.

Anyway, tell me if you improve it in anyway, or if you otherwise use and enjoy PulkoCMS!

Leave a comment

Name: Mail: