Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
bug 1328839: enable "require" of npm packages
Browse files Browse the repository at this point in the history
* add "mdn-browser-compat-data" to package.json
  and update "npm-shrinkwrap.json"
* change name of "require" method on "APIContext"
  class to "require_macro", and update all macros
  and tests to reflect the change
* add "require" method on "APIContext" class that
  maps to the nodejs "require" and add test
  • Loading branch information
escattone committed May 24, 2017
1 parent fd480ca commit db40f8f
Show file tree
Hide file tree
Showing 19 changed files with 209 additions and 164 deletions.
32 changes: 18 additions & 14 deletions lib/kumascript/api.js
Expand Up @@ -227,7 +227,7 @@ var APIContext = ks_utils.Class({
// require() expects to run inside a Fiber
Fiber(function () {
var tmpl_name = autorequire[install_name],
exports = $this.require(tmpl_name);
exports = $this.require_macro(tmpl_name);
setCaseVariantAliases($this, install_name, exports);
fe_next();
}).run();
Expand Down Expand Up @@ -340,30 +340,34 @@ var APIContext = ks_utils.Class({
return output;
},

// #### require(path)
// #### require_macro(name)
//
// Attempts to load and execute a template which, as a side effect, can
// populate an exports object in quasi-CommonJS style. The template output
// is ignored.
require: function (name) {

// Use an internal cache, so that repeated require() calls reuse the
// previously loaded results.
// This is a request to load a Kumascript macro as a module. It attempts
// to load and execute the named macro which, as a side effect, should
// populate an "exports" or "module.exports" object in nodejs style (the
// macro output is ignored).
require_macro: function (name) {
// Use an internal cache, so that repeated require() calls
// reuse the previously loaded results.
if (!(name in this._require_cache)) {
var clone_ctx = _.clone(this);

// Let's pretend we're following CommonJS module conventions
// Let's pretend we're following nodejs module conventions.
clone_ctx.module = { exports: {} };
clone_ctx.exports = clone_ctx.module.exports;

// clone_ctx is just like calling a template, only we ignore the output
// and return the side effect of populating exports.
// We ignore the output and return the side effect of
// populating the "module.exports" object.
clone_ctx.template(name, []);
this._require_cache[name] = clone_ctx.module.exports;
}

return this._require_cache[name];
}
},

// #### require(name)
//
// Load an npm package (the real "require" has its own cache).
require: require

});

Expand Down
50 changes: 25 additions & 25 deletions macros/DekiScript-Page.ejs
@@ -1,8 +1,8 @@
<% module.exports = buildAPI({
initialize: function (options) {
BaseAPI.prototype.initialize.call(this, options);
// Incomplete list of Page vars, but probably good enough.
// See also: <http://developer.mindtouch.com/en/docs/DekiScript/Reference/Wiki_Functions_and_Variables/Page>
this.setVars({
Expand All @@ -16,15 +16,15 @@
uri: env.url
});
},
// Determines whether or not the page has the specified tag. Returns true
// if it does, otherwise false. This is case-insensitive.
//
// Parameters:
//
hasTag: function(aPage, aTag) {
//
hasTag: function(aPage, aTag) {
// First, return false at once if there are no tags on the page
if (aPage.tags == undefined || aPage.tags == null || aPage.tags.length == 0) {
return false;
}
Expand All @@ -34,13 +34,13 @@
var theTag = aTag.toLowerCase();
// Now look for a match
for (var i=0; i<aPage.tags.length; i++) {
if (aPage.tags[i].toLowerCase() == theTag) {
return true;
}
}
return false;
},
Expand All @@ -60,7 +60,7 @@
if(depth_check >= 0) {
url += '?depth=' + depth_check;
}
var mdn = require('MDN:Common');
var mdn = require_macro('MDN:Common');
var subpages = mdn.fetchJSONResource(url);
var result = [];
if (subpages != null) {
Expand All @@ -85,14 +85,14 @@
} else {
url = env.url + '$children';
}
url += '?expand';
var depth_check = parseInt(depth);
if(depth_check >= 0) {
url += '&depth=' + depth_check;
}
var mdn = require('MDN:Common');
var mdn = require_macro('MDN:Common');
var subpages = mdn.fetchJSONResource(url);
var result = [];
if (subpages != null) {
Expand All @@ -104,7 +104,7 @@
}
return result;
},
// If reverse is non-zero, the sort is backward
subPagesSort: function (pages, reverse) {
if (reverse == 0) {
Expand Down Expand Up @@ -161,12 +161,12 @@
return aa.length - bb.length;
}
},
// Flatten subPages list
subPagesFlatten: function (pages) {
var output = [];
process_array(pages);
return output;
Expand All @@ -186,25 +186,25 @@
}
}
},
// If ordered is true, the output is an <ol> instead of <ul>
//
// Special note: If ordered is true, pages whose locale differs from
// the current page's locale are omitted, to work around misplaced
// localizations showing up in navigation.
subPagesFormatList: function (pages, ordered) {
return process_array(pages, ordered != 0);
function process_array(arr, ordered) {
var result = '';
var openTag = '<ul>';
var closeTag = '</ul>';
if (ordered) {
openTag = '<ol>';
closeTag = '</ol>';
}
if(arr.length) {
result += openTag;
arr.forEach(function(item) {
Expand All @@ -217,15 +217,15 @@
result += item.title;
}
result += process_array(item.subpages || [], ordered) + '</li>';
});
result += closeTag;
}
return result;
}
},
translations: function (path) {
var url = '';
if (path) {
Expand All @@ -234,13 +234,13 @@
} else {
url = env.url + '$json';
}
var mdn = require('MDN:Common');
var mdn = require_macro('MDN:Common');
var json = mdn.fetchJSONResource(url);
var result = [];
if (json != null) {
result = json.translations || [];
}
return result;
},
}); %>

0 comments on commit db40f8f

Please sign in to comment.