Skip to content

Instantly share code, notes, and snippets.

@tgpfeiffer
Created October 11, 2012 22:08
Show Gist options
  • Save tgpfeiffer/3875826 to your computer and use it in GitHub Desktop.
Save tgpfeiffer/3875826 to your computer and use it in GitHub Desktop.
Convert a Django-Zinnia blog to Ikiwiki
import java.io._
import java.util.Date
import java.text.SimpleDateFormat
import scala.io.Source
import net.liftweb.json._
import net.liftweb.util.Helpers._
object Zinnia2Ikiwiki extends Application {
// read and parse JSON dump from Django
val data = Source.fromFile("live-data.json")
val json = parse(data.mkString)
// filter to get just the relevant entries
val postsData = json.filter(_ \ "model" == JString("zinnia.entry"))
// define the structure of a blog post
case class BlogFields(
status: Int,
login_required: Boolean,
password: String,
end_publication: Date,
template: String,
comment_enabled: Boolean,
tags: String,
image: String,
title: String,
excerpt: String,
sites: List[Int],
related: List[Int],
creation_date: Date,
content: String,
featured: Boolean,
start_publication: Date,
pingback_enabled: Boolean,
authors: List[Int],
last_update: Date,
slug: String,
categories: List[Int])
case class Blog(
pk: Int,
model: String,
fields: BlogFields)
// extract post data
object MyFormats extends DefaultFormats {
object MyDateFormat extends DateFormat {
val df = new SimpleDateFormat("yyy-MM-dd HH:mm:ss")
def parse(s: String) = tryo { df.parse(s) }
def format(d: Date) = df.format(d)
}
override val dateFormat = MyDateFormat
}
implicit val formats = MyFormats
val posts = postsData.map(p => p.extract[Blog])
println(posts)
// write the contents of the blog post to directory structure
posts.filter(_.fields.status == 2).foreach(p => {
val dir = "/PATH_TO_WEBLOG/" + ("%tY/%<tm/%<td" format p.fields.creation_date)
// create folder if not exists
(new File(dir)).mkdirs
// write to file
val fname = dir + "/" + p.fields.slug + ".mdwn"
val out = new FileWriter(fname)
out.write("""[[!meta author="%s"]]
[[!meta date="%s"]]
[[!meta title="%s"]]
[[!tag %s]]
""".format("AUTHOR", p.fields.creation_date, p.fields.title, p.fields.tags.split(",").mkString))
out.write(p.fields.content)
out.close
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment