Tao Te Ching in fortune

2019年11月19日

Recently I find this nice website that has various English translations of Tao Te Ching online, and I think, “why not make these chapters MOTD in my shell?”, and it turns out very easy.

I want to use fortune to display a random chapter each time when I start a new terminal window, and to do this, I need to gather of the chapters into a fortune text format, which uses a % in between of quotes.

for i in {1..81}; do
  echo $i
  curl -s "http://www.taoteching.org.uk/index.php?c=$i&a=Stephen+Mitchell" | ruby -r nokogiri -e 'puts Nokogiri.parse(ARGF).at_css("#right")' |html2text >> tao_te_ching.txt
  echo '%' >> tao_te_ching.txt
done

Here apparently I have to install ruby Nokogiri gem as well as python html2text package.

Now I can generate an index for the fortune file

strfile tao_te_ching.txt

To use the new fortune in my shell (zsh), I put the fortune files in ~/doc/book

width=$(($COLUMNS - 2)); fortune ~/doc/book | sed -e :a -e "s/^.\{1,${width}\}$/ & /;ta"

The result looks like this

taoteching_motd