Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@NdYAG
Created July 23, 2014 04:19
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 NdYAG/ecf73ff96cf125f0d72d to your computer and use it in GitHub Desktop.
Save NdYAG/ecf73ff96cf125f0d72d to your computer and use it in GitHub Desktop.
Kobito.app Exporter
var util = require('util')
, sqlite3 = require('sqlite3').verbose()
, fs = require('fs')
, mkdirp = require('mkdirp')
var Kobito = function(path) {
this.db = new sqlite3.Database(path)
}
Kobito.prototype = {
execAll: function(sql, callback) {
if (typeof callback !== "function") return
this.db.serialize((function() {
this.db.all(sql, function(err, rows) {
if (err) { return console.log(err) }
callback.apply(this, Array.prototype.slice.call(arguments, 0))
})
}).bind(this))
},
fetchAllArticles: function(callback) {
this.execAll("SELECT Z_PK AS id, ZTITLE AS title, ZRAW_BODY AS raw_body FROM ZITEM", callback)
},
fetchOneArticle: function(id, callback) {
this.execAll("SELECT ZTITLE AS title, ZRAW_BODY AS raw_body FROM ZITEM WHERE Z_PK = " + id, callback)
},
fetchTagsByArticle: function(id, callback) {
this.execAll("SELECT Z_1TAGS.Z_2TAGS AS id, ZTAG.ZNAME AS name FROM Z_1TAGS INNER JOIN ZTAG ON Z_1TAGS.Z_2TAGS = ZTAG.Z_PK WHERE Z_1TAGS.Z_1ITEMS = " + id, callback)
},
fetchAllTags: function(callback) {
this.execAll("SELECT Z_PK AS id FROM ZTAG", callback)
},
fetchOneTag: function(id, callback) {
this.execAll("SELECT Z_PK AS id, ZNAME AS name FROM ZTAG WHERE Z_PK = " + id, callback)
},
fetchArticlesByTag: function(id, callback) {
this.execAll("SELECT Z_1TAGS.Z_1ITEMS AS id, ZITEM.ZTITLE AS title FROM Z_1TAGS INNER JOIN ZITEM ON Z_1TAGS.Z_1ITEMS = ZITEM.Z_PK WHERE Z_1TAGS.Z_2TAGS = " + id, callback)
},
fetchArticle: function(id, callback) {
var self = this
this.fetchOneArticle(id, function(err, article) {
self.fetchTagsByArticle(id, function(err, tags) {
article[0].tags = (function() {
var ret = []
tags.forEach(function(tag) {
ret.push(tag.name)
})
return ret
})()
article[0].id = id
callback(err, article[0])
})
})
},
fetchTag: function(id, callback) {
var self = this
this.fetchOneTag(id, function(err, tag) {
self.fetchArticlesByTag(id, function(err, articles) {
tag[0].articles = articles
callback(err, tag)
})
})
}
}
var kobito = new Kobito( util.format('%s/Library/Kobito/Kobito.db', process.env.HOME) )
kobito.fetchAllArticles(function(err, articles) {
mkdirp.sync('src')
articles.forEach(function(article) {
kobito.fetchArticle(article.id, function(err, article) {
// export to yaml for metalsmith/jekyll
var content = '---\n' +
'title: ' + article.title.replace(':', '') + '\n' +
'tags: ' + article.tags.join(',') + '\n' +
'template: index.html\n' +
'---\n\n' +
article.raw_body
fs.writeFile('src/' + article.id + '.md', content, function(err) {
if (err) { return console.log(err) }
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment