Fix some snippets, start creating scripts

This commit is contained in:
Angelos Chalaris
2019-02-02 13:19:06 +02:00
parent a732143542
commit 9f3ff79952
8 changed files with 1012 additions and 2 deletions

59
scripts/extract.js Normal file
View File

@ -0,0 +1,59 @@
const fs = require("fs-extra")
const path = require("path")
const chalk = require("chalk")
const {
attempt,
readSnippets,
getCodeBlocks,
getSection,
getTitle,
getTextualContent
} = require("./util");
console.time("Extractor");
attempt("snippet_data.json generation", () => {
const output = Object.entries(readSnippets()).map(([name, contents]) => {
const title = getTitle(contents);
const text = getTextualContent(contents);
const codeBlocks = getCodeBlocks(contents);
const notes = getSection("#### Notes", contents, false)
.split("\n")
.map(v => v.replace(/[*-] /g, ""))
.filter(v => v.trim() !== "")
// const answer = getSection("#### Answer", contents)
// const goodToHear = getSection("#### Good to hear", contents, false)
// .split("\n")
// .map(v => v.replace(/[*-] /g, ""))
// .filter(v => v.trim() !== "")
// const links = getSection("##### Additional links", contents)
// .split("\n")
// .filter(v =>
// /(\/\*[\w\'\s\r\n\*]*\*\/)|(\/\/[\w\s\']*)|(\<![\-\-\s\w\>\/]*\>)/.test(
// v
// )
// )
// .map(v => v.replace(/[*-] /g, ""))
// .filter(v => v.trim() !== "" && !v.includes("tags"))
return {
name,
title,
text,
codeBlocks,
expertise: parseInt(
(contents.match(/<!--\s*expertise:\s*\(*(.+)\)*/) || [])[1],
10
),
tags: (contents.match(/<!--\s*tags:\s*\(*(.+)\)*\s*-->/) || [])[1].split(",").map(v => v.trim()),
notes
}
})
fs.writeFileSync("./data/snippet_data.json", JSON.stringify(output, null, 2))
})
console.log(`${chalk.green("SUCCESS!")} snippet_data.json file generated!`);
console.timeEnd("Extractor");

95
scripts/util.js Normal file
View File

@ -0,0 +1,95 @@
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");
const SNIPPETS_PATH = "./snippets";
const attempt = (task, cb) => {
try {
return cb();
} catch (e) {
console.log(`${chalk.red("ERROR!")} During ${task}: ${e}`);
process.exit(1);
return null;
}
};
const capitalize = ([first, ...rest], lowerRest = false) =>
first.toUpperCase() + (lowerRest ? rest.join("").toLowerCase() : rest.join(""));
const readSnippets = () =>
attempt("read snippets", () =>
fs
.readdirSync(SNIPPETS_PATH)
.sort((a, b) => (a.toLowerCase() < b.toLowerCase() ? -1 : 1))
.reduce((acc, name) => {
acc[name] = fs
.readFileSync(path.join(SNIPPETS_PATH, name), "utf8")
.replace(/\r\n/g, "\n");
return acc;
}, {})
);
const getCodeBlocks = str => {
const regex = /```[.\S\s]*?```/g;
const results = [];
let m = null;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex += 1;
}
m.forEach(match => results.push(match));
}
return results;
}
const getSection = (searchString, contents, includeSubsections = true) => {
const indexOfSearch = contents.indexOf(searchString);
if (indexOfSearch < 0) return "";
let endSearch = "\\n#"
if (includeSubsections) {
let i;
for (i = 0; searchString[i] === "#" && i < searchString.length; i++);
if (i > 0) {
endSearch += `{${i - 1},${i}}[^#]`;
}
}
const endRegex = new RegExp(endSearch);
const sliceStart = indexOfSearch + searchString.length + 1;
const endIndex = contents.slice(sliceStart).search(endRegex);
const sliceEnd = endIndex === -1 ? undefined : endIndex + sliceStart;
return contents.slice(sliceStart, sliceEnd).trim();
}
const getTextualContent = str => {
const regex = /###.*\n*([\s\S]*?)```/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);
});
}
return results[1];
};
const getTitle = (contents) =>
contents.split('\n')[0].replace(/^#+\s+/g,'');
module.exports = {
attempt,
readSnippets,
SNIPPETS_PATH,
capitalize,
getTextualContent,
getCodeBlocks,
getSection,
getTitle
};