Codacy style update for web.js

This commit is contained in:
Angelos Chalaris
2018-08-02 13:02:05 +03:00
parent d4bb9a8d0b
commit 7ecedec79c

View File

@ -138,7 +138,7 @@ if(!util.isTravisCI() || (util.isTravisCI() && (process.env['TRAVIS_EVENT_TYPE']
// Test the API's rate limit (keep for various reasons) // Test the API's rate limit (keep for various reasons)
https.get({host: githubApi, path: '/rate_limit?', headers: headers}, res => { https.get({host: githubApi, path: '/rate_limit?', headers: headers}, res => {
res.on('data', function (chunk) { res.on('data', function (chunk) {
console.log('Remaining requests: '+JSON.parse(chunk).resources.core.remaining); console.log(`Remaining requests: ${JSON.parse(chunk).resources.core.remaining}`);
}); });
}); });
// Send requests and wait for responses, write to the page // Send requests and wait for responses, write to the page
@ -146,10 +146,10 @@ if(!util.isTravisCI() || (util.isTravisCI() && (process.env['TRAVIS_EVENT_TYPE']
https.get({host: githubApi, path: '/repos/chalarangelo/30-seconds-of-code/contributors?per_page=1', headers: headers}, resContributors => { https.get({host: githubApi, path: '/repos/chalarangelo/30-seconds-of-code/contributors?per_page=1', headers: headers}, resContributors => {
https.get({host: githubApi, path: '/repos/chalarangelo/30-seconds-of-code/stargazers?per_page=1', headers: headers}, resStars => { https.get({host: githubApi, path: '/repos/chalarangelo/30-seconds-of-code/stargazers?per_page=1', headers: headers}, resStars => {
let commits = resCommits.headers.link.split('&').slice(-1)[0].replace(/[^\d]/g, ''), let commits = resCommits.headers.link.split('&').slice(-1)[0].replace(/[^\d]/g, ''),
contribs = resContributors.headers.link.split('&').slice(-1)[0].replace(/[^\d]/g, ''), contribs = resContributors.headers.link.split('&').slice(-1)[0].replace(/[^\d]/g, ''),
stars = resStars.headers.link.split('&').slice(-1)[0].replace(/[^\d]/g, ''); stars = resStars.headers.link.split('&').slice(-1)[0].replace(/[^\d]/g, '');
indexStaticFile = indexStaticFile.replace(/\$snippet-count/g, Object.keys(snippets).length).replace(/\$commit-count/g,commits).replace(/\$contrib-count/g,contribs).replace(/\$star-count/g, stars); indexStaticFile = indexStaticFile.replace(/\$snippet-count/g, Object.keys(snippets).length).replace(/\$commit-count/g, commits).replace(/\$contrib-count/g,contribs).replace(/\$star-count/g, stars);
indexStaticFile = minify(indexStaticFile, { indexStaticFile = minify(indexStaticFile, {
collapseBooleanAttributes: true, collapseBooleanAttributes: true,
collapseWhitespace: true, collapseWhitespace: true,
decodeEntities: false, decodeEntities: false,
@ -181,56 +181,56 @@ if(!util.isTravisCI() || (util.isTravisCI() && (process.env['TRAVIS_EVENT_TYPE']
// Create the output for individual category pages // Create the output for individual category pages
try { try {
// Add the start static part // Add the start static part
output += `${startPart + '\n'}`; output += `${startPart}${'\n'}`;
// Loop over tags and snippets to create the table of contents // Loop over tags and snippets to create the table of contents
for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))] for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))]
.filter(v => v) .filter(v => v)
.sort((a, b) => util.capitalize(a, true) === 'Uncategorized' ? 1 : util.capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) { .sort((a, b) => util.capitalize(a, true) === 'Uncategorized' ? 1 : util.capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) {
output += `<h3>` + output += '<h3>' +
md md
.render(`${util.capitalize(tag, true)}\n`) .render(`${util.capitalize(tag, true)}\n`)
.replace(/<p>/g, '') .replace(/<p>/g, '')
.replace(/<\/p>/g, '') + .replace(/<\/p>/g, '') +
`</h3>`; '</h3>';
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag)) for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag))
output += md output += md
.render(`[${taggedSnippet[0]}](./${tag}#${taggedSnippet[0].toLowerCase()})\n`) .render(`[${taggedSnippet[0]}](./${tag}#${taggedSnippet[0].toLowerCase()})\n`)
.replace(/<p>/g, '') .replace(/<p>/g, '')
.replace(/<\/p>/g, '') .replace(/<\/p>/g, '')
.replace(/<a/g, `<a class="sublink-1" tags="${taggedSnippet[1].join(',')}"`); .replace(/<a/g, `<a class="sublink-1" tags="${taggedSnippet[1].join(',')}"`);
output += '\n'; output += '\n';
} }
output += `</nav><main class="col-sm-12 col-md-8 col-lg-9" style="height: 100%;overflow-y: auto; background: #eceef2; padding: 0;">`; output += '</nav><main class="col-sm-12 col-md-8 col-lg-9" style="height: 100%;overflow-y: auto; background: #eceef2; padding: 0;">';
output += `<a id="top">&nbsp;</a>`; output += '<a id="top">&nbsp;</a>';
// Loop over tags and snippets to create the list of snippets // Loop over tags and snippets to create the list of snippets
for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))] for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1][0]))]
.filter(v => v) .filter(v => v)
.sort((a, b) => util.capitalize(a, true) === 'Uncategorized' ? 1 : util.capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) { .sort((a, b) => util.capitalize(a, true) === 'Uncategorized' ? 1 : util.capitalize(b, true) === 'Uncategorized' ? -1 : a.localeCompare(b))) {
let localOutput = output.replace(/\$tag/g, util.capitalize(tag)).replace(new RegExp(`./${tag}#`,'g'),'#'); let localOutput = output.replace(/\$tag/g, util.capitalize(tag)).replace(new RegExp(`./${tag}#`,'g'),'#');
localOutput += md localOutput += md
.render(`## ${util.capitalize(tag, true)}\n`) .render(`## ${util.capitalize(tag, true)}\n`)
.replace(/<h2>/g, '<h2 style="text-align:center;">'); .replace(/<h2>/g, '<h2 style="text-align:center;">');
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag)) for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1][0] === tag))
localOutput += localOutput +=
'<div class="card fluid">' + '<div class="card fluid">' +
md md
.render(`\n${snippets[taggedSnippet[0] + '.md']}`) .render(`\n${snippets[taggedSnippet[0] + '.md']}`)
.replace(/<h3/g, `<h3 id="${taggedSnippet[0].toLowerCase()}" class="section double-padded"`) .replace(/<h3/g, `<h3 id="${taggedSnippet[0].toLowerCase()}" class="section double-padded"`)
.replace(/<\/h3>/g, `${taggedSnippet[1].includes('advanced')?'<mark class="tag">advanced</mark>':''}</h3>`) .replace(/<\/h3>/g, `${taggedSnippet[1].includes('advanced')?'<mark class="tag">advanced</mark>':''}</h3>`)
.replace(/<\/h3>/g, '</h3><div class="section double-padded">') .replace(/<\/h3>/g, '</h3><div class="section double-padded">')
.replace(/<pre><code class="language-js">([^\0]*?)<\/code><\/pre>/gm, (match, p1) => `<pre class="language-js">${Prism.highlight(unescapeHTML(p1), Prism.languages.javascript)}</pre>`) .replace(/<pre><code class="language-js">([^\0]*?)<\/code><\/pre>/gm, (match, p1) => `<pre class="language-js">${Prism.highlight(unescapeHTML(p1), Prism.languages.javascript)}</pre>`)
.replace(/<\/pre>\s+<pre/g, '</pre><label class="collapse">Show examples</label><pre') + .replace(/<\/pre>\s+<pre/g, '</pre><label class="collapse">Show examples</label><pre') +
'<button class="primary clipboard-copy">&#128203;&nbsp;Copy to clipboard</button>' + '<button class="primary clipboard-copy">&#128203;&nbsp;Copy to clipboard</button>' +
'</div></div>'; '</div></div>';
// Add the ending static part // Add the ending static part
localOutput += `\n${endPart + '\n'}`; localOutput += `\n${endPart + '\n'}`;
// Optimize punctuation nodes // Optimize punctuation nodes
localOutput = util.optimizeNodes(localOutput, /<span class="token punctuation">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token punctuation">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token punctuation">${p1}${p2}${p3}</span>`); localOutput = util.optimizeNodes(localOutput, /<span class="token punctuation">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token punctuation">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token punctuation">${p1}${p2}${p3}</span>`);
// Optimize operator nodes // Optimize operator nodes
localOutput = util.optimizeNodes(localOutput, /<span class="token operator">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token operator">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token operator">${p1}${p2}${p3}</span>`); localOutput = util.optimizeNodes(localOutput, /<span class="token operator">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token operator">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token operator">${p1}${p2}${p3}</span>`);
// Optimize keyword nodes // Optimize keyword nodes
localOutput = util.optimizeNodes(localOutput, /<span class="token keyword">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token keyword">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token keyword">${p1}${p2}${p3}</span>`); localOutput = util.optimizeNodes(localOutput, /<span class="token keyword">([^\0<]*?)<\/span>([\n\r\s]*)<span class="token keyword">([^\0]*?)<\/span>/gm, (match, p1, p2, p3) => `<span class="token keyword">${p1}${p2}${p3}</span>`);
pagesOutput.push({'tag': tag,'content': localOutput}); pagesOutput.push({'tag': tag,'content': localOutput});
} }
// Minify output // Minify output
pagesOutput.forEach(page => { pagesOutput.forEach(page => {
@ -269,8 +269,8 @@ try {
.filter(key => beginnerSnippetNames.map(name => name+'.md').includes(key)) .filter(key => beginnerSnippetNames.map(name => name+'.md').includes(key))
.reduce((obj, key) => { .reduce((obj, key) => {
obj[key] = snippets[key]; obj[key] = snippets[key];
return obj; return obj;
}, {}); }, {});
for (let snippet of Object.entries(filteredBeginnerSnippets)) for (let snippet of Object.entries(filteredBeginnerSnippets))
beginnerOutput += beginnerOutput +=