How I migrated to a static blog


Until one week ago my blog was hosted at my house, on a raspberrypi with debian + wordpress. I was not satisfied by my setup because given the minimal size of my blog and the really scarce content I post every now and then, a full LLMP stack was overblown. I decided to change distribution (my server now runs Void) and to migrate to a static blog without CMS nor PHP.

Welcome to Jekyll and Hyde

The process of migration was rather painless. First of all I installed ruby on my desktop computer, then via ruby gems I installed jekyll:

gem install jekyll

gem install jekyll-gist

gem install jekyll-paginate

I used a wordpress plugin to copy all my old posts. Then I cloned from git the Hyde theme which you can see a demo here and corrected a pair of warning that jekyll printed on my terminal. Actually the jekyll docs are quite complete and covered all the errors that I encountered.

Jekyll structure is quite simple to understand: in the folder _post/ there are your post in markdown format (remember to delete the examples in that folder); in the root the are some files that should be modified: the about.md file, the 404 page and index.html that is the frontpage of the blog; finally _config.yml contains the general configuration for the website and should be adjusted to your own likings. When Jekyll builds a website it parses all the markdown files and stores them in _site folder. Jekyll uses the html files in _layouts and _includes to render the markdown files.A

I added a simple archive page following the little piece of code in this page {% raw %} --- layout: page title: Archive ---

## Blog Posts

{% for post in site.posts %}
  * {{ post.date | date_to_string }} » [ {{ post.title }} ]({{ post.url }})
{% endfor %}:wq

{% endraw %} I noticed that in _includes/head.html there is this line:

<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800,600' rel='stylesheet' type='text/css'

so I proceed to remove it because is not needed for my blog. Finally I put a link to the archive, my github page and the atom feed on the sidebar by simple adding a href on _includes/sidebar.html.

I did not proceed with further modifications but there are tons of possibilities with jekyll. I think that the main advantages are the fact that you don't have to manage html code when writing a new post and that everything can be done via cli.

Francesco Mecca




The Buridan's donkey in python


During the final weeks of my exam session I started reading a bit about python 3 using an excellent book: Dive into Python. When I noted that python uses the Mersenne Twister PRNG as well I decided to write another version of my Buridan's donkey program.

.. code:: python

    import random, sys

    if __name__ == '__main__':
        args = list()
        if not sys.stdin.isatty():
            for line in sys.stdin:
                if line[-1] is '\n':
                    line = line[:-1]
                args.append(line)
        else:
            args = sys.argv[1:]
        argRange = len(args)
        for i in range(argRange):
            print(str(i+1) + '.', args.pop(random.randrange(0, len(args))))

This script works in a different way than the one in c++. Rather than shuffling a list made by the entries in the arguments, it pops randomly one entry from the list till the list is empty.

Not satisfied enough, I wrote also a telegram bot using the telebot library that works as the script above but inside the telegram app. The bot can be added to your contact list by simply searching for @duridan_donkey_bot (yes, a typo!)

All the code is opensource and can be found on my github page.

Francesco Mecca




The Buridan&#8217;s donkey paradox


The Buridan’s donkey is an illustration of a paradox regarding the philosophy of moral determinism and free will.

The paradox shows an hypothetical situation in which a donkey searching for food is caught in the middle of two equally appealing stacks of hay located at the same distance from the donkey. Because the donkey has no real reason to choose one over the other he dies of starvation.

Deliberations_of_CongressI have decided to write a cli program that chooses for me when I can’t make up my mind.

The program is written in C++ and when invoked along with two or more arguments it puts them in a vector and then changes the order randomly.

.. code:: c

#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <string>
#include <sys/poll.h>
using namespace std;

struct pollfd stdin_poll = {
    .fd = fileno (stdin), .events = POLLIN
};

void read_from_piped_input (vector<string>& lst)
{
    string x;
    while (getline (cin, x)) {
        lst.push_back (x);
    }
}

void read_from_arguments (const int& argc, char* argv[], vector<string>& lst)
{
    if (argc  == 1) {
        cout << "Usage: asino [string] [string] ..." << endl;
        exit;
    }
    for (vector<string>::size_type i = 1; i < argc; ++i) {
        lst.push_back (argv[i]);
    }
}

int main (int argc, char* argv[])
{
    vector<string> lst;
    int poll_ret = poll (&stdin_poll, 1, 0);
    if (poll_ret > 0) {
        read_from_piped_input (lst);
    }
    else {
    read_from_arguments (argc, argv, lst);
    }

    random_device rd;
    mt19937 m(rd());
    shuffle (lst.begin (), lst.end (), m);

    int i = 1;
    for (vector<string>::iterator it = lst.begin (); it != lst.end (); ++it) {
        cout << i++ << ". " << *it << endl;
    }
}

I have used the Mersenne Twister PRNG just to give it a try.

One of the challenges was to read from stdin instead of arguments when the program is piped after another program in the shell:

.. code:: sh

ls /media/my_movies/ | buridan

So I have used poll() that checks for a specified amount of time if the selected device (/dev/stdin in my case) can perform I/O operations; in my code:

.. code:: bash

poll (&stdin_poll, 1, 0)

I selected the POLLIN as event so poll() only checks if there is data to read, 1 as the number of items in the fds array, 0 milliseconds of timeout because when the program is invoked /dev/stdin may already contain input.

The program should be compiled this way:

.. code:: bash

g++ -std=c++11 ./program.cpp -o output

You are free to reuse this little piece of code as you wish.

EDIT: 02-04-2016 The original idea for the Buridan's donkey came from my mentor Simone Basso who wrote the original one in haskell.

Francesco Mecca