Merge pull request #3582 from tschaub/index-requires

Index what the examples require.
This commit is contained in:
Tim Schaub
2015-04-18 08:41:46 -06:00
+22 -16
View File
@@ -8,7 +8,7 @@ var pkg = require('../package.json');
var markupRegEx = /([^\/^\.]*)\.html$/; var markupRegEx = /([^\/^\.]*)\.html$/;
var cleanupJSRegEx = /.*(goog\.require(.*);|.*renderer: common\..*,?)[\n]*/g; var cleanupJSRegEx = /.*(goog\.require(.*);|.*renderer: common\..*,?)[\n]*/g;
var googRequiresRegEx = /.*goog\.require\('(ol\.\S*)'\);/g; var requiresRegEx = /.*goog\.require\('(ol\.\S*)'\);/g;
var isCssRegEx = /\.css$/; var isCssRegEx = /\.css$/;
var isJsRegEx = /\.js$/; var isJsRegEx = /\.js$/;
@@ -17,20 +17,20 @@ var destDir = path.join(__dirname, '..', 'build', 'examples');
var templatesDir = path.join(__dirname, '..', 'config', 'examples'); var templatesDir = path.join(__dirname, '..', 'config', 'examples');
/** /**
* Returns an array of classes that are explicitly required inside the source * Returns an array of names that are explicitly required inside the source
* by calling 'goog.require(…)' * by calling `goog.require('ol.…')`. Only returns `ol.` prefixed names.
* *
* @param {string} src The JavaScript sourcecode to search for goog.require. * @param {string} src The JavaScript sourcecode to search for goog.require.
* @returns {Array.<string>} An array of ol-classes that the source requires. * @returns {Array.<string>} An array of `ol.*` names.
*/ */
function getGoogRequires(src) { function getRequires(src) {
var googRequires = []; var requires = [];
var match = googRequiresRegEx.exec(src); var match = requiresRegEx.exec(src);
while (match) { while (match) {
googRequires.push(match[1]); requires.push(match[1]);
match = googRequiresRegEx.exec(src); match = requiresRegEx.exec(src);
} }
return googRequires; return requires;
} }
/** /**
@@ -38,12 +38,12 @@ function getGoogRequires(src) {
* HTML-snippet with an unordered list to the API-docs for the particular * HTML-snippet with an unordered list to the API-docs for the particular
* classes. * classes.
* *
* @param {Array.<string>} googRequires An array of ol-classes that the source * @param {Array.<string>} requires An array of `ol.` names that the source
* requires. * requires.
* @returns {string} The HTML-snippet with the list of links to API-docs. * @returns {string} The HTML-snippet with the list of links to API-docs.
*/ */
function getLinkToApiHtml(googRequires) { function getLinkToApiHtml(requires) {
var lis = googRequires.map(function(symb) { var lis = requires.map(function(symb) {
var href = '../apidoc/' + symb + '.html'; var href = '../apidoc/' + symb + '.html';
return '<li><a href="' + href + '" title="API documentation for ' + return '<li><a href="' + href + '" title="API documentation for ' +
symb +'">' + symb + '</a></li>'; symb +'">' + symb + '</a></li>';
@@ -83,10 +83,12 @@ function augmentExamples(files, metalsmith, done) {
throw new Error('No .js file found for ' + filename); throw new Error('No .js file found for ' + filename);
} }
var jsSource = files[jsFilename].contents.toString(); var jsSource = files[jsFilename].contents.toString();
var requires = getRequires(jsSource);
file.requires = requires;
file.js = { file.js = {
tag: '<script src="loader.js?id=' + id + '"></script>', tag: '<script src="loader.js?id=' + id + '"></script>',
source: jsSource.replace(cleanupJSRegEx, ''), source: jsSource.replace(cleanupJSRegEx, ''),
apiHtml: getLinkToApiHtml(getGoogRequires(jsSource)) apiHtml: getLinkToApiHtml(requires)
}; };
// add css tag and source // add css tag and source
@@ -127,10 +129,13 @@ function augmentExamples(files, metalsmith, done) {
*/ */
function createWordIndex(exampleInfos) { function createWordIndex(exampleInfos) {
var index = {}; var index = {};
var keys = ['shortdesc', 'title', 'tags']; var keys = ['shortdesc', 'title', 'tags', 'requires'];
exampleInfos.forEach(function(info, i) { exampleInfos.forEach(function(info, i) {
keys.forEach(function(key) { keys.forEach(function(key) {
var text = info[key]; var text = info[key];
if (Array.isArray(text)) {
text = text.join(' ');
}
var words = text ? text.split(/\W+/) : []; var words = text ? text.split(/\W+/) : [];
words.forEach(function(word) { words.forEach(function(word) {
if (word) { if (word) {
@@ -177,7 +182,8 @@ function createIndex(files, metalsmith, done) {
example: filename, example: filename,
title: example.title, title: example.title,
shortdesc: example.shortdesc, shortdesc: example.shortdesc,
tags: example.tags tags: example.tags,
requires: example.requires
}); });
} }
} }