Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move kumascript .json files to content (yari) #4081

Merged
merged 14 commits into from
Aug 23, 2021
4 changes: 4 additions & 0 deletions build/spas.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ async function buildSPAs(options) {
continue;
}
for (const locale of fs.readdirSync(root)) {
if (locale === "jsondata") {
// This is actually not a locale but it's located next to the locales.
continue;
}
if (!fs.statSync(path.join(root, locale)).isDirectory()) {
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions kumascript/macros/APIRef.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ var hasTag = page.hasTag;
var htmlEscape = kuma.htmlEscape;
var rtlLocales = ['ar', 'he', 'fa'];

var webAPIData = JSON.parse(await template("InterfaceData"));
var webAPIData = web.getJSONData("InterfaceData");
if (group) {
var webAPIGroups = JSON.parse(await template("GroupData"));
var webAPIGroups = web.getJSONData("GroupData");
}

var commonl10n = JSON.parse(await template('L10n:Common'));
var commonl10n = web.getJSONData("L10n-Common");

var text = {
'Methods': mdn.getLocalString(commonl10n, 'Methods'),
Expand Down
2 changes: 1 addition & 1 deletion kumascript/macros/CSSInfo.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let replacePlaceholders = mdn.replacePlaceholders;
let localWiki = wiki;
let currentPage = page;

let cssLocalStrings = JSON.parse(await template("L10n:CSS"));
let cssLocalStrings = web.getJSONData("L10n-CSS");
let localStrings = require('mdn-data/l10n/css');

/*
Expand Down
2 changes: 1 addition & 1 deletion kumascript/macros/CSSSyntax.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let rawSyntax = "";
let formattedSyntax = "";
let localize = mdn.getLocalString;

let localStrings = JSON.parse(await template("L10n:CSS"));
let localStrings = web.getJSONData("L10n-CSS");

let s_where = mdn.localString({
"en-US": "where ",
Expand Down
4 changes: 2 additions & 2 deletions kumascript/macros/DefaultAPISidebar.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var htmlEscape = kuma.htmlEscape;
// slug is not available in preview mode.
if (slug && group) {

var webAPIGroups = JSON.parse(await template("GroupData"));
var webAPIGroups = web.getJSONData("GroupData");

var commonl10n = JSON.parse(await template('L10n:Common'));
var commonl10n = web.getJSONData("L10n-Common");

var text = {
'Methods': mdn.getLocalString(commonl10n, 'Methods'),
Expand Down
6 changes: 3 additions & 3 deletions kumascript/macros/InheritanceDiagram.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%
<%
/*
Draws an responsive SVG diagram for the inheritance chain (inverted in rtl)

Expand All @@ -8,7 +8,7 @@
$3 - Interface name
*/

var data = JSON.parse(await template("InterfaceData"));
var data = web.getJSONData("InterfaceData");
var slug = env.slug;
var result = "";
// slug is not available in preview mode.
Expand Down Expand Up @@ -62,7 +62,7 @@ function lineWithTriangle(x, y, reverse) {
var str = '';
if (reverse) {
str += '<polyline points="'+x+','+(y+24)+' '+(x-10)+','+(y+19)+' '+(x-10)+','+(y+29)+' '+x+','+(y+24)+'" stroke="#D4DDE4" fill="none"/>';
x -= 10;
x -= 10;
str += '<line x1="'+x+'" y1="'+(y+24)+'" x2="'+(x-30)+'" y2="'+(y+24)+'" stroke="#D4DDE4"/>';
} else {
str += '<polyline points="'+x+','+(y+24)+' '+(x+10)+','+(y+19)+' '+(x+10)+','+(y+29)+' '+x+','+(y+24)+'" stroke="#D4DDE4" fill="none"/>';
Expand Down
4 changes: 2 additions & 2 deletions kumascript/macros/JSRef.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
// $2 - Related objects to display (comma separated) – OBSOLETE

// Strings
var commonl10n = JSON.parse(await template('L10n:Common'));
var jsl10n = JSON.parse(await template('L10n:JavaScript'));
var commonl10n = web.getJSONData('L10n-Common');
var jsl10n = web.getJSONData('L10n-JavaScript');

var text = {
'stdlib': mdn.getLocalString(jsl10n, 'stdlib'),
Expand Down
80 changes: 0 additions & 80 deletions kumascript/macros/L10n-HTML.json

This file was deleted.

2 changes: 1 addition & 1 deletion kumascript/macros/ListGroups.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let spacesToUnderscores = web.spacesToUnderscores;

// Get the GroupData database

let groupData = JSON.parse(await template('GroupData'))[0];
let groupData = web.getJSONData("GroupData")[0];
let groupNames = Object.keys(groupData);
groupNames.sort();

Expand Down
2 changes: 1 addition & 1 deletion kumascript/macros/ListSubpagesForSidebar.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var code = !$1 ? '<code>' : '';
var endcode = !$1 ? '</code>' : '';

// Strings
var commonl10n = JSON.parse(await template('L10n:Common'));
var commonl10n = web.getJSONData('L10n-Common');
var text = {
'translate': mdn.getLocalString(commonl10n, '[Translate]'),
'title': mdn.getLocalString(commonl10n, 'TranslationCTA'),
Expand Down
1 change: 0 additions & 1 deletion kumascript/macros/MissingPages.json

This file was deleted.

2 changes: 1 addition & 1 deletion kumascript/macros/RFC.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
var link = "https://tools.ietf.org/html/rfc" + $0;
var text = "";

var commonl10n = JSON.parse(await template('L10n:Common'));
var commonl10n = web.getJSONData('L10n-Common');

if ($1 && $1 != undefined && $1.length) {
text = ": " + $1;
Expand Down
2 changes: 1 addition & 1 deletion kumascript/macros/SectionOnPage.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let text = "";
let page = await wiki.getPage($0);
let title = kuma.htmlEscape(page.title);

let commonLocalStrings = JSON.parse(await template("L10n:Common"));
let commonLocalStrings = web.getJSONData("L10n-Common");
let localize = mdn.getLocalString;

if (!title || title == undefined || title == "undefined") {
Expand Down
2 changes: 1 addition & 1 deletion kumascript/macros/SpecName.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var title;
var result;
var lang = env.locale;

var specList = JSON.parse(await template('SpecData'));
var specList = web.getJSONData("SpecData");

// Some APIs share specifications. Here we attend to that.

Expand Down
2 changes: 1 addition & 1 deletion kumascript/macros/WebExtAPISidebar.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var baseAPIPage = baseURL + "Mozilla/Add-ons/WebExtensions/API";
var htmlEscape = kuma.htmlEscape;
var rtlLocales = ['ar', 'he', 'fa'];

let commonl10n = JSON.parse(await template('L10n:Common'));
let commonl10n = web.getJSONData('L10n-Common');

let text = {
'translate': mdn.getLocalString(commonl10n, '[Translate]'),
Expand Down
2 changes: 1 addition & 1 deletion kumascript/macros/spec2.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* parameters: */
/* $0 = name of specification */

var specList = JSON.parse(await template('SpecData'));
var specList = web.getJSONData("SpecData");

var status = {};

Expand Down
6 changes: 3 additions & 3 deletions kumascript/macros/svginfo.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
var slug = env.slug;
var name = $0 || (slug ? slug.split("/").pop() :
"preview-wiki-content");
var data = JSON.parse(await template("SVGData"));
var data = web.getJSONData("SVGData");
var formattedError = "<span style=\"color:red;\">$1$</span>";
var commonLocalStrings = JSON.parse(await template("L10n:Common"));
var localStrings = JSON.parse(await template("L10n:SVG"));
var commonLocalStrings = web.getJSONData("L10n-Common");
var localStrings = web.getJSONData("L10n-SVG");

var localize = mdn.getLocalString;
var replacePlaceholders = mdn.replacePlaceholders;
Expand Down
33 changes: 30 additions & 3 deletions kumascript/src/api/web.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/**
* @prettier
*/
const fs = require("fs");
const path = require("path");

const util = require("./util.js");
const Templates = require("../templates.js");

const DUMMY_BASE_URL = "https://example.com";
const L10N_COMMON_STRINGS = new Templates().getLocalizedCommonStrings();

const { CONTENT_ROOT } = require("../../../content");

const _warned = new Map();
// The purpose of this function is to make sure `console.warn` is only called once
Expand Down Expand Up @@ -159,8 +162,9 @@ module.exports = {
flawAttribute = ` data-flaw-src="${util.htmlEscape(flaw.macroSource)}"`;
}
// Let's get a potentially localized title for when the document is missing.
fs.appendFileSync("/tmp/reads.log", `titleWhenMissing\n`, "utf-8");
const titleWhenMissing = this.mdn.getLocalString(
L10N_COMMON_STRINGS,
this.web.getJSONData("L10n-Common"),
"summary"
);
return `<a class="page-not-created" title="${titleWhenMissing}"${flawAttribute}>${content}</a>`;
Expand All @@ -179,4 +183,27 @@ module.exports = {
// Remove unsafe characters, and collapse whitespace gaps into
// underscores.
slugify: util.slugify,

// Return specific .json files from the content root
getJSONData(name) {
const filePath = path.join(CONTENT_ROOT, "jsondata", `${name}.json`);
try {
return readJSONDataFile(filePath);
} catch (error) {
console.error(`Tried to read JSON from ${filePath}`, error);
throw error;
}
},
};

const _readJSONDataCache = new Map();
function readJSONDataFile(filePath) {
if (_readJSONDataCache.has(filePath)) {
return _readJSONDataCache.get(filePath);
}
const payload = JSON.parse(fs.readFileSync(filePath, "utf-8"));
if (process.env.NODE_ENV === "production") {
_readJSONDataCache.set(filePath, payload);
}
return payload;
}
5 changes: 0 additions & 5 deletions kumascript/src/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ class Templates {
}
}

getLocalizedCommonStrings() {
const path = this.macroNameToPath.get("l10n-common");
return JSON.parse(fs.readFileSync(path));
}

async render(name, args) {
// Normalize the macro name by converting colons to hyphens and
// uppercase letters to lowercase.
Expand Down
21 changes: 12 additions & 9 deletions kumascript/tests/macros/DefaultAPISidebar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ const commonl10nFixturePath = path.resolve(
__dirname,
"fixtures/defaultapisidebar/commonl10n.json"
);
const commonl10nFixture = fs.readFileSync(commonl10nFixturePath, "utf8");
const commonl10nFixture = JSON.parse(
fs.readFileSync(commonl10nFixturePath, "utf8")
);
const groupDataFixturePath = path.resolve(
__dirname,
"fixtures/defaultapisidebar/groupdata.json"
);
const groupDataFixture = fs.readFileSync(groupDataFixturePath, "utf8");
const groupDataFixture = JSON.parse(
fs.readFileSync(groupDataFixturePath, "utf8")
);

/**
* All the const objects that follow define bits of the data we expect.
Expand Down Expand Up @@ -301,16 +305,15 @@ describeMacro("DefaultAPISidebar", function () {
beforeEachMacro(function (macro) {
// env.slug only has to be truthy
macro.ctx.env.slug = "not undefined";
// Mock calls to L10n-Common and GroupData
const originalTemplate = macro.ctx.template;
macro.ctx.template = jest.fn(async (name, ...args) => {
if (name === "L10n:Common") {
return commonl10nFixture;
}
// Mock calls to getJSONData()
macro.ctx.web.getJSONData = jest.fn((name) => {
if (name === "GroupData") {
return groupDataFixture;
}
return await originalTemplate(name, ...args);
if (name === "L10n-Common") {
return commonl10nFixture;
}
throw new Error(`Unimplemented mock fixture ${name}`);
});
// Mock calls to wiki.getPage()
macro.ctx.wiki.getPage = jest.fn(async (url) => {
Expand Down
Loading