let topics = [ 'All', 'SoftwareSecurity', 'NetworkSecurity', 'Vulnerability', 'MemorySafety', 'WebSecurity', 'AppSecurity', 'Cryptography', 'SystemSecurity', 'PenTest' ] let topic_dd = make_dropdown("Choose a topic:", topics, "topic-dd"); let optbtn = document.getElementsByClassName("optionsbtn")[0]; let closebtn = document.getElementsByClassName("closebtn")[0]; let optionpanel = document.getElementById("option-panel"); let body = document.getElementById("content-body"); let display = document.getElementById("display"); let optboxes = document.getElementsByClassName("optbox"); let opt_dds = document.getElementsByClassName("opt-dd"); optboxes[0].innerHTML += topic_dd; topic_dd = document.getElementById("topic-dd"); topic_dd.addEventListener("change", filter_data); let filters = {}; optbtn.addEventListener("click", openNav); closebtn.addEventListener("click", closeNav); for (each of opt_dds) { each.addEventListener("change", change_filters); } filter_data(); function openNav() { optionpanel.style.width = "20vw"; display.style.width = "60vw"; for (each of optionpanel.children) { each.style.display = "block"; } } function closeNav() { optionpanel.style.width = "0"; display.style.width = "80vw"; for (each of optionpanel.children) { each.style.display = "none"; } } function change_filters(e) { filters.topic = document.getElementById("topic-dd").value; } function create_page(d) { if (d.length === 0) { body.innerHTML = "

No example satisfies All the filters.

"; } else { col1 = create_col(d.slice(0, d.length / 2)); col2 = create_col(d.slice(d.length / 2)); body.innerHTML = col1 + col2; } reflow(body); console.log("reflowed"); } function escapeHtml(html) { return html .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } function create_col(data) { res = []; for (each of data) { res.push(create_example(each)); } return `
${res.join("")}
`; } function create_example(data) { let escapedQuestion = escapeHtml(data.question); let question = make_qt(escapedQuestion); let escapedChoices = data.choices.map(choice => escapeHtml(choice)); let choices = make_choices(escapedChoices); let answer = make_answer(data.answer); html = make_box([question, choices, answer]) + "
"; return html; } function convert_latex(data) { const re = /(\$+)([^\$]*?[^\\])\1|\\begin\{tabular\}.*?\\end\{tabular\}/gs; let html = ''; let match; let lastIndex = 0; while ((match = re.exec(data)) !== null) { html += `${data.slice(lastIndex, match.index)}`; if (match[0].startsWith('\\begin{tabular}')) { const table = match[0]; const tableHtml = '...'; html += tableHtml; } else { const latex = match[2]; const formula = katex.renderToString(latex, {throwOnError: false}); html += `${formula}`; } lastIndex = match.index + match[0].length; } html += `${data.slice(lastIndex)}`; return html; } function make_qt(text) { let html = `

Question

${convert_latex(text).replace(/\\n|\n/g, "
")}

`; return html; } function make_box(contents, cls = "") { if (contents.join("").length === 0) return ""; let html = `
${contents.join(" ").replace(/\\n|\n/g, "")}
`; return html; } function make_choices(choices) { let temp = ""; let len = 0; for (each of choices) { let html = make_choice(each); temp += html; len += each.length; } let html = ""; if (len < 60) html = `

Choices

${convert_latex(temp).replace(/\\n|\n/g, "
")}
`; else html = `

Choices

${convert_latex(temp).replace(/\\n|\n/g, "
")}
`; return html; } function make_choice(choice) { let html = `
${convert_latex(choice).replace(/\\n|\n/g, "
")}
`; return html; } function make_answer(answer) { let html = `

Answer

${convert_latex(answer)}

`; return html; } function make_dropdown(label, options, id, default_ind = 0) { let html = ""; for (let i = 0; i < options.length; i++) { if (i === default_ind) html += ``; else html += ``; } html = `
`; return html; } function filter_data() { change_filters(); res = problem_data; if (filters.topic !== "All") res = res.filter(e => e.topics.includes(filters.topic)); d = _.sample(res, Math.min(100, res.length)); create_page(d); } function reflow(elt) { elt.offsetHeight; }