MA0301/tools/discrete-extraction-tools/scripts/boolean-algebra/outer.js

164 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2021-05-17 00:31:21 +02:00
console.log("boolean-algebra/outer.js injected successfully!");
const injectInnerScript = () => {
const s = document.createElement('script');
s.src = chrome.runtime.getURL('scripts/boolean-algebra/inner.js');
(document.head || document.documentElement).appendChild(s);
}
const replaceParens = (parens, counterParens, replacements, text) => {
parPattern = RegExp(`${parens[0]}.*${parens[1]}`);
while (parPattern.test(text)) {
const match = RegExp(parens[0], 'g').exec(text);
let i = match.index + match[0].length;
let parCount = 1;
while (parCount != 0) {
if (text[i] == counterParens[0]) parCount++;
if (text[i] == counterParens[1]) parCount--;
i++;
}
const secondPar = parens[1].replace(/\\(?!\\)/g, '').replace(/\\\\/,'\\');
text = text.substring(0, match.index)
+ replacements[0] + text.substring(match.index + match[0].length, i - secondPar.length)
+ replacements[1] + text.substring(i);
}
return text;
}
const parseExpression = (expression) => {
expression = replaceParens(["!\\(", "\\)"], ["(", ")"], ["\\overline{", "}"], expression);
expression = expression.replace(/!([A-Za-z01])/g, '\\overline{$1}');
return expression;
}
const insertExpression = (expression) => {
localStorage.setItem('boolean-expression', parseExpression(expression));
injectInnerScript();
}
const parseLatex = resultArray => {
const lines = [];
lines.push('\\begin{array}{ccl}');
for ([latex, comment] of resultArray) {
let l = latex;
l = replaceParens(['\\\\class{[-\\w]+}{', '}'], ['{', '}'], ['',''], l);
const lawTable = [
["Distributive", "Distributive law"],
["Demorgan", "Demorgan theorem"],
["Involution", "Involution law"],
["Distribution", "Distributive law"],
["Identity", "Identity law"],
["Complement", "Complement law"],
["Associative", "Associative law"],
["Idempotent", "Associative law"],
["Absorption", "Absorption law"],
];
let c = '&\\qquad&';
for (law of lawTable) {
if (comment.includes(law[0]))
c = ` &\\qquad& \\text{${law[1]}}`;
}
lines.push(`${l}${c} \\\\`);
}
lines.push('\\end{array}');
return lines.join('\n');
}
const whileFindReplace = (pattern, replacement, text) => {
while (pattern.test(text))
text = text.replace(pattern, replacement);
return text;
}
const convertToPropositionalLogic = latex => {
// replace and
const ignoreCommands = ['(?<=[\\s\\+])', '(?=[\\s\\+])']
const char = '(?!\\\\text|&\\\\quad&|\\s|\\+)([A-Z0-9]|\\\\overline{.+?})';
const connectedChars2 = RegExp(`${ignoreCommands[0]}${char.repeat(2)}${ignoreCommands[1]}`)
const connectedChars3 = RegExp(`${ignoreCommands[0]}${char.repeat(3)}${ignoreCommands[1]}`)
const connectedChars4 = RegExp(`${ignoreCommands[0]}${char.repeat(4)}${ignoreCommands[1]}`)
const connectedChars5 = RegExp(`${ignoreCommands[0]}${char.repeat(5)}${ignoreCommands[1]}`)
latex = whileFindReplace(connectedChars5, '($1 \\wedge $2 \\wedge $3 \\wedge $4 \\wedge $5)', latex);
latex = whileFindReplace(connectedChars4, '($1 \\wedge $2 \\wedge $3 \\wedge $4)', latex);
latex = whileFindReplace(connectedChars3, '($1 \\wedge $2 \\wedge $3)', latex);
latex = whileFindReplace(connectedChars2, '($1 \\wedge $2)', latex);
// replace neg
latex = replaceParens(['\\\\overline{', '}'], ['{', '}'], ['\\neg (', ')'], latex);
latex = whileFindReplace(/\\neg \(((?:\\neg )*[A-Za-z])\)/, '\\neg $1', latex);
2021-05-17 00:31:21 +02:00
// // replace or
latex = latex.replace(/\+/g, ' \\vee ');
return latex;
}
const convertToSetTheory = latex => {
// replace and
const ignoreCommands = ['(?<=[\\s\\+])', '(?=[\\s\\+])']
const char = '(?!\\\\text|&\\\\quad&|\\s|\\+)([A-Z0-9]|\\\\overline{.+?})';
const connectedChars2 = RegExp(`${ignoreCommands[0]}${char.repeat(2)}${ignoreCommands[1]}`)
const connectedChars3 = RegExp(`${ignoreCommands[0]}${char.repeat(3)}${ignoreCommands[1]}`)
const connectedChars4 = RegExp(`${ignoreCommands[0]}${char.repeat(4)}${ignoreCommands[1]}`)
const connectedChars5 = RegExp(`${ignoreCommands[0]}${char.repeat(5)}${ignoreCommands[1]}`)
latex = whileFindReplace(connectedChars5, '($1 \\cap $2 \\cap $3 \\cap $4 \\cap $5)', latex);
latex = whileFindReplace(connectedChars4, '($1 \\cap $2 \\cap $3 \\cap $4)', latex);
latex = whileFindReplace(connectedChars3, '($1 \\cap $2 \\cap $3)', latex);
latex = whileFindReplace(connectedChars2, '($1 \\cap $2)', latex);
2021-05-17 00:31:21 +02:00
// // replace or
latex = latex.replace(/\+/g, ' \\cup ');
return latex;
}
const getLatex = () => {
return parseLatex(JSON.parse(localStorage.getItem('boolean-result')));
}
const timeout = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const calculateExpression = async (expression) => {
console.log("Inserting expression");
insertExpression(expression);
await timeout(2);
return getLatex();
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("Checking Message Type");
if (message.type !== "bool_alg") return;
calculateExpression(message.content.expression)
.then(latex => sendResponse({
content: message.content.outputType === 'logic' ? convertToPropositionalLogic(latex) :
message.content.outputType === 'set' ? convertToSetTheory(latex) :
latex
}));
return true;
});