OpoTests/Public/ecma/Snippets/TemariosEnPDF.snippet.js

132 lines
4.9 KiB
JavaScript

"use strict";
export const TemariosEnPDF = (function(){
/**
* @callback temarios_en_pdf_preload_callback
* @param {?HTMLElement} item
* @returns {void}
*/
/**
* @constructs TemariosEnPDF
* @returns {void}
* @access private
* @static
*/
const TemariosEnPDF = function(){};
/**
* @param {!string} selector
* @param {!temarios_en_pdf_preload_callback} callback
* @returns {void}
* @access public
* @static
*/
TemariosEnPDF.preload = (selector, callback) => {
/** @type {number} */
let interval = setInterval(() => {
/** @type {?HTMLElement} */
const item = document.querySelector(selector);
if(item){
clearInterval(interval);
callback(item);
};
}, 100);
};
/**
* @param {!string} html
* @returns {string}
* @access public
* @static
*/
TemariosEnPDF.load_tests_from_txt_body = () => {
TemariosEnPDF.preload("body", body => {
/** @type {[string, string]} */
const [questions_box, results_box] = body.innerHTML.split(/[\r\n](?:PREGUNTAS TIPO TEST|PLANTILLA DE CONTESTACIÓN|CLAVES DE CONTESTACIÓN)[\r\n]/ig).filter((_, i) => i % 2),
/** @type {string[]} */
results = results_box.split(/[\r\n]+/).filter(line => /^[0-9]+[\s.-]*[A-Z]$/i.test(line)).map(line => line.replace(/^[0-9]+[\s.-]*/i, "").trim()),
/** @type {Array<{origin: string, sources: string[], title: string, queries: Array<{question: string, rights: string[], wrongs: string[]}>}>} */
data = [];
/** @type {string} */
let subject,
/** @type {string} */
group,
/** @type {number} */
i = -1,
/** @type {number} */
j,
/** @type {number|null} */
k = null,
/** @type {number} */
m;
questions_box.replace(/(?:(?:(?!(?:página\s+[0-9]+))(?:.|[\r\n]+))+)página\s+[0-9]\s*[\r\n]+/i, "").replace(/^www\.[^\r\n]+[\r\n]+©[^\r\n]+[\r\n]+/igm, "").replace(/^(?:[0-9]+\.\-\s*([^\r\n]+)|([a-z])\)\s*([^\r\n]+)|([^\r\n]+))/img, (_, question, identifier, answer, subtitle) => {
if(subtitle){
if(subtitle.toUpperCase() == subtitle){
if(subject){
if(j == -1)
data[i].title += " " + subtitle.trim();
else{
group = subtitle.trim();
data.push({
/** @type {string} */
origin : "temariosenpdf.es",
/** @type {Array.<string>} */
sources : ["https://temariosenpdf.es/archivos/50_Test_LOPD_3.pdf"],
/** @type {string} */
title : subject + " - " + group,
/** @type {Array.<Object.<string, string|Array.<string>>>} */
queries : []
});
i ++;
j = -1;
};
}else
subject = subtitle.trim();
}else{
if(k)
data[i].queries[j][k][m] = (data[i].queries[j][k][m] + " " + subtitle.trim()).replace(/\- /g, "");
else
data[i].queries[j].question = (data[i].queries[j].question + " " + subtitle.trim()).replace(/\- /g, "");
};
}else if(question){
k = null;
data[i].queries.push({
/** @type {string} */
question: question.trim(),
/** @type {Array.<string>} */
rights : [],
/** @type {Array.<string>} */
wrongs : []
});
j ++;
}else if(answer){
m = data[i].queries[j][k = results[j] == identifier ? "rights" : "wrongs"].length;
data[i].queries[j][k].push(answer.trim());
};
});
console.log(JSON.stringify(data));
});
};
TemariosEnPDF.load_direct_tests_from_html = () => {
TemariosEnPDF.preload("body", body => {
const data = [];
body.innerHTML.split(/[\r\n]CLAVES DE RESPUESTAS[\r\n]/i)[1].split(/[\r\n]+/).forEach(line => {});
});
};
return TemariosEnPDF;
}());