Skip to content

Instantly share code, notes, and snippets.

@morris821028
Created August 19, 2015 02:40
Show Gist options
  • Save morris821028/e47ad8a6d15e3db204d5 to your computer and use it in GitHub Desktop.
Save morris821028/e47ad8a6d15e3db204d5 to your computer and use it in GitHub Desktop.
hexo 2.8.3 to 3.1.1 tag plugin migrate
/*
$ npm install mkdirp --save
$ node hexo-migrate.js _post/
$ cd file_migrate/_post
*/
var fs = require("fs");
var path = require("path");
var mkdirp = require("mkdirp");
function file_migrate(file_path) {
fs.readFile(file_path, function(err, buf_data) {
var data = buf_data.toString();
data = data.replace(/{% math-block/g, '{% math_block');
data = data.replace(/{% endmath-block/g, '{% endmath_block');
data = data.replace(/{% rawblock/g, '{% raw');
data = data.replace(/{% endrawblock/g, '{% endraw');
var start_pos = 0;
while (true) {
var st_str = '{% math ', args = '';
var pos = data.indexOf(st_str, start_pos);
if (pos == -1) break;
for (var i = pos + st_str.length; i < data.length; i++) {
if (data[i] == '%' && data[i+1] == '}') {
var tmp = data.substring(0, pos-1) + '{% math %}' + args + '{% endmath %}';
start_pos = tmp.length;
data = tmp + data.substring(i+2);
break;
}
args = args + data[i];
}
}
var store_path = path.join('file_migrate', file_path);
mkdirp(path.dirname(store_path), function (err) {
if (err) return 0;
fs.writeFile(store_path, data, function(err) {
if (err) {
console.log(err);
return 0;
}
console.log('success : ' + file_path);
});
});
});
}
function read_dir(dir) {
fs.readdir(dir, function(err, files) {
if (err) return 0;
// Print folder name
files.forEach(function(file) {
file_full_path = path.join(dir, file);
(function(file_full_path) {
// Get file stat
fs.stat(file_full_path, function(err, stats) {
if (err) return 0;
if (stats.isDirectory())
read_dir(file_full_path);
if (!stats.isDirectory()) {
file_migrate(file_full_path);
// console.log("file_full_path="+file_full_path);
}
});
})(file_full_path);
});
});
}
read_dir(process.argv[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment