More flexible Frontmater parser

I understand that ‘migration from other systems’ is on its way. But till then is it possible to parse our existing FrontMatter data into Hugo? Currently I generated the below frontmatter from another static generator.

title: My Title
tags:

  • tag1
  • tag2
  • tag3
    id: 225
    categories:
  • cat1
  • cat2
  • cat3
  • cat4
    date: 2009-02-06 07:09:00

As you can see, the data is NOT surrounded by Double Quotes and Hugo isn’t parsing them. Is there any workaround?

I spoke too soon. Hugo indeed reads the above FrontMatter with couple modifications.

  1. I had to add — on the first line (Standard for YAML)
  2. I had to remove TIME part from ‘date’ field. Is it possible to fix this one?

Sure. Pipe it through g/awk:

awk '{
  if (NR == 1) { print "---"; }
  if ($1 == "date:") {
    printf("%s %sT%s-07:00\n", $1, $2, $3);
  } else {
    print $0;
  }
}' < yourFile > newFile

This example assumes that your timezone is 7 hours behind UTC.

Awesome :slight_smile: That worked charm. I just had change this a bit to run on all files.

for file in *; do awk '{
  if (NR == 1) { print "---"; }
  if ($1 == "date:") {
    printf("%s %sT%s-05:00\n", $1, $2, $3);
  } else {
    print $0;
  }
}' "$file" > temp.md && mv temp.md $file ; done
1 Like