Last active
August 29, 2015 13:56
-
-
Save eschwartz/9142013 to your computer and use it in GitHub Desktop.
Handlebars helpers for parsing object types for YUIDocs templates
This file contains 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
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); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice