Skip to content

Instantly share code, notes, and snippets.

@eschwartz
Last active August 29, 2015 13:56
Show Gist options
  • Save eschwartz/9142013 to your computer and use it in GitHub Desktop.
Save eschwartz/9142013 to your computer and use it in GitHub Desktop.
Handlebars helpers for parsing object types for YUIDocs templates
var NATIVES = {
'Array': 1,
'Boolean': 1,
'Date': 1,
'decodeURI': 1,
'decodeURIComponent': 1,
'encodeURI': 1,
'encodeURIComponent': 1,
'eval': 1,
'Error': 1,
'EvalError': 1,
'Function': 1,
'Infinity': 1,
'isFinite': 1,
'isNaN': 1,
'Math': 1,
'NaN': 1,
'null': 1,
'Number': 1,
'Object': 1,
'parseFloat': 1,
'parseInt': 1,
'RangeError': 1,
'ReferenceError': 1,
'RegExp': 1,
'String': 1,
'SyntaxError': 1,
'TypeError': 1,
'undefined': 1,
'URIError': 1,
'HTMLElement': 'https:/' + '/developer.mozilla.org/en/Document_Object_Model_(DOM)/{type}',
'HTMLCollection': 'https:/' + '/developer.mozilla.org/en/Document_Object_Model_(DOM)/{type}',
'DocumentFragment': 'https:/' + '/developer.mozilla.org/en/Document_Object_Model_(DOM)/{type}',
'HTMLDocument': 'https:/' + '/developer.mozilla.org/en/Document_Object_Model_(DOM)/{type}',
'*': 'https:/' + '/developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects',
'$': 'https:/' + '/api.jquery.com/',
'jQuery': 'https:/' + '/api.jquery.com/'
};
function getTypeLink(type) {
var nativeTypeLink = NATIVES[type];
if (nativeTypeLink === 1) {
return 'https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/' + type;
}
else if (nativeTypeLink) {
return nativeTypeLink.replace('{type}', type);
}
else {
// not a native type
// Link to local class docs
return '/classes/' + type + '.html';
}
}
var helpers = {
// eg.
// {{#eachType 'Array.<Number>'}}
// <a href="{{link}}">{{type}}</a>
// {{/eachType}}
eachType: function(typeString, options) {
var typePattern = /(([\w\-]+(\.[\w-]+)+)|\w+|\*)/ig;
return typeString.replace(typePattern, function(type) {
return options.fn({
type: type,
link: getTypeLink(type)
});
})
},
// eg. (will wrap links around all object references)
// {{#eachTypeInText 'Some text {Array.<Number>} more text {String} foobar.'}}
// <a href="{{link}}">{{type}}</a>
// {{/eachTypeInText}}
eachTypeInText: function(text, options) {
var objectRefPattern = /(\{.*?\})/gi; // eg {SomeObject} (anything in brackets)
return text.replace(objectRefPattern, function(match) {
var matchWithoutBrackets = match.substr(1, match.length - 2);
return helpers.eachType(matchWithoutBrackets, options);
});
}
}
@eschwartz
Copy link
Author

I put this together while working on a Weather maps and data API library. The documentation for the project includes some custom YUIDoc themes, as well as some funky roll-your-own Handlebars templating using raw data generated from the Y.YUIDoc Nodejs module.

The project will be open-source within the next couple of weeks. If you're interested in taking a look, just let me know. I would love to get some beta testers poking around our code 😄

@mientjan
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment