var level = require('level');
var marked = require('marked');
var request = require('request');
var postsDB = level('../db/posts');
var leveldown = require('leveldown');
var updateOption = {
   valueEncoding: 'json'
};
var path = 'https://raw.githubusercontent.com/liushuping/blog/master/';
    
marked.setOptions({
    renderer: new marked.Renderer(),
    gfm: true,
    tables: true,
    breaks: false,
    pedantic: false,
    sanitize: true,
    smartLists: true,
    smartypants: false
});

function destroyDB(callback) {
    leveldown.destroy('../db/posts', function(err) {
        if (err) {
            console.log(err);
        } else {
            console.log('../db/posts is destroyed');
            (callback instanceof Function) && callback();
        }
    });
}

function updateAnPost(folder, post) {
    fetchAPost(path + folder + '/' + post.path, function(body) {
        var pattern = /^\s*#\s+(.+)$/m;
        var matches = pattern.exec(body);
        
        post.title = matches[1];
        post.slug = post.title;
        post.body = marked(body.replace(pattern, ''));
        postsDB.put(post.id, post, updateOption, function (err) {
            console.log('update post ', post.id, ' :', post.slug);
            if (err) {
                console.log(err);
            }
        });
    });
}

function fetchAPost(url, callback) {
    request(url, function(err, res, body) {
        (callback instanceof Function) && callback(body);
    });
}

request(path + 'posts.json', function(err, res, body) {
    var postConfig = JSON.parse(body);
    postConfig['2014'].forEach(function(post) {
        updateAnPost('2014', post);
    });
});

@@ -1553,24 +1553,99 @@
 arse(body);%0A
+    for (var key in postConfig) %7B%0A        if (/%5Cd%7B4%7D/.test(key)) %7B%0A        
     postConf
@@ -1647,22 +1647,19 @@
 tConfig%5B
-'2014'
+key
 %5D.forEac
@@ -1673,24 +1673,32 @@
 ion(post) %7B%0A
+        
         upda
@@ -1710,14 +1710,11 @@
 ost(
-'2014'
+key
 , po
@@ -1722,16 +1722,40 @@
 t);%0A    
-%7D);
+        %7D);%0A        %7D%0A    %7D
 %0A%7D);%0A

optimize gen.js