diff --git a/lib/helpers.js b/lib/helpers.js index f7f0f5a..647a612 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -17,7 +17,6 @@ helpers.unresolve = function (base, abs) { helpers.buildToc = function (src) { - console.log(src); var src = "./pages"; // get list of directories. @@ -27,22 +26,24 @@ helpers.buildToc = function (src) { Object.keys(_articles).forEach(function (i) { if (_articles[i].isDirectory) { - _articles[i] = ""; + _articles[i] = null; } }); // build up the tree. var toc = fs2.dirToTree(_articles); + // sort the tree. + toc = helpers.tocSort(toc)[0]; + // digs until there is a layer with >1 item in it. - while(toc.length === 1) { - var key = Object.keys(toc[0])[0]; + while (toc.length === 1) { + var row = toc[0]; + var key = Object.keys(row)[0]; toc = toc[0][key]; } - //console.log(JSON.stringify(toc, true, 2)); - return helpers.treeToHTML(toc); } @@ -54,42 +55,66 @@ var isElement = helpers.isElement = function(xs, e) { // // Function to use with sorting the ToC. -// ie. tocSort(xs, order); +// ie. tocSort(toc); // -helpers.tocSort = function (xs, order) { +helpers.tocSort = function(toc) { + return toc.map(function sortToc (row, paths) { + var paths = paths || []; + var key = Object.keys(row)[0]; + var subtoc = row[key]; - Object.keys(order).forEach(function (t) { - if (order[t] < 0) { - // This is so you can use negative indices, like python - order[t] = xs.length + order[t]; - } - }) - - return xs.sort( function (a, b) { - - //console.log(path.basename(a)); - //console.log(path.basename(b)); - var aHasOrder = order.hasOwnProperty(path.basename(a)), - bHasOrder = order.hasOwnProperty(path.basename(b)); - - if (aHasOrder && bHasOrder) { - return order[a]-order[b]; - } else if (aHasOrder) { - console.log("a: "+order[path.basename(a)]); - console.log("b: "+xs.indexOf(b)); - return order[path.basename(a)] - xs.indexOf(b) - } else if (bHasOrder) { - console.log(path.basename(a)+": "+xs.indexOf(a)); - console.log(path.basename(b)+": "+order[path.basename(b)]); - return order[path.basename(b)]; - xs.indexOf(a) + var paths = paths.concat([[key]]); + + //Pretty sweet directory view XD + //console.log(Array(paths.length+1).join(Array(3).join(" ")) + "-"+paths[paths.length-1]); + + // TODO: fs.fileExists instead + try { + var order = JSON.parse(fs.readFileSync(paths.join("/")+"/../metadata.json").toString()).order; + + // try to sort the table of contents + subtoc = subtoc.sort(function (a, b) { + var aKey = Object.keys(a)[0]; + var bKey = Object.keys(b)[0]; + + var aIndex = order[aKey] || 0; + var bIndex = order[bKey] || 0; + + + // would be cleaner with a sgn() function + if (aIndex < 0) { + aIndex = subtoc.length + aIndex; + } else if (aIndex > 0) { + aIndex = aIndex - subtoc.length; + } + + if (bIndex < 0) { + bIndex = subtoc.length + bIndex; + } else if (bIndex > 0) { + bIndex = bIndex - subtoc.length; + } + + return aIndex-bIndex; + }); + + + } catch (e) { + //this just means there's nothing to sort. + if (e.code !== "EBADF") { + throw e; + } } - if (a > b) { - return 1; - } else if (a < b) { - return -1; + if (typeof subtoc === "object" && subtoc !== null) { + return subtoc.map(function (row) { + var newRow = {}; + var key = Object.keys(row)[0]; + + newRow[key] = sortToc(row, paths); + return newRow; + }); } else { - return 0; + return subtoc; } }); };