Skip to content

Instantly share code, notes, and snippets.

@26medias
Created June 22, 2017 22:45
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 26medias/7313fd15e67648cbd07039435f8ebe4e to your computer and use it in GitHub Desktop.
Save 26medias/7313fd15e67648cbd07039435f8ebe4e to your computer and use it in GitHub Desktop.
NodeJS code to convert a MD style formatting to a JS object
var fstool = require('fs-tool');
var _ = require('underscore');
var path = require('path');
var pstack = require('pstack');
var log = function(a,b) {
console.log("#["+a+"]",JSON.stringify(b,null,4));
}
var converter = function() {};
converter.prototype.download = function(mdFile, outputDir) {
var scope = this;
this.mdFile = mdFile;
this.outputDir = outputDir;
// Read the file
this.read(this.mdFile, function(data) {
log("data",data);
// Get the images with their names
var labels = scope.mapToLabels(data);
log("labels",labels);
// Download
var stack = new pstack();
_.each(labels, function(jpath,url) {
stack.add(function(done) {
fstool.file.download(url, outputDir+'/'+jpath.join('-')+'.png', function(response) {
log("Downloaded:", url);
done();
});
});
});
stack.start(function() {
log("Status", "Completed");
});
});
}
converter.prototype.mapToLabels = function(map, callback) {
var output = {};
this.labelExplorer([], map, output);
var i;
var n=0;
for (i in output) {
n++;
output[i] = [n].concat(output[i]);
}
return output;
}
converter.prototype.labelExplorer = function(jpath, map, output) {
var i;
for (i in map) {
if (_.isArray(map[i])) {
// Array of images!
_.each(map[i], function(img) {
output[img] = jpath.concat(i);
});
} else if(_.isObject(map[i])) {
this.labelExplorer(jpath.concat(i), map[i], output);
}
}
return output;
}
converter.prototype.read = function(filename, callback) {
var scope = this;
fstool.file.read(filename, function(content) {
var lines = content.split('\n');
lines = _.map(lines, function(line, n) {
var chars = line.split('');
var i;
var l = chars.length;
var t = 0;
for (i=0;i<l;i++) {
if (chars[i]=='\t') {
t++;
} else {
break;
}
}
return {
n: n,
t: t,
line: line.replace(/[\t]./gmi,'').replace('- ','').trim()
}
});
var data = scope.convert(lines);
callback(data);
});
}
converter.prototype.convert = function(lines) {
var scope = this;
if (!lines || lines.length==0) {
return false;
}
var baseLevel = lines[0].t;
var output = {};
lines = _.map(lines, function(line, n) {
line.n = n;
return line;
});
// Find the base level
_.each(lines, function(line, n) {
if (line.t==baseLevel) {
// Get the sublines that follow
var temp = [];
var _k = line.t+1;
for (i=line.n+1;i<lines.length;i++) {
if (lines[i].t>=_k) {
temp.push(lines[i]);
skipUntil = lines[i].n;
} else {
break;
}
}
output[line.line] = temp;
}
});
for (i in output) {
if (output[i] && output[i].length>0 && path.extname(output[i][0].line)=='.png') {
// Images!
output[i] = _.map(output[i], function(item) {
return item.line;
});
} else {
if (output[i] && output[i].length>0) {
output[i] = scope.convert(output[i]);
}
}
}
return output;
}
var conv = new converter();
conv.download('test.md', 'output');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment