How to convert your CoffeeScript project to Literate style in about 30 minutes

Posted on

CoffeeScript 1.5 brings with it the ability to use Literate style programming. This is an incredibly human way to treat code. Not everybody will like it, but those who do will be happy to know that it’s relatively simple to convert your existing project to use the literate style in 30 minutes or less. Here’s how I did it (in order). BTW , make sure to read through to the end where I outline my assumptions.

Replace existing code blocks with blockquoted code blocks

find . -type f \( -name "*.coffee" \) | xargs sed -i 's/#\s\s\s\s\s/# >    /g'

Add four spaces to the beginning of every coffee file

find . -type f \( -name "*.coffee" \) | xargs sed -i 's/^/    /g'

Remove preceeding spaces and # symbols from single line comments

find . -type f \( -name "*.coffee" \) | xargs sed -i 's/^\s*#\s//g'

Replace lines that are only made up of # symbols with an empty line

find . -type f \( -name "*.coffee" \) | xargs sed -i 's/^[ \t]*#\{1,3\}//g'

Replace lines that are ony made up of spaces with an empty line

find . -type f \( -name "*.coffee" \) | xargs sed -i 's/^\s*$//g'

Rename all coffee files to litcoffee

find . -type f \( -name "*.coffee" \) -exec rename -v 's/\.coffee$/.litcoffee/' {} \;

Compile litcoffee files

coffee --compile ./

Bonus: SVN Remove all coffee files

svn st | grep ^! | awk '{print $2}' | xargs svn delete

My assumptions are that you do not use any block comments (###) and that you do not have any lines that comment out code e.g. # foo = ‘bar’. While these will just get turned into regular text it’s probably a good idea to quickly look through your litcoffee files to make sure that things aren’t messed up. This is especially true if you have block commented out code. In that case you will probably end up with blocks of code that will actually get parsed and break stuff.