Compare commits

...
Sign in to create a new pull request.

4 commits

Author SHA1 Message Date
James Brooks
c32b48da0f General formatting 2017-07-27 19:57:05 -04:00
James Brooks
085e76d7bb Be consistent 2017-07-27 19:55:53 -04:00
James Brooks
2fb885282b Error messages now make sense 2017-07-27 19:49:23 -04:00
James Brooks
1207f9a87c Use strict equality checks 2017-07-27 19:49:04 -04:00

View file

@ -3,23 +3,23 @@ fs = require('fs')
function md_trim(str, context) { function md_trim(str, context) {
str = str.replace(/(^\s+)|(\s+$)/g, ""); str = str.replace(/(^\s+)|(\s+$)/g, "");
if (context == 1) { // Name if (context === 1) { // Name
// placeholder for any formatting on name value // placeholder for any formatting on name value
} else if (context == 2) { // Description } else if (context === 2) { // Description
str = str.replace(".", ""); // remove ending periods on descriptions str = str.replace(".", ""); // remove ending periods on descriptions
} else if (context == 3) { // Auth } else if (context === 3) { // Auth
if (str.toUpperCase() == "NO") { if (str.toUpperCase() === "NO") {
str = null str = null
} else { } else {
str = str.replace("`", "").replace("`", "") str = str.replace("`", "").replace("`", "")
} }
} else if (context == 4) { // HTTPS } else if (context === 4) { // HTTPS
if (str.toUpperCase() == "YES") { if (str.toUpperCase() === "YES") {
str = true str = true
} else { } else {
str = false str = false
} }
} else if (context == 5) { // Link } else if (context === 5) { // Link
str = str.replace("[Go!]", "").slice(1, -1); str = str.replace("[Go!]", "").slice(1, -1);
} }
return str; return str;
@ -30,6 +30,7 @@ function handle(filename, anchor) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
var lines = text.split("\n"); var lines = text.split("\n");
var cur_line = 0; var cur_line = 0;
var line = "" var line = ""
@ -37,17 +38,20 @@ function handle(filename, anchor) {
var col_num = 0; var col_num = 0;
var cols = []; var cols = [];
var rows = []; var rows = [];
var root = {};
function read_line() { function read_line() {
return lines[cur_line++]; return lines[cur_line++];
} }
var root = {};
while (true) { while (true) {
var cols = []; var cols = [],
var rows = []; rows = [];
while (line.indexOf(anchor) == -1 && cur_line != lines.length) { while (line.indexOf(anchor) == -1 && cur_line != lines.length) {
line = read_line(); line = read_line();
} }
if (cur_line == lines.length) { if (cur_line == lines.length) {
break; break;
} }
@ -59,10 +63,10 @@ function handle(filename, anchor) {
if (line) { if (line) {
line = line.split("|") line = line.split("|")
for (var j in line) { for (var j in line) {
line[j] = md_trim(line[j], 0) line[j] = md_trim(line[j], 0)
if ((j == 0 || j == line.length - 1) && line[j] === "") {
if ((j == 0 || j == line.length - 1) && line[j] === "") {
//
} else { } else {
cols.push(line[j]); cols.push(line[j]);
} }
@ -71,61 +75,71 @@ function handle(filename, anchor) {
cols = line; cols = line;
rows.push(cols) rows.push(cols)
} else { } else {
console.error("markdown expect column title") console.error("Markdown expects column title")
break; break;
} }
} else { } else {
console.error("markdown expect table content") console.error("Markdown expects table content")
break; break;
} }
line = read_line() line = read_line()
if (!line) { if (!line) {
console.error("markdown expect table spliter") console.error("Markdown expects table spliter")
break; break;
} }
line = read_line()
while (line.indexOf("|") != -1 && cur_line != lines.length) {
line = read_line()
while (line.indexOf("|") != -1 && cur_line != lines.length) {
var line_this = line.split("|") var line_this = line.split("|")
var row = [] var row = []
for (var j in line_this) { for (var j in line_this) {
line_this[j] = md_trim(line_this[j], j) line_this[j] = md_trim(line_this[j], j)
if ((j == 0 || j == line_this.length - 1) && line_this[j] === "") { if ((j == 0 || j == line_this.length - 1) && line_this[j] === "") {
//
} else { } else {
row.push(line_this[j]); row.push(line_this[j]);
} }
} }
rows.push(row); rows.push(row);
line = read_line() line = read_line()
} }
var data=[]; var data = [];
for (var j in rows) { for (var j in rows) {
if (j != 0) { if (j != 0) {
var ele = {}; var ele = {};
for (var k in rows[j]) { for (var k in rows[j]) {
ele[rows[0][k]] = rows[j][k]; ele[rows[0][k]] = rows[j][k];
} }
data.push(ele); data.push(ele);
} }
} }
root[table_name] = data; root[table_name] = data;
} }
console.log(JSON.stringify(root)); console.log(JSON.stringify(root));
}); });
} }
if (process.argv.length < 3) { if (process.argv.length < 3) {
console.log("No .md file passed!"); console.log("No .md file passed!");
return; return;
} }
if (process.argv.length < 4) { if (process.argv.length < 4) {
anchorText = "###"; anchorText = "###";
} else { } else {
anchorText = process.argv[3]; anchorText = process.argv[3];
} }
handle(process.argv[2].toString(), anchorText); handle(process.argv[2].toString(), anchorText);