samedi 14 février 2015

Livereload FTW

If you deal with HTML on a daily basis, you should really use a livereload software. What do theses tools do?

  • watching a set of files,
  • send notification to web browser to trigger page reload. To do so, an extension is used.

I use an implementation of livereload in Python. Install it with pip:

pip install livereload

It comes with livereload command that serve port 35729 and watch everything in current working directory:

romain:~/Documents/sites/blogs/romCodeCorner なに? livereload
Serving on
[I 150214 21:50:02 handlers:99] Browser Connected: file:///Users/romain/Documents/sites/blogs/romCodeCorner/cqrs.htm
[I 150214 21:50:02 handlers:104] Watch current working directory
[I 150214 21:50:02 handlers:108] Start watching changes
[I 150214 21:50:03 handlers:49] File /Users/romain/Documents/sites/blogs/romCodeCorner/ changed

Notice that on Windows, you should run it through python with the absolute path. For instance:

python C:\Python34\scripts\livereload

Then open a watched file in your browser and activate the extension. Modify a watched file and enjoy auto refresh.

There is more of course as you can read on PyPI page. You can trigger a command when a watched file is modified. I wrote following script to watch markdown files, transform them into HTML and reload the page.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from livereload import Server

# create a server
server = Server()
# run make everytime a markdown file is modified'*.md', 'make')
# serve on livereload extension default listening port.

It's neat.

I'm a huge fan of GNU make… The Makefile I use to compile markdown files is:

all: $(shell ls *.md | sed 's/\.md/.htm/')

        markdown_py -e utf-8 -f $@ $^

        rm *.htm

Of course, I use Python implementation of markdown ;-)