Skip to content

Instantly share code, notes, and snippets.

@noqqe
Last active February 24, 2017 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noqqe/f2981daf33221332b4462f93b5df4772 to your computer and use it in GitHub Desktop.
Save noqqe/f2981daf33221332b4462f93b5df4772 to your computer and use it in GitHub Desktop.
Restructuring the metadata of my blog with frontmatter
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import frontmatter
import glob
import codecs
home = '/home/noqqe/Code/noqqe.de/content/blog/'
files = glob.glob(home + '*.md')
for f in files:
print f
with open(f, "r") as o:
post = frontmatter.loads(o.read())
tags = post["tags"]
tags = [ unicode(v).lower() for v in tags ]
try:
tags = list(set(tags))
except:
continue
tags = [ v for v in tags if not unicode(v).lower().startswith('ubuntuusers') ]
tags = [ v for v in tags if not unicode(v).lower().startswith('planetenblogger') ]
tags = [ v for v in tags if not unicode(v).lower().startswith('osbn') ]
post["tags"] = tags
# correct date
post["date"] = post["date"].strftime("%Y-%m-%dT%H:%M:%S")
# remove comments flag
try:
del post["comments"]
except KeyError:
pass
with codecs.open(f, "wb+", 'utf8') as o:
o.write(frontmatter.dumps(post))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment