/** * @license * SPDX-License-Identifier: AGPL-3.0-or-later * This file is part of Wolfree. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. */ // @ts-check export default class PodsParser { parse = ({ input = String(), i2d = Boolean(), extraPodstates = Array(), response = { queryresult: { pods: Array() } }, } = {}) => { try { return { html: this.buildHTML({ input, i2d, extraPodstates, response }) }; } catch (error) { return console.error({ error }), { error }; } }; buildHTML = ({ input = String(), i2d = Boolean(), extraPodstates = Array(), response = { queryresult: { pods: Array() } }, } = {}) => { return `
${this.parseQueryResult({ response })} ${this.buildTechnicalInfo({ input, i2d, extraPodstates, response, })}
`; }; parseQueryResult = ({ response = { queryresult: { pods: Array() } } }) => { const { queryresult } = response; const { pods } = queryresult; if (pods) { return pods.map(this.parsePod).join(""); } return ""; }; parsePod = ( pod = { title: String(), states: Array(), subpods: Array(), } ) => { return `
${this.buildPodHeader(pod)}
${pod.subpods.map(this.buildSubpod).join("")}
`; }; buildPodHeader = (pod = { title: String(), states: Array() }) => { return `

${this.escapeHTML(pod.title)}

${pod.states ? pod.states.map(this.buildSelectElement).join("") : ""}
`; }; buildSubpod = ( subpod = { img: { src: String(), alt: String() }, plaintext: String() } ) => { return `
${this.escapeHTML(subpod.img.alt)}
${this.buildSubpodDetails(subpod)}
`; }; buildSubpodDetails = (subpod = { plaintext: String() }) => { return `
${this.escapeHTML(subpod.plaintext)}

`; }; buildSelectElement = (state = { value: String(), states: Array() }) => { if (state.states) { return ` `; } return ""; }; buildOption = (state = { name: String() }) => { return ` `; }; buildTechnicalInfo = ({ input = String(), i2d = Boolean(), extraPodstates = Array(), response = { queryresult: { pods: Array() } }, }) => { return `

Technical information

${this.buildTechnicalInfoDetails({ input, i2d, extraPodstates, response, })}
`; }; buildTechnicalInfoDetails = ({ input = String(), i2d = Boolean(), extraPodstates = Array(), response = { queryresult: { pods: Array() } }, }) => { return `
If you have programming knowledge, feel free to explore the technical information provided below:
${this.buildTextarea({ input, i2d, extraPodstates, response })}
`; }; buildTextarea = ({ input = String(), i2d = Boolean(), extraPodstates = Array(), response = { queryresult: { pods: Array() } }, }) => { return ` `; }; escapeHTML = (unsafe = String()) => { return unsafe .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }; } // Can I escape HTML special chars in JavaScript? - Stack Overflow // https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript // test case: // https://www.wolframalpha.com/input?i=solve+%7By%27%28x%29+%3D+-2+y%2C+y%280%29%3D1%7D+from+0+to+10+using+r+k+f+algorithm