Skip to content

Commit

Permalink
Fix Docx
Browse files Browse the repository at this point in the history
  • Loading branch information
bi1101 committed May 16, 2024
1 parent 53e9f28 commit 6224304
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 57 deletions.
46 changes: 20 additions & 26 deletions Assets/js/docx_export.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Script Name: Docx Export
* Version: 1.0.5
* Last Updated: 07-12-2023
* Version: 1.0.7
* Last Updated: 16-5-2024
* Author: bi1101
* Description: Export the result page as docx files with comments.
*/
Expand Down Expand Up @@ -61,10 +61,14 @@ function createSectionsWithComments(rawComments) {
.map((p) => p.trimStart());
const outputParagraphs = [];

// Filter out faulty comments before processing
const validComments = rawComments.filter(comment =>
essayText.toLowerCase().includes(comment.originalVocab.toLowerCase())
);

// Add the essay prompt paragraphs to the output
for (let promptParagraph of essayPromptParagraphs) {
if (promptParagraph.trim()) {
// Check if paragraph is not just whitespace
outputParagraphs.push(
new docx.Paragraph({
children: [
Expand All @@ -78,53 +82,43 @@ function createSectionsWithComments(rawComments) {
}
}

let globalCommentIndex = 0; // Initialize global comment index for valid comments

for (let paraText of essayParagraphs) {
if (paraText.trim()) {
// Check if paragraph is not just whitespace
let currentPosition = 0;
const paraChildren = [];
let localCommentIndex = 0; // Reset for each paragraph

while (localCommentIndex < rawComments.length) {
while (globalCommentIndex < validComments.length) {
const commentStartPos = paraText
.toLowerCase()
.indexOf(
rawComments[localCommentIndex].originalVocab.toLowerCase(),
validComments[globalCommentIndex].originalVocab.toLowerCase(),
currentPosition
);

if (commentStartPos !== -1) {
// Add text before the comment
const preCommentText = paraText.slice(
currentPosition,
commentStartPos
);
const preCommentText = paraText.slice(currentPosition, commentStartPos);
paraChildren.push(new docx.TextRun(preCommentText));

// Add the comment
paraChildren.push(new docx.CommentRangeStart(localCommentIndex));
paraChildren.push(new docx.CommentRangeStart(globalCommentIndex));
paraChildren.push(
new docx.TextRun(rawComments[localCommentIndex].originalVocab)
new docx.TextRun(validComments[globalCommentIndex].originalVocab)
);
paraChildren.push(new docx.CommentRangeEnd(localCommentIndex));
paraChildren.push(new docx.CommentRangeEnd(globalCommentIndex));
paraChildren.push(
new docx.TextRun({
children: [new docx.CommentReference(localCommentIndex)]
children: [new docx.CommentReference(globalCommentIndex)]
})
);

currentPosition =
commentStartPos +
rawComments[localCommentIndex].originalVocab.length;

localCommentIndex++;
currentPosition = commentStartPos + validComments[globalCommentIndex].originalVocab.length;
globalCommentIndex++; // Move to next comment
} else {
console.warn(
`Skipped raw comment at index ${localCommentIndex} because it was not found in the essay text.`
);
// If no comment is found in the current paragraph, move on to the next comment
localCommentIndex++;
continue;
// Break out of the loop if the current comment is not found in this paragraph
break;
}
}

Expand Down
50 changes: 22 additions & 28 deletions Assets/js/docx_export_speaking.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Script Name: Docx Export for Speaking
* Version: 1.0.0
* Last Updated: 04-12-2023
* Version: 1.0.1
* Last Updated: 16-05-2024
* Author: bi1101
* Description: Export the result page as docx files with comments.
*/
Expand Down Expand Up @@ -52,58 +52,52 @@ function convertRawCommentsToDocxFormat(rawComments) {

function createSectionsWithComments(rawComments) {
const essayText = document.querySelector("#my-text").innerText;

const essayParagraphs = essayText.split(/\\r?\\n/).map((p) => p.trimStart());

const essayParagraphs = essayText.split(/[\r\n]+/).map((p) => p.trimStart());

const outputParagraphs = [];

// Filter out faulty comments before processing
const validComments = rawComments.filter(comment =>
essayText.toLowerCase().includes(comment.originalVocab.toLowerCase())
);

let globalCommentIndex = 0; // Initialize global comment index for valid comments

for (let paraText of essayParagraphs) {
if (paraText.trim()) {
// Check if paragraph is not just whitespace
let currentPosition = 0;
const paraChildren = [];
let localCommentIndex = 0; // Reset for each paragraph

while (localCommentIndex < rawComments.length) {
while (globalCommentIndex < validComments.length) {
const commentStartPos = paraText
.toLowerCase()
.indexOf(
rawComments[localCommentIndex].originalVocab.toLowerCase(),
validComments[globalCommentIndex].originalVocab.toLowerCase(),
currentPosition
);

if (commentStartPos !== -1) {
// Add text before the comment
const preCommentText = paraText.slice(
currentPosition,
commentStartPos
);
const preCommentText = paraText.slice(currentPosition, commentStartPos);
paraChildren.push(new docx.TextRun(preCommentText));

// Add the comment
paraChildren.push(new docx.CommentRangeStart(localCommentIndex));
paraChildren.push(new docx.CommentRangeStart(globalCommentIndex));
paraChildren.push(
new docx.TextRun(rawComments[localCommentIndex].originalVocab)
new docx.TextRun(validComments[globalCommentIndex].originalVocab)
);
paraChildren.push(new docx.CommentRangeEnd(localCommentIndex));
paraChildren.push(new docx.CommentRangeEnd(globalCommentIndex));
paraChildren.push(
new docx.TextRun({
children: [new docx.CommentReference(localCommentIndex)]
children: [new docx.CommentReference(globalCommentIndex)]
})
);

currentPosition =
commentStartPos +
rawComments[localCommentIndex].originalVocab.length;

localCommentIndex++;
currentPosition = commentStartPos + validComments[globalCommentIndex].originalVocab.length;
globalCommentIndex++; // Move to next comment
} else {
console.warn(
`Skipped raw comment at index ${localCommentIndex} because it was not found in the essay text.`
);
// If no comment is found in the current paragraph, move on to the next comment
localCommentIndex++;
continue;
// Break out of the loop if the current comment is not found in this paragraph
break;
}
}

Expand Down
6 changes: 3 additions & 3 deletions writify-gform-openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Writify
* Description: Score IELTS Essays x GPT
* Version: 1.2.0
* Version: 1.2.1
* Author: IELTS Science
* Copyright: © 2023-2026 RLT
*/
Expand Down Expand Up @@ -223,7 +223,7 @@ function writify_enqueue_scripts()

// Enqueue the script only if the slug starts with 'result'
if (substr($slug, 0, 6) === 'result') {
wp_enqueue_script('writify-docx-export', plugin_dir_url(__FILE__) . 'Assets/js/docx_export.js', array('jquery'), '1.0.6', true);
wp_enqueue_script('writify-docx-export', plugin_dir_url(__FILE__) . 'Assets/js/docx_export.js', array('jquery'), '1.0.7', true);
// Enqueue Docx script
wp_enqueue_script('docx', 'https://unpkg.com/[email protected]/build/index.js', array(), null, true);
// Enqueue FileSaver script
Expand Down Expand Up @@ -265,7 +265,7 @@ function writify_enqueue_scripts()
// Enqueue the script only if the slug starts with 'speaking-result'
if (substr($slug, 0, 15) === 'speaking-result') {
// Enqueue necessary scripts
wp_enqueue_script('writify-docx-export', plugin_dir_url(__FILE__) . 'Assets/js/docx_export_speaking.js', array('jquery'), '1.0.0', true);
wp_enqueue_script('writify-docx-export', plugin_dir_url(__FILE__) . 'Assets/js/docx_export_speaking.js', array('jquery'), '1.0.1', true);
// Enqueue Docx script
wp_enqueue_script('docx', 'https://unpkg.com/[email protected]/build/index.js', array(), null, true);
// Enqueue FileSaver script
Expand Down

0 comments on commit 6224304

Please sign in to comment.