Post a Day

February 6, 2017

I would like to get better at writing. Expecially technical writing.

Therefore I am making it my goal to create a blog post a day for a month.

Since this is supposed to be a programming blog I will attempt to keep that focus.

Hopefully I can think of something to post everyday.

Onto something programming related.

Making parsers

I enjoy writing parsers. I like the ability to create my own file formats and parse them.

I beleive there is virtue in rolling your own thing and learning something new.

For example, I was working on a project that sent out emails based on events it received. Most of our email templates are text based and don't require any HTML in them. However in one instance we wanted an html template to be sent out. There already existed some format in our collection of email template files but I thought it was ugly and hard to parse. So I just rolled my own. It's a dead simple format that made it for anyone to maintain and easy for the code to parse.

It consists of a header portion describing the Subject, content type and other tags and then a body that is the HTML template. eg

Subject: Welcome
From: no-reply@mycompany.com
Content-Type: text/html
<p>Some content</p>

Stupid easy to parse, you just split the template on the --- and to get the headers split on \n and :.

function parseThatTemplate(template){
  var result = {};
  var parts = template.split("---");
  var body = parts[1];
  var header = parts[0];
  result.body = body;
  result.headers = {};
    var headerParts = line.split(":");
    var key = line[0].trim();
    var value = line[1].trim();
    result.headers[key] = value;
  return result;

Easy to allow templating via mustache or your flavor of template language in the html portion and is easy for the template editor to read.

Not a lifechaning post, but I like parsing things, so I thought I would talk about it.