Skip to content

Instantly share code, notes, and snippets.

@osa1
Created August 5, 2012 20:47
Show Gist options
  • Save osa1/3267084 to your computer and use it in GitHub Desktop.
Save osa1/3267084 to your computer and use it in GitHub Desktop.
export blog posts as octopress compatible markdown files
from django.core.management.base import BaseCommand
from main.models import *
class Command(BaseCommand):
def handle(self, *args, **options):
for post in Post.objects.all():
name = post.date.strftime("%Y-%m-%d") + "-" + post.verbose_name + ".markdown"
full_date = post.date.strftime("%Y-%m-%d %H:%M")
tags = map(lambda p: p.tag.encode('UTF-8'), post.tag.all())
title = post.title.encode('UTF-8')
post_text = change_code_syntax(post.post.encode('UTF-8'))
#post_text = re.sub(lang_name, repl_lang_name, post.post.encode('UTF-8'))
with open("/home/sinan/yazilar/%s" % name, 'w') as f:
f.write("---\n")
f.write("layout: post\n")
f.write("title: \"" + title + "\"\n")
f.write("date: " + full_date + "\n")
f.write("comments: true\n")
f.write("categories: " + " ".join(tags) + "\n")
f.write("---\n\n")
f.write(post_text)
def change_code_syntax(text):
lines = text.split("\r\n")
r = []
in_code = False
for line in lines:
cl = line.strip()
if cl.startswith("::"):
in_code = True
lang = cl.split(":")[-1]
if lang == "lisp":
lang = "cl"
elif lang == "CoffeeScript":
lang = "coffeescript"
r.append("```" + lang)
elif in_code:
if line.startswith("\t"):
r.append(line[1:])
elif line.startswith(" "):
r.append(line[4:])
elif line.strip() == "":
r.append(line)
else:
r.append("```")
r.append(line)
in_code = False
else:
r.append(line)
return "\n".join(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment