Skip to content

Commit

Permalink
[refactor] Rebuilt table of contents rendering to enable sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
jfhbrook committed Nov 5, 2011
1 parent 493db4f commit 7f5a77e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 89 deletions.
18 changes: 11 additions & 7 deletions lib/fs2.js
Expand Up @@ -70,7 +70,7 @@ fs2.dirToTree = function (dir) {

//Very similar to filesToTree. Builds your basic nested deal.
var _tree = Object.keys(dir).reduce(function (acc, p) {
var ps = ("./"+path.normalize("./pages/"+p)).split("/");
var ps = (p).replace("//", "/").split("/");

traverse(acc).set(ps, {});
return acc;
Expand All @@ -81,17 +81,21 @@ fs2.dirToTree = function (dir) {
var ordered = function ordered (tree) {
var hash = Hash(tree).map(function (v) {
var files = [];
Object.keys(v).forEach( function (sv) {
// order-ify the next layer down
ordered(v[sv]).forEach(function(o) {
files.push(o);
});
// order-ify the next layer down
ordered(v).forEach(function(o) {
files.push(o);
});
return files;
}).items;

var _tree = [];
Object.keys(hash).forEach(function(k) {
Object.keys(hash).filter(function (k) {
if (k === "") {
console.log("One of the hash values is \"\". This should not be happening.");
return false;
}
return true;
}).forEach(function(k) {
var obj = {};
obj[k] = hash[k];
_tree.push(obj);
Expand Down
20 changes: 17 additions & 3 deletions lib/generators/content.js
Expand Up @@ -19,11 +19,22 @@ content.weld = function(dom, pages) {
//
var $ = docs.window.$;


// Here, we build up the table of contents.
var toc = helpers.buildToc(docs.src);

Object.keys(pages).forEach( function (i){
/*console.log((function () {
var sorts = {};
Object.keys(pages).forEach( function (i) {
if (pages[i].metadata && pages[i].metadata.order) {
Object.keys(pages[i].metadata.order).forEach(function (j) {
sorts[i+"/"+j] = pages[i].metadata.order[j];
});
}
});
return fs2.filesToTree(sorts);
})());*/

Object.keys(pages).forEach( function (i) {

// parse content if it exists
if (pages[i].content) {
Expand Down Expand Up @@ -134,7 +145,10 @@ content.weld = function(dom, pages) {
$('title', dom).html('node docs - ' + (metadata && metadata.title) || "");

//Add some meta tags
$('meta[name=keywords]', dom).attr('content', (metadata && metadata.tags || []).concat(docs.config.get("tags") || []).join(','));
$('meta[name=keywords]', dom).attr('content',
(metadata && metadata.tags || [])
.concat(docs.config.get("tags") || []).join(',')
);

//
// Remark: perform code highlighting, convert only inside <code/>
Expand Down
147 changes: 68 additions & 79 deletions lib/helpers.js
Expand Up @@ -17,28 +17,31 @@ helpers.unresolve = function (base, abs) {

helpers.buildToc = function (src) {

var p = helpers.unresolve(path.resolve(__dirname + "/../pages"), src);
console.log(src);
var src = "./pages";

var _articles = findit.sync(src);
// get list of directories.
var _articles = fs2.readDirSync(src, true, function (a) {
return path.extname(a).length === 0;
});

//
// Filter out all non-markdown files
//
_articles = _articles.filter(function(a){
a = path.resolve(a);
if(a.match(/\./)){
return false;
} else {
return true;
Object.keys(_articles).forEach(function (i) {
if (_articles[i].isDirectory) {
_articles[i] = "";
}
});

_articles = _articles.map(function (a) {
return helpers.unresolve(src, a);
});
// build up the tree.
var toc = fs2.dirToTree(_articles);

// digs until there is a layer with >1 item in it.
while(toc.length === 1) {
var key = Object.keys(toc[0])[0];

toc = toc[0][key];
}

// toc has an "undefined" value.
var toc = fs2.filesToTree(_articles);
//console.log(JSON.stringify(toc, true, 2));

return helpers.treeToHTML(toc);
}
Expand All @@ -51,83 +54,69 @@ var isElement = helpers.isElement = function(xs, e) {

//
// Function to use with sorting the ToC.
// ie. toc.sort(tocSort);
// ie. tocSort(xs, order);
//
helpers.tocSort = function (a, b) {
// For future reference, here is the list from the top-level.
/*[
'cryptography',
'advanced',
'intermediate',
'errors',
'file-system',
'javascript-conventions',
'child-processes',
'command-line',
'REPL',
'getting-started',
'HTTP' ]
*/

// These items should be listed *first*.
var first = [
'getting-started',
'javascript-conventions'
];

// These items should be listed *last*.
var last = [
'intermediate',
'advanced',
'cryptography'
];

var aInFirst = isElement(first, a),
bInFirst = isElement(first, b),
aInLast = isElement(last,a),
bInLast = isElement(last, b);


// Handles the case where a and/or b is in the "first" list.
if ( aInFirst || bInFirst ) {
if (aInFirst && bInFirst) {
return first.indexOf(a)-first.indexOf(b);
} else {
return aInFirst ? -1 : 1;
helpers.tocSort = function (xs, order) {

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)
}
}

if ( aInLast || bInLast ) {
if ( aInLast && bInLast) {
return last.indexOf(a) - last.indexOf(b);
if (a > b) {
return 1;
} else if (a < b) {
return -1;
} else {
return aInLast ? 1 : -1;
return 0;
}
}

if (a > b) {
return 1;
} else if (a < b) {
return -1;
} else {
return 0;
}

});
};

// TODO: Use weld.
helpers.treeToHTML = function(values, parent) {
var str = '<ul>';

//
// Sort the keys here for a reasonable ToC layout.
//
Object.keys(values).sort(helpers.tocSort).forEach( function(key) {
if (typeof values === "string") {
//todo: Make source of value be the article name.

if (typeof values[key]=='object' && values[key] != null){
//var title = values.split("/");
//title = title[title.length-1];

return "";

}

values.forEach( function(val, i) {
if (val) {
var key = Object.keys(val)[0];
}
if (typeof values[i]=='object' && values[i] != null){
var newParent = parent || '';
newParent = newParent + '/' + key;
var link = '/articles' + newParent;
str+='<li><a href="' + link + '">'+key.replace(/-/g, ' ')+'</a>' +helpers.treeToHTML(values[key], newParent)+'</li>';
str+='<li><a href="' + link + '">'+key.replace(/-/g, ' ')+'</a>' + helpers.treeToHTML(values[i][key], newParent)+'</li>';
}
});

Expand Down

0 comments on commit 7f5a77e

Please sign in to comment.