free-programming-books-parser/index.js

294 lines
10 KiB
JavaScript
Raw Normal View History

2021-11-05 00:29:01 +00:00
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const remark = require("remark");
const languages = require("./languages");
const commandLineArgs = require("command-line-args");
const optionDefinitions = [
{ name: "input", multiple: true, defaultValue: ["./fpb/books"] },
{ name: "output", defaultValue: "./parser/fpb.json" },
];
2021-10-07 18:56:44 +00:00
2021-10-22 19:04:23 +00:00
const excludes = [
"README.md",
"CONTRIBUTING.md",
"CODE_OF_CONDUCT.md",
"SUMMARY.md",
];
2021-10-07 18:56:44 +00:00
// TODO!!
/**
* Summary TBD.
*
2021-10-07 18:56:44 +00:00
* Desciption TBD.
*
2021-10-07 18:56:44 +00:00
* @param {Object} listItem - a listItem in AST format defined by remark-parse
*
2021-10-07 18:56:44 +00:00
* @return {Object} Returns an Object containing details about the piece of media Exact format TBD.
*/
let parseListItem = function (listItem) {
let stripParens = function (s) {
if (s.slice(0,1) === "(" && s.slice(-1) === ")")
return s.slice(1,-1);
return s;
}
2021-10-16 19:57:23 +00:00
let entry = {};
let s = ""; // If we need to build up a string over multiple listItem elements
const [link, ...otherStuff] = listItem; // head of listItem = url, the rest is "other stuff"
2021-10-16 19:57:23 +00:00
entry.url = link.url;
entry.title = link.children[0].value;
// remember to get OTHER STUFF!! remember there may be multiple links!
for (let i of otherStuff) {
if (s === "") { // this is almost always, except for when we are parsing a multi-element note
if (i.type === "text" && i.value.slice(0,3) === " - ") {
// author found
let parenIndex = i.value.indexOf("(");
if (parenIndex === -1) {
entry.author = i.value.slice(3).trim();
}
else {
entry.author = i.value.slice(3, parenIndex).trim(); // go from " - " until the first "("
}
}
if (i.type === "emphasis" &&
i.children[0].value.slice(0, 1) === "(" && i.children[0].value.slice(-1) === ")") {
// access notes found (currently assumes exactly one child, so far this is always the case)
entry.accessNotes = i.children[0].value.slice(1, -1);
}
if (i.type === "link") {
// other links found
if (entry.otherLinks === undefined) entry.otherLinks = [];
entry.otherLinks.push({title: stripParens(i.children[0].value), url: i.url});
// entry.otherLinks = [...entry.otherLinks, {title: i.children[0].value, url: i.url}]; // <-- i wish i could get this syntax to work with arrays
}
if (i.type === "text" && i.value.indexOf("(") !== -1) {
// notes found (currently assumes no nested parentheses)
if (entry.notes === undefined) entry.notes = [];
let leftParen = i.value.indexOf("(");
let rightParen = i.value.indexOf(")", leftParen);
if (rightParen === -1) {
// there must be some *emphasis* found
s += i.value.slice(leftParen);
} else {
entry.notes.push(i.value.slice(leftParen + 1, rightParen));
}
// also TODO: if theres more than one disjoint set of parens
}
} else { // for now we assume that all previous ifs are mutually exclusive with this, may polish later
if (i.type === "emphasis") {
// this is the emphasis, add it in boldface and move on
s += "*" + i.children[0].value + "*";
} else if (i.type === "link") {
// something has gone terribly wrong.
entry.manualReviewRequired = true;
break;
} else {
// hopefully this is the end of the note
let rightParen = i.value.indexOf(")");
if (rightParen === -1) {
// we have to go AGAIN
s += i.value;
} else {
// finally, we have reached the end of the note
entry.notes.push(stripParens(s + i.value.slice(0, rightParen + 1)));
s = "";
// TODO: there can be a normal note following a bold-containing note
}
}
}
}
2021-10-16 19:57:23 +00:00
return entry;
};
2021-10-07 18:56:44 +00:00
2021-10-22 19:04:23 +00:00
// from free-programming-books-lint
function getLangFromFilename(filename) {
const dash = filename.lastIndexOf("-");
const dot = filename.lastIndexOf(".");
let lang = filename.slice(dash + 1, dot).replace(/_/, "-");
2021-10-22 19:04:23 +00:00
if (!languages.hasOwnProperty(lang)) {
if (/^[a-z]{2}$/.test(lang) || /^[a-z]{2}-[A-Z]{2}$/.test(lang)) {
return "";
}
lang = "en-US";
2021-10-22 19:04:23 +00:00
}
return lang;
2021-10-22 19:04:23 +00:00
}
// from free-programming-books-lint
function getFilesFromDir(dir) {
return fs
.readdirSync(dir)
.filter(
(file) =>
path.extname(file) === ".md" && excludes.indexOf(file) === -1
)
.map((file) => path.join(dir, file));
2021-10-22 19:04:23 +00:00
}
function getMediaFromDirectory(dir) {
const slash = dir.lastIndexOf("/");
2021-10-22 19:04:23 +00:00
let mediaType = dir.slice(2, slash);
return mediaType;
}
let parseMarkdown = function (doc) {
2021-10-22 19:04:23 +00:00
let tree = remark.parse(doc).children;
let sections = []; // This will go into root object later
2021-11-05 17:31:29 +00:00
let errors = [];
2021-11-05 19:31:51 +00:00
let currentDepth = 3; // used to determine if the last heading was an h4 or h3
2021-10-07 18:56:44 +00:00
2021-10-22 19:04:23 +00:00
// find where Index ends
2021-10-14 16:37:21 +00:00
// probably could be done better, review later
let i = 0,
count = 0;
for (i; i < tree.length; i++) {
if (tree[i].type == "heading" && tree[i].depth == "3") count++;
if (count == 2) break;
2021-10-14 16:37:21 +00:00
}
2021-10-22 19:04:23 +00:00
tree.slice(i).forEach((item) => {
// Start iterating after Index
try {
if (item.type == "heading" && item.children[0].value == "Index")
return;
if (item.type == "heading") {
if (item.depth == 3) {
// Heading is an h3
currentDepth = 3;
let newSection = {
section: item.children[0].value, // Get the name of the section
entries: [],
subsections: [],
};
sections.push(newSection); // Push the section to the output array
} else if (item.depth == 4) {
// Heading is an h4
currentDepth = 4;
let newSubsection = {
section: item.children[0].value, // Get the name of the subsection
entries: [],
};
sections[sections.length - 1].subsections.push(
newSubsection
); // Add to subsection array of most recent h3
2021-10-07 18:56:44 +00:00
}
} else if (item.type == "list") {
item.children.forEach((listItem) => {
let content = listItem.children[0].children; // gets array containing a remark-link and a remark-paragraph
// if(content[0].type !== 'link'){ // SKIPS OVER bad formatting
// return;
// }
if (currentDepth == 3) {
let contentJson = parseListItem(content);
sections[sections.length - 1].entries.push(contentJson); // add the entry to most recent h3
} else if (currentDepth == 4) {
let lastSection = sections.length - 1;
let lastSubSec =
sections[lastSection].subsections.length - 1;
let contentJson = parseListItem(content);
sections[lastSection].subsections[
lastSubSec
].entries.push(contentJson); // add entry to most recent h4
}
});
}
2021-10-28 21:34:49 +00:00
} catch (e) {
// if there was an error while parsing, print the error to an error log
// looks really ugly, maybe try to refine output later
2021-11-10 21:57:33 +00:00
let errStart = JSON.stringify(item.position.start.line)
let errEnd = JSON.stringify(item.position.end.line)
str = `Error at line ${errStart} - line ${errEnd}.`
errors.push(str);
2021-10-28 21:34:49 +00:00
}
2021-10-22 19:04:23 +00:00
});
2021-11-10 21:57:33 +00:00
return {sections: sections, errors: errors};
};
2021-10-22 19:04:23 +00:00
function parseDirectory(directory) {
2021-11-05 19:31:51 +00:00
let dirChildren = []; // this will hold the output each markdown doc
2021-11-10 21:57:33 +00:00
let dirErrors = []; //contains error for a given directory
2021-10-22 19:04:23 +00:00
let mediaType = getMediaFromDirectory(directory);
const filenames = getFilesFromDir(path.resolve(directory));
filenames.forEach((filename) => {
2021-11-05 18:27:14 +00:00
const doc = fs.readFileSync(filename);
2021-11-10 21:57:33 +00:00
let { sections, errors } = parseMarkdown(doc); // parse the markdown document
2021-10-22 19:04:23 +00:00
const langCode = getLangFromFilename(filename);
2021-11-10 21:57:33 +00:00
// Entries
2021-10-22 19:04:23 +00:00
let docJson = {
language: {
code: langCode,
name: languages[langCode],
},
index: {},
sections: sections,
2021-10-22 19:04:23 +00:00
};
dirChildren.push(docJson);
2021-11-10 21:57:33 +00:00
// Errors
if (errors.length !== 0) {
let docErrors = {
file: path.basename(filename),
errors: errors
}
dirErrors.push(docErrors);
}
2021-10-22 19:04:23 +00:00
});
2021-11-10 21:57:33 +00:00
// File entries
2021-10-22 19:04:23 +00:00
let dirJson = {
type: mediaType,
index: {},
children: dirChildren,
2021-10-22 19:04:23 +00:00
};
2021-11-10 21:57:33 +00:00
// Errors
return {dirJson: dirJson, dirErrors: dirErrors};
2021-10-22 19:04:23 +00:00
}
function parseAll(directories) {
2021-11-05 19:31:51 +00:00
let rootChildren = []; // this will hold the output of each directory
2021-11-10 21:57:33 +00:00
let rootErrors = [];
2021-10-22 19:04:23 +00:00
directories.forEach((directory) => {
2021-11-10 21:57:33 +00:00
let { dirJson, dirErrors } = parseDirectory(directory);
2021-10-22 19:04:23 +00:00
rootChildren.push(dirJson);
2021-11-10 21:57:33 +00:00
if (dirErrors.length !== 0) {
rootErrors.push({directory: path.basename(directory), files: dirErrors});
}
2021-10-22 19:04:23 +00:00
});
2021-11-10 21:57:33 +00:00
// ALl entries
2021-10-22 19:04:23 +00:00
let rootJson = {
type: "root",
children: rootChildren,
};
2021-11-10 21:57:33 +00:00
// Errors
let allErrors = {
type: "root",
directories: rootErrors
};
fs.writeFileSync(output, JSON.stringify(rootJson, null, 3), function (err) {
2021-10-22 19:04:23 +00:00
if (err) {
console.log(err);
}
});
2021-11-10 21:57:33 +00:00
fs.writeFileSync('./parser/fpb.log', JSON.stringify(allErrors, null, 3), function(err) {
if(err){
console.log(err);
}
});
2021-10-07 18:56:44 +00:00
}
console.time("Parse Time");
let { input, output } = commandLineArgs(optionDefinitions);
parseAll(input);
console.timeEnd("Parse Time");