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

Commit

Permalink
refactor(env): Put exports at the bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Mar 30, 2019
1 parent 4807e01 commit df96a50
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 127 deletions.
4 changes: 2 additions & 2 deletions src/api/kuma.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const util = require('./util.js');

module.exports = {
/**
* Expose url from node.js to templates
* Expose url from node.js to templates.
*/
url: url,
url,
htmlEscape: util.htmlEscape
};
66 changes: 37 additions & 29 deletions src/api/mdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,37 @@ const request = require('request');

const util = require('./util.js');

module.exports = {
/**
* Given a set of names and a corresponding list of values, apply HTML
* escaping to each of the values and return an object with the results
* associated with the names.
*/
htmlEscapeArgs(names, args) {
function htmlEscapeArgs(names, args) {
var e = {};
names.forEach(function(name, idx) {
e[name] = util.htmlEscape(args[idx]);
});
return e;
},
}

/**
* Given a set of strings like this:
* { "en-US": "Foo", "de": "Bar", "es": "Baz" }
* Return the one which matches the current locale.
*/
localString(strings) {
function localString(strings) {
var lang = this.env.locale;
if (!(lang in strings)) lang = 'en-US';
return strings[lang];
},
}

/**
* Given a set of string maps like this:
* { "en-US": {"name": "Foo"}, "de": {"name": "Bar"} }
* Return a map which matches the current locale, falling back to en-US
* properties when the localized map contains partial properties.
*/
localStringMap(maps) {
function localStringMap(maps) {
var lang = this.env.locale;
var defaultMap = maps['en-US'];
if (lang == 'en-US' || !(lang in maps)) {
Expand All @@ -53,7 +52,7 @@ module.exports = {
}
}
return map;
},
}

/**
* Given a set of strings like this:
Expand All @@ -68,7 +67,7 @@ module.exports = {
* "hello");
* => "Hallo!" (in case the locale is 'de')
*/
getLocalString(strings, key) {
function getLocalString(strings, key) {
if (!strings.hasOwnProperty(key)) {
return key;
}
Expand All @@ -79,7 +78,7 @@ module.exports = {
}

return strings[key][lang];
},
}

/**
* Given a string, replaces all placeholders outlined by
Expand All @@ -103,7 +102,7 @@ module.exports = {
* {hello: "hallo", world: "Welt", contributor: "Mitwirkender"})
* => "hallo Welt, hallo Mitwirkender!"
*/
replacePlaceholders(string, replacements) {
function replacePlaceholders(string, replacements) {
function replacePlaceholder(placeholder) {
var index = placeholder.substring(1, placeholder.length - 1);
if (!Number.isNaN(Number(index))) {
Expand All @@ -113,18 +112,13 @@ module.exports = {
}

return string.replace(/\$\w+\$/g, replacePlaceholder);
},

/**
* Given a string, escapes all quotes within it.
*/
escapeQuotes: util.escapeQuotes,
}

/**
* Accepts a relative URL or an attachment object
* Returns the content of a given file.
*/
async getFileContent(fileObjOrUrl) {
async function getFileContent(fileObjOrUrl) {
var url = fileObjOrUrl.url || fileObjOrUrl;
if (!url) return '';

Expand Down Expand Up @@ -161,11 +155,11 @@ module.exports = {
next('error: ' + e);
}
});
},
}

// Fetch an HTTP resource with JSON representation, parse the JSON and
// return a JS object.
async fetchJSONResource(url, opts) {
async function fetchJSONResource(url, opts) {
opts = util.defaults(opts || {}, {
headers: {
'Cache-Control': this.env.cache_control,
Expand All @@ -174,10 +168,10 @@ module.exports = {
}
});
return JSON.parse(await this.MDN.fetchHTTPResource(url, opts));
},
}

// Fetch an HTTP resource, return the response body.
async fetchHTTPResource(url, opts) {
async function fetchHTTPResource(url, opts) {
opts = util.defaults(opts || {}, {
method: 'GET',
headers: {
Expand Down Expand Up @@ -205,21 +199,18 @@ module.exports = {
}

if (opts.ignore_cache_control) {
return await util.cacheFnIgnoreCacheControl(
opts.cache_key,
to_cache
);
return await util.cacheFnIgnoreCacheControl(opts.cache_key, to_cache);
} else {
return await util.cacheFn(
opts.cache_key,
this.env.cache_control,
to_cache
);
}
},
}

// http://www.bugzilla.org/docs/4.2/en/html/api/Bugzilla/WebService/Bug.html#search
async bzSearch(query) {
async function bzSearch(query) {
/* Fix colon (":") encoding problems */
query = query.replace(/&/g, '&');
query = encodeURI(query);
Expand All @@ -229,12 +220,29 @@ module.exports = {
query;
var resource = await this.MDN.fetchJSONResource(url);
return resource.result;
},
}

/* Derive the site URL from the request URL */
siteURL() {
function siteURL() {
var p = url.parse(this.env.url, true),
site_url = p.protocol + '//' + p.host;
return site_url;
}

module.exports = {
htmlEscapeArgs,
localString,
localStringMap,
getLocalString,
replacePlaceholders,

/**
* Given a string, escapes all quotes within it.
*/
escapeQuotes: util.escapeQuotes,
getFileContent,
fetchJSONResource,
fetchHTTPResource,
bzSearch,
siteURL
};
30 changes: 17 additions & 13 deletions src/api/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
*/
const util = require('./util.js');

module.exports = {
// Determines whether or not the page has the specified tag. Returns
// true if it does, otherwise false. This is case-insensitive.
//
hasTag: function(aPage, aTag) {
function hasTag(aPage, aTag) {
// First, return false at once if there are no tags on the page

if (
Expand All @@ -31,7 +30,7 @@ module.exports = {
}

return false;
},
}

// Optional path, defaults to current page
//
Expand All @@ -43,7 +42,7 @@ module.exports = {
//
// This is not called by any macros, and is only used here by
// wiki.tree(), so we could move it to be part of that function.
async subpages(path, depth, self) {
async function subpages(path, depth, self) {
var url = util.apiURL((path ? path : this.env.url) + '$children');
var depth_check = parseInt(depth);
if (depth_check >= 0) {
Expand All @@ -60,7 +59,7 @@ module.exports = {
}
}
return result;
},
}

// Optional path, defaults to current page
//
Expand All @@ -70,10 +69,8 @@ module.exports = {
// Optional self, defaults to false. Include the path page in
// the results
//
async subpagesExpand(path, depth, self) {
var url = util.apiURL(
(path ? path : this.env.url) + '$children?expand'
);
async function subpagesExpand(path, depth, self) {
var url = util.apiURL((path ? path : this.env.url) + '$children?expand');
var depth_check = parseInt(depth);
if (depth_check >= 0) {
url += '&depth=' + depth_check;
Expand All @@ -88,10 +85,10 @@ module.exports = {
}
}
return result;
},
}

// Flatten subPages list
subPagesFlatten(pages) {
function subPagesFlatten(pages) {
var output = [];

process_array(pages);
Expand All @@ -114,9 +111,9 @@ module.exports = {
});
}
}
},
}

async translations(path) {
async function translations(path) {
var url = util.apiURL((path ? path : this.env.url) + '$json');
var json = await this.MDN.fetchJSONResource(url);
var result = [];
Expand All @@ -125,4 +122,11 @@ module.exports = {
}
return result;
}

module.exports = {
hasTag,
subpages,
subpagesExpand,
subPagesFlatten,
translations
};

0 comments on commit df96a50

Please sign in to comment.