Last active
August 29, 2015 14:03
-
-
Save shadowmint/c9561de43f9a87e56be6 to your computer and use it in GitHub Desktop.
Tagged post filter for Wintersmith
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"locals": { | |
"url": "http://shadowmint.com", | |
"name": "Coffee & Code", | |
"owner": "shadowmint", | |
"description": "Code adventures with coffee~" | |
}, | |
"plugins": [ | |
"./plugins/paginator.coffee", | |
"./plugins/debug.js", // <------------ | |
"./plugins/tagged.js" // <------------ | |
], | |
"require": { | |
"moment": "moment", | |
"_": "underscore", | |
"typogr": "typogr" | |
}, | |
"jade": { | |
"pretty": true | |
}, | |
"markdown": { | |
"smartLists": true, | |
"smartypants": true | |
}, | |
"paginator": { | |
"perPage": 3 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Extra credit, a debugging module too | |
module.exports = function (env, callback) { | |
/** Helps sort arbitrary content */ | |
var categorize = function (rname, root, key, from) { | |
// Special exceptions | |
if (key == 'constructor') | |
return; | |
else if (key == 'parent') | |
return; | |
else if (key == 'html') | |
return; | |
// Is this some kind of helper function? | |
if (typeof(from[key]) == 'function') { | |
try { | |
root.helpers[key + '()'] = from[key](); | |
} | |
catch (e) { | |
// Stupid functions with no obvious usage can fuck off. | |
} | |
} | |
// Not a function? Must be data | |
else { | |
try { | |
JSON.stringify(from[key]); | |
root.data[rname + '.' + key] = from[key]; | |
} | |
catch(e) { | |
var tmp = {}; | |
for (var k in from[key]) { | |
try { | |
JSON.stringify(from[key][k]); | |
tmp[k] = from[key][k]; | |
} | |
catch(e) { | |
tmp[k] = '...'; | |
} | |
} | |
root.data[key] = tmp; | |
} | |
} | |
}; | |
/** Page processing helper */ | |
var handle_page = function (page) { | |
// Setup data blocks | |
var data = { | |
helpers: { | |
__info: 'via page.XXX; eg. page.getUrl()' | |
}, | |
data: { | |
__info: 'via page.XXX; eg. page.metadata.template', | |
'parent': 'parent data object, see env.helpers.debug(page.parent, contents)' | |
} | |
}; | |
// Process page data | |
for (var key in page) { | |
if (!key.indexOf('_') == 0) { | |
categorize('page', data, key, page); | |
} | |
} | |
// Return all debugging data | |
var rtn = { | |
'page': data, | |
toString: function () { | |
return JSON.stringify(rtn, null, 4); | |
} | |
}; | |
return rtn; | |
}; | |
/** Contents processing helper */ | |
var handle_contents = function (page) { | |
// Setup data blocks | |
var data = { | |
data: { | |
} | |
}; | |
// Process page data | |
for (var key in page) { | |
if (!key.indexOf('_') == 0) { | |
categorize('contents', data, key, page); | |
} | |
} | |
// Return all debugging data | |
var rtn = { | |
'contents': data, | |
toString: function () { | |
return JSON.stringify(rtn, null, 4); | |
} | |
}; | |
return rtn; | |
}; | |
/** Publish this helper to external use */ | |
env.helpers.debug = function (c) { | |
if (!c) { | |
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'); | |
console.log('Debugging is not possible without context.'); | |
console.log('required usage in template is one of:\n'); | |
console.log('- var x = env.helpers.debug(page)\n'); | |
console.log('- var x = env.helpers.debug(contents)\n'); | |
console.log('= env.helpers.debug(page).toString()\n'); | |
console.log('= env.helpers.debug(contents).toString()\n'); | |
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'); | |
throw new Error('Invalid usage of env.helpers.debug') | |
} | |
if (c['filepath']) { | |
return handle_page(c); | |
} | |
return handle_contents(c); | |
}; | |
// Done! | |
callback(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends layout | |
block content | |
| Meta | |
- var articles = env.helpers.tagged(contents, ['hello']) | |
each article in articles | |
h1 | |
= article.metadata.title | |
pre | |
= env.helpers.debug(article) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (env, callback) { | |
/** | |
* Return a list of articles that match any of the given tags | |
* | |
* Tag articles by putting tags in the meta data for an article: | |
* | |
* | |
* @param c contents | |
* @param tags array of tag values | |
* @returns Array of articles sorted by creation date | |
*/ | |
env.helpers.tagged = function (c, tags) { | |
var rtn = []; | |
for (var name in c.articles) { | |
var article = c.articles[name]['index.md']; | |
var atags = article.metadata['tags'] ? article.metadata.tags : ''; | |
var ptags = atags.split(',').map(function(x) { return x.trim(); }); | |
var done = false; | |
for (var i = 0; !done && (i < tags.length); ++i) { | |
for (var j = 0; !done && (j < ptags.length); ++j) { | |
if (tags[i].toLowerCase() == ptags[j].toLowerCase()) { | |
rtn.push(article); | |
done = true; | |
} | |
} | |
} | |
} | |
return rtn; | |
}; | |
// Done! | |
callback(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment