diff --git a/src/service/print/docxHelper.js b/src/service/print/docxHelper.js index 8e70f03cd4a4717543e4fe97a33939c5eade53d9..6285500550768e3d3a1360ef7af432757988f4a5 100644 --- a/src/service/print/docxHelper.js +++ b/src/service/print/docxHelper.js @@ -1,6 +1,7 @@ var cheerio = require("cheerio"); var cheerioTableparser = require("cheerio-tableparser"); const sizeOf = require("image-size"); +const { compact } = require("lodash"); const ProgramServiceHelper = require("../../helpers/programHelper"); const programServiceHelper = new ProgramServiceHelper(); @@ -83,7 +84,6 @@ function createImageElement(src, width) { function extractTextFromElement(elem) { let rollUp = ""; if (cheerio.text(elem)) return cheerio.text(elem); - // if () else if (elem.name === "sup") return { text: elem.children[0].data, superScript: true }; else if (elem.name === "sub") @@ -94,6 +94,12 @@ function extractTextFromElement(elem) { } else { return { text: elem.children[0].data, bold: true }; } + } else if (elem.name === "span") { + if (elem.children[0].data === undefined) { + return getStyleEle(elem); + } else { + return elem.children[0].data; + } } else if (elem.name === "i") { if (elem.children[0].data === undefined) { return getStyleEle(elem); @@ -138,7 +144,31 @@ function getStyleEle(el) { return getStyleEle(el.children[0]); } else { if (el.children[0].data !== undefined) { - { + if (el.name && el.name === "u") { + return { + text: + el.children[0] && + (el.children[0].data || + (el.children[0].children[0] && el.children[0].children[0].data)), + underline: true, + }; + } else if (el.name && el.name === "i") { + return { + text: + el.children[0] && + (el.children[0].data || + (el.children[0].children[0] && el.children[0].children[0].data)), + italics: true, + }; + } else if (el.name && (el.name === "b" || el.name === "strong")) { + return { + text: + el.children[0] && + (el.children[0].data || + (el.children[0].children[0] && el.children[0].children[0].data)), + bold: true, + }; + } else { return ( el.children[0] && (el.children[0].data || @@ -148,6 +178,7 @@ function getStyleEle(el) { } } } + async function getStack(htmlString) { const stack = []; $ = cheerio.load(htmlString); @@ -206,7 +237,7 @@ async function getStack(htmlString) { } async function renderMCQ(question, questionCounter, marks) { - const questionOptions = [] + const questionOptions = []; let questionTitle; for (const [index, qo] of question.editorState.options.entries()) { let qoBody = qo.value.body; @@ -230,31 +261,25 @@ async function renderMCQ(question, questionCounter, marks) { (q.match(/<ol>/g) && q.match(/<ol>/g).length >= 1) ? await getStack(q, questionCounter) : [`${questionCounter}. ${cleanHTML(q)}`]; - + let questionOpt = []; let imageProperties = []; if (questionOptions[0] !== undefined) { - if ( questionOptions[0][0] !== undefined && typeof questionOptions[0][0] === "object" ) { - if( questionOptions[0][0].text){ - questionOpt.push( - ["I." , questionOptions[0][0].text[1]] - ); - } else{ - questionOpt.push( - ["I." , questionOptions[0][0].image] - ); + if (questionOptions[0][0].text) { + questionOpt.push(["I.", questionOptions[0][0].text[1]]); + } else { + questionOpt.push(["I.", questionOptions[0][0].image]); imageProperties.push({ width: questionOptions[0][0].width, height: questionOptions[0][0].height, }); } - } else { - questionOpt.push(["I." , questionOptions[0][0]]); + questionOpt.push(["I.", questionOptions[0][0]]); imageProperties.push({ width: 0, height: 0, @@ -267,22 +292,17 @@ async function renderMCQ(question, questionCounter, marks) { questionOptions[1][0] !== undefined && typeof questionOptions[1][0] === "object" ) { - if( questionOptions[1][0].text){ - questionOpt.push( - [ "II." ,questionOptions[1][0].text[1]] - ); - } else{ - questionOpt.push( - ["II." , questionOptions[1][0].image] - ); + if (questionOptions[1][0].text) { + questionOpt.push(["II.", questionOptions[1][0].text[1]]); + } else { + questionOpt.push(["II.", questionOptions[1][0].image]); imageProperties.push({ width: questionOptions[1][0].width, height: questionOptions[1][0].height, }); } - } else { - questionOpt.push(["II." ,questionOptions[1][0]]); + questionOpt.push(["II.", questionOptions[1][0]]); imageProperties.push({ width: 0, height: 0, @@ -295,22 +315,17 @@ async function renderMCQ(question, questionCounter, marks) { questionOptions[2][0] !== undefined && typeof questionOptions[2][0] === "object" ) { - if( questionOptions[2][0].text){ - questionOpt.push( - ["III." ,questionOptions[2][0].text[1]] - ); + if (questionOptions[2][0].text) { + questionOpt.push(["III.", questionOptions[2][0].text[1]]); } else { - questionOpt.push( - ["III." , questionOptions[2][0].image] - ); + questionOpt.push(["III.", questionOptions[2][0].image]); imageProperties.push({ width: questionOptions[2][0].width, height: questionOptions[2][0].height, }); } - } else { - questionOpt.push(["III." , questionOptions[2][0]]); + questionOpt.push(["III.", questionOptions[2][0]]); imageProperties.push({ width: 0, height: 0, @@ -323,23 +338,17 @@ async function renderMCQ(question, questionCounter, marks) { questionOptions[3][0] !== undefined && typeof questionOptions[3][0] === "object" ) { - - if( questionOptions[3][0].text){ - questionOpt.push( - ["IV." , questionOptions[3][0].text[1]] - ); + if (questionOptions[3][0].text) { + questionOpt.push(["IV.", questionOptions[3][0].text[1]]); } else { - questionOpt.push( - ["IV." , questionOptions[3][0].image] - ); + questionOpt.push(["IV.", questionOptions[3][0].image]); imageProperties.push({ width: questionOptions[3][0].width, height: questionOptions[3][0].height, }); } - } else { - questionOpt.push(["IV." ,questionOptions[3][0]]); + questionOpt.push(["IV.", questionOptions[3][0]]); imageProperties.push({ width: 0, height: 0, @@ -357,10 +366,10 @@ async function renderMCQ(question, questionCounter, marks) { Marks: marks, Language: detectLanguage(questionTitle[0]), type: "MCQ", - height1: imageProperties[0].height, - width1: imageProperties[0].width, - height2: imageProperties[1].height, - width2: imageProperties[1].width, + height1: imageProperties[0] ? imageProperties[0].height : undefined, + width1: imageProperties[0] ? imageProperties[0].width : undefined, + height2: imageProperties[1] ? imageProperties[1].height : undefined, + width2: imageProperties[1] ? imageProperties[1].width : undefined, height3: imageProperties[2] ? imageProperties[2].height : undefined, width3: imageProperties[2] ? imageProperties[2].width : undefined, height4: imageProperties[3] ? imageProperties[3].height : undefined, @@ -372,7 +381,6 @@ async function renderMCQ(question, questionCounter, marks) { async function renderQuestion(question, questionCounter, marks, Type) { let data; $ = cheerio.load(question.editorState.question); - cheerioTableparser($); var columns = $("table").parsetable(false, false, false); if (columns.length !== 0) { @@ -393,6 +401,7 @@ async function renderQuestion(question, questionCounter, marks, Type) { } else { data = [`${questionCounter}. ${cleanHTML(question.editorState.question)}`]; } + let quedata = { QuestionIndex: questionCounter, Questions: data, diff --git a/src/service/print/getdocxdata.js b/src/service/print/getdocxdata.js index d2d8e61bdd16c2796d26a01cdab5b5b6d2cf91e0..d5a9fe00944a04676d7baf3e37d1a0ffee6dd264 100644 --- a/src/service/print/getdocxdata.js +++ b/src/service/print/getdocxdata.js @@ -211,7 +211,6 @@ function create(data, paperData) { ) ); count++; - }); arr.push( new Paragraph({ @@ -258,7 +257,7 @@ function create(data, paperData) { children: [], // Just newline without text }) ); - + arr.push(mtfTableData(question)); arr.push( new Paragraph({ @@ -365,7 +364,6 @@ function MTFTabel(question) { question[0].Questions.map((item) => { arr.push( new TableRow({ - children: [displayMTFData(item.left[0]), displayMTFData(item.right[0])], }) ); @@ -375,90 +373,6 @@ function MTFTabel(question) { rows: arr, }); } -function Marks(data) { - if (data[0].Marks !== undefined) { - return new Paragraph({ - alignment: AlignmentType.RIGHT, - children: [ - new TextRun({ - text: `${data[0].Marks}`, - bold: true, - }), - ], - }); - } else { - return new Paragraph({ - alignment: AlignmentType.RIGHT, - children: [], - }); - } -} -function createFTBObject(data) { - const arr = []; - if (data.text) { - data.text - .map((text) => { - if (typeof text === "object") { - arr.push(new TextRun(text)); - } else { - arr.push( - new TextRun({ - text: `${text}`, - }) - ); - } - }) - .reduce((prev, curr) => prev.concat(curr), []); - } else if (data.image) { - if (data.image.includes("data:image/")) { - let image = getBufferImg(data.image); - return new Paragraph({ - children: [ - new ImageRun({ - data: image, - transformation: { - width: data.width, - height: data.height, - }, - }), - ], - }); - } - } else if (data.ol) { - let count1 = 0; - data.ol - .map((text) => { - count1++; - if (typeof text === "object") { - text = count1 + text.text; - arr.push( - new TextRun({ - text: `${count1}.${text}`, - }) - ); - } else { - arr.push( - new TextRun({ - text: `${count1}.${text}`, - break: 2, - }) - ); - } - }) - .reduce((prev, curr) => prev.concat(curr), []); - return new Paragraph({ - alignment: AlignmentType.LEFT, - indent: { - left: 200, - }, - children: arr, - }); - } - return new Paragraph({ - alignment: AlignmentType.LEFT, - children: arr, - }); -} function createFTB(data, count) { if (count !== 0) { @@ -547,7 +461,29 @@ function createSAObject(data, count) { break: 0.5, }) ); - // arr.push(new Paragraph({})) + } + }) + .reduce((prev, curr) => prev.concat(curr), []); + return new Paragraph({ + alignment: AlignmentType.LEFT, + indent: { + left: 200, + }, + children: arr, + }); + } else if (data.ul) { + let count1 = 0; + data.ul + .map((text) => { + if (typeof text === "object") { + arr.push(new TextRun(text)); + } else { + arr.push( + new TextRun({ + text: ` ${text}`, + break: 0.5, + }) + ); } }) .reduce((prev, curr) => prev.concat(curr), []); @@ -573,10 +509,6 @@ function imageData(image) { return (bufferImage = image.replace("data:image/jpeg;base64,", "")); } } -function getBufferData(data) { - let image = imageData(data); - return image.substr(2); -} function getBufferImg(data) { let image = imageData(data); @@ -591,7 +523,6 @@ function formatOptions(data) { optionArr.push(testimage.Option2); optionArr.push(testimage.Option3); optionArr.push(testimage.Option4); - optionArr.push(testimage.height1); optionArr.push(testimage.width1); optionArr.push(testimage.height2); @@ -664,6 +595,7 @@ const MCQborder = { color: "ffffff", }, }; + function headers(text1, text2) { return new Table({ columnWidths: [4505, 4505], @@ -780,20 +712,20 @@ function displayNumber(data) { function displayOptionsObject(data, count) { const arr = []; - + if (data.text) { - if(typeof data === "object") { + if (typeof data === "object") { arr.push(new TextRun(data)); - } else{ + } else { data.text - .map((text) => { - if (typeof text === "object") { - arr.push(new TextRun(text)); - } - }) - .reduce((prev, curr) => prev.concat(curr), []); + .map((text) => { + if (typeof text === "object") { + arr.push(new TextRun(text)); + } + }) + .reduce((prev, curr) => prev.concat(curr), []); } - + return new TableCell({ borders: MCQborder, width: { @@ -815,8 +747,8 @@ function displayOptionsObject(data, count) { }); } } + function displayOptions(option, height, width) { - if (option !== undefined) { if (typeof option[1] === "object") { return displayOptionsObject(option[1]); @@ -850,7 +782,7 @@ function displayOptions(option, height, width) { ], }); } else { - return new TableCell({ + return new TableCell({ borders: MCQborder, width: { size: 4505, @@ -896,6 +828,7 @@ function displayOptions(option, height, width) { }); } } + function displayViewData(data) { return new TableCell({ borders: MCQborder, @@ -934,6 +867,7 @@ function displayMarks(data) { children: [createSAObject(data, 0)], }); } + function formatview(data, count, questionCounter, marks) { if (count === 0) { return new Table({ @@ -994,7 +928,6 @@ function optionsTabel(testimage) { columnWidths: [4505, 4505], rows: [ new TableRow({ - children: [ displayNumber(testimage[0]),