Skip to content

Instantly share code, notes, and snippets.

@sideb0ard
Created December 1, 2013 04:47
Show Gist options
  • Save sideb0ard/7728607 to your computer and use it in GitHub Desktop.
Save sideb0ard/7728607 to your computer and use it in GitHub Desktop.
simple script for converting a tsv text dump from a wordpress export to markdown files for use in a static site generator. (Hugo in this case)
#!/usr/bin/env python
from __future__ import print_function
import csv
import datetime
import re
def qtr(word):
word = re.sub('^|$','"', word)
return word
with open('theb0ardside.txt','rb') as tsvin:
tsvin = csv.reader(tsvin, delimiter='\t')
for row in tsvin:
title, date, tags, content = row[1], row[2], row[4], row[8]
slug = title.replace(' ','-').lower()
slug = ''.join(e for e in slug if e.isalnum())
dateObject = date.split()
day, month, year = dateObject[1], dateObject[0], dateObject[2]
day = day.replace(',','')
tempnewdate = "{} {} {}".format(day, month, year)
date = str(datetime.datetime.strptime(tempnewdate, "%d %B %Y")).replace(' 00:00:00','')
taglist = [qtr(t.strip()) for t in tags.split(',')]
quotedTagListString = ",".join(taglist)
f = open('content/' + slug + '.md', 'wb')
print("---", file=f)
print("Title: " + qtr(title), file=f)
print("Description: " + qtr(title), file=f)
print("Tags: [" + quotedTagListString + "]", file=f)
print("Date: " + qtr(date), file=f)
print("Categories:", file=f)
print(" - \"blog\"", file=f)
print("Slug: " + qtr(slug),file=f)
print("---", file=f)
print(content, file=f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment