HTML Journal Emacs Mode

In order to get better at elisp, I was looking for a simple emacs project to work on. Emacs is great to read and write text, so I thought it'd be fun to create a way to read HTML Journal directly from it. It turns out, it's not that hard to do! Here's how I did it.

To read an HTML Journal from within emacs, we need to do a few things:

Let's break it down.

Get the HTML of the Journal

It turns out that emacs ships with a very useful package called url. It lets us make HTTP requests, but not only. In this case, here's how we can fetch the HTML of a journal:

(url-retrieve-synchronously "https://my-journal.com" nil t)

This sends a GET request to the passed URL and writes the response to a new buffer. That's convenient for us: all we need to do now is parse this buffer.

Parse the HTML

Speaking of convenience, emacs ships with an XML parser too! Well, it does if it's been compiled with it, but chances are that it has. This parser only needs to know the region in the buffer to parse, and returns a dom tree. Here's how it works:

(libxml-parse-html-region (point-max) (point-min))

Now that we have a parsed dom tree, we can use it to display the journal.

Display it

I'm sure there are much better ways to do it than how I'm doing it so far, but the idea is the following:

  1. Create a new buffer
  2. Iterate on each article of the dom tree
  3. Write the entry on the buffer
  4. Switch to the buffer

Conclusion

It's relatively straightforward to create modes on emacs, and it's fun to do. The editor is loaded with useful packages that we can use and compose with. If you want to use this mode, or just to look at it, check my emacs config, under "/lisp".