Add webber script (WIP)

This commit is contained in:
Angelos Chalaris
2019-08-23 11:02:57 +03:00
parent 2e0301296f
commit 6990b61e44
42 changed files with 3007 additions and 2909 deletions

135
src/docs/util/index.js Normal file
View File

@ -0,0 +1,135 @@
const config = require('../../../config');
// Capitalizes the first letter of a string
const capitalize = (str, lowerRest = false) =>
str.slice(0, 1).toUpperCase() +
(lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
// Get the textual content in a gatsby page
const getTextualContent = (str, noExplain = false) => {
const regex = /([\s\S]*?)<div class="gatsby-highlight"/g;
const results = [];
let m = null;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) regex.lastIndex += 1;
m.forEach((match, groupIndex) => {
results.push(match);
});
}
if(noExplain)
return results[1].slice(0, results[1].lastIndexOf('<p>'));
return results[1];
};
// Gets the code blocks in a gatsby page
const getCodeBlocks = str => {
const regex = /<pre[.\S\s]*?<\/pre>/g;
let results = [];
let m = null;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) regex.lastIndex += 1;
// eslint-disable-next-line
m.forEach((match, groupIndex) => {
results.push(match);
});
}
const replacer = new RegExp(
`<pre class="language-${config.language}"><code class="language-${config.language}">([\\s\\S]*?)</code></pre>`,
'g',
);
const secondReplacer = new RegExp(
`<pre class="language-${config.secondLanguage}"><code class="language-${config.secondLanguage}">([\\s\\S]*?)</code></pre>`,
'g',
);
const optionalReplacer = new RegExp(
`<pre class="language-${config.optionalLanguage}"><code class="language-${config.optionalLanguage}">([\\s\\S]*?)</code></pre>`,
'g',
);
results = results.map(v =>
v
.replace(replacer, '$1')
.replace(secondReplacer, '$1')
.replace(optionalReplacer, '$1')
.trim()
);
if (results.length > 2)
return {
html: results[0],
css: results[1],
js: results[2],
};
return {
html: results[0],
css: results[1],
js: '',
};
};
// Optimizes nodes in an HTML string
const optimizeNodes = (data, regexp, replacer) => {
let count = 0;
let output = data;
do {
output = output.replace(regexp, replacer);
count = 0;
while (regexp.exec(output) !== null) ++count;
} while (count > 0);
return output;
};
const optimizeAllNodes = html => {
let output = html;
// Optimize punctuation nodes
output = optimizeNodes(
output,
/<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
output = optimizeNodes(
output,
/<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
output = optimizeNodes(
output,
/<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>`,
);
return output;
};
// Gets the code blocks for a snippet file.
const getRawCodeBlocks = str => {
const regex = /```[.\S\s]*?```/g;
let results = [];
let m = null;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) regex.lastIndex += 1;
// eslint-disable-next-line
m.forEach((match, groupIndex) => {
results.push(match);
});
}
const replacer = new RegExp(
`\`\`\`${config.language}([\\s\\S]*?)\`\`\``,
'g',
);
results = results.map(v => v.replace(replacer, '$1').trim());
return {
code: results[0],
example: results[1],
};
};
module.exports = {
capitalize,
getTextualContent,
getCodeBlocks,
optimizeNodes,
optimizeAllNodes,
getRawCodeBlocks,
};