Skip to content

Instantly share code, notes, and snippets.

@jlewin
Last active December 23, 2015 07:59
Show Gist options
  • Select an option

  • Save jlewin/6604334 to your computer and use it in GitHub Desktop.

Select an option

Save jlewin/6604334 to your computer and use it in GitHub Desktop.
Helper script to extract JSON BOM data from http://reprap.org/wiki/Prusa_Mendel_(iteration_2)
var bom = {
parts: importTable($('table')[3]),
vitamins: importTable($('table')[5], 'Fasteners'),
wades: {
parts: importTable($('table')[4]),
vitamins: importTable($('table')[6], 'Fasteners')
}
};
function importTable(tbl, memberName) {
// Select column headers into fields array
var fields = $('thead th', tbl).map(function (t, m) { return $(m).text().trim(); }),
rows = [],
scope = {};
// In cases where the table has no heading separators, we don't need to create a named
// scope for the rows and can simply return the array
if (memberName) {
scope[memberName] = rows;
} else {
scope = rows;
}
$('tbody tr', tbl).each(function () {
var th_text = $('th', this).eq(0).text().trim();
if (th_text) {
// The prusa Rods section has a small paragraph in it, clip that
if (th_text.indexOf('Rods ') == 0) th_text = 'Rods';
// Shouldn't enter this block if memberName is empty. Caller has the responsiblity
// of making the distinction
rows = [];
scope[th_text] = rows;
console.log('setting: ' + th_text);
} else {
var row = $('td', this).map(function () { return $(this).text().trim(); }).get(),
o = {};
for (var c in row) {
o[fields[c]] = row[c];
}
rows.push(o);
}
});
return scope;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment