From e26d97d46a2b02afedc6a9d29a1593a8475f55fd Mon Sep 17 00:00:00 2001 From: "Bennie, the Shepherd" <118771676+benniefolyfe@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:06:27 -0700 Subject: [PATCH 1/6] Add files via upload Four files to help developers turn blocks of text (with/without paragraph breaks) into commented text blocks of max 66 characters, and vice versa. --- Comment Text Block w Paragraph.js | 80 +++++++++++++++++++++++++++++ Comment Text Block.js | 70 +++++++++++++++++++++++++ Uncomment Text Block w Paragraph.js | 35 +++++++++++++ Uncomment Text Block.js | 28 ++++++++++ 4 files changed, 213 insertions(+) create mode 100644 Comment Text Block w Paragraph.js create mode 100644 Comment Text Block.js create mode 100644 Uncomment Text Block w Paragraph.js create mode 100644 Uncomment Text Block.js diff --git a/Comment Text Block w Paragraph.js b/Comment Text Block w Paragraph.js new file mode 100644 index 00000000..9ac3f409 --- /dev/null +++ b/Comment Text Block w Paragraph.js @@ -0,0 +1,80 @@ +/** + { + "api": 1, + "name": "Commented Text Block w/ Paragraph", + "description": "Converts a block of text into a commented text block with max length of 66 characters and line breaks before or after words.", + "author": "Your Name", + "icon": "quote", + "tags": "comment,text,line break" + } +**/ + +function main(state) { + try { + // Extract the text from the state + const text = state.fullText; + + // Split the text into paragraphs + const paragraphs = text.split('\n\n'); + + // Process each paragraph separately + const commentedParagraphs = paragraphs.map(processParagraph); + + // Join the paragraphs with paragraph breaks and assign the commented text back to the state.text property + state.text = commentedParagraphs.join('\n\n'); + } catch (error) { + state.postError("Something went wrong while processing the text: " + error.message); + } +} + +function processParagraph(paragraph) { + // Split the paragraph into words + const words = paragraph.split(/\s+/); + + // Create commented lines with line breaks before or after words + const commentedLines = createCommentedLines(words); + + // Join the lines with line breaks + const commentedParagraph = commentedLines.join('\n'); + + return commentedParagraph; +} + +function createCommentedLines(words) { + const lines = []; + let currentLine = ''; + let lineLength = 0; + + for (const word of words) { + const wordLength = word.length; + + // Check if adding the word would exceed the character limit + if (lineLength + wordLength + 3 > 66) { // 3 accounts for comment markers and space + + // Add the current line to the list + lines.push(currentLine); + + // Reset the line for the next line + currentLine = ''; + lineLength = 0; + } + + // Add the word to the current line + if (currentLine !== '') { + currentLine += ' '; + lineLength++; + } + currentLine += word; + lineLength += wordLength; + } + + // Add the last line to the list + if (currentLine !== '') { + lines.push(currentLine); + } + + // Add comment markers to each line + const commentedLines = lines.map(line => `// ${line}`); + + return commentedLines; +} diff --git a/Comment Text Block.js b/Comment Text Block.js new file mode 100644 index 00000000..7c1ad057 --- /dev/null +++ b/Comment Text Block.js @@ -0,0 +1,70 @@ +/** + { + "api": 1, + "name": "Commented Text Block", + "description": "Converts a block of text into a commented text block with max length of 66 characters and line breaks before or after words.", + "author": "Your Name", + "icon": "quote", + "tags": "comment,text,line break" + } +**/ + +function main(state) { + try { + // Extract the text from the state + const text = state.fullText; + + // Split the text into words + const words = text.split(/\s+/); + + // Create commented lines with line breaks before or after words + const commentedLines = createCommentedLines(words); + + // Join the lines with line breaks + const commentedText = commentedLines.join('\n'); + + // Assign the commented text back to the state.text property + state.text = commentedText; + } catch (error) { + state.postError("Something went wrong while processing the text: " + error.message); + } +} + +function createCommentedLines(words) { + const lines = []; + let currentLine = ''; + let lineLength = 0; + + for (const word of words) { + const wordLength = word.length; + + // Check if adding the word would exceed the character limit + if (lineLength + wordLength + 3 > 66) { // 3 accounts for comment markers and space + + // Add the current line to the list + lines.push(currentLine); + + // Reset the line for the next line + currentLine = ''; + lineLength = 0; + } + + // Add the word to the current line + if (currentLine !== '') { + currentLine += ' '; + lineLength++; + } + currentLine += word; + lineLength += wordLength; + } + + // Add the last line to the list + if (currentLine !== '') { + lines.push(currentLine); + } + + // Add comment markers to each line + const commentedLines = lines.map(line => `// ${line}`); + + return commentedLines; +} diff --git a/Uncomment Text Block w Paragraph.js b/Uncomment Text Block w Paragraph.js new file mode 100644 index 00000000..c9187f07 --- /dev/null +++ b/Uncomment Text Block w Paragraph.js @@ -0,0 +1,35 @@ +/** + { + "api": 1, + "name": "Uncomment Text w/ Paragraphs", + "description": "Removes comment markers and restores the commented text to a block of text while maintaining paragraph breaks.", + "author": "benniefolyfe", + "icon": "quote", + "tags": "uncomment,text,paragraph breaks" + } +**/ + +function main(state) { + try { + // Extract the text from the state + const text = state.fullText; + + // Split the text into paragraphs + const commentedParagraphs = text.split('\n\n'); + + // Process each commented paragraph separately + const restoredParagraphs = commentedParagraphs.map(restoreParagraph); + + // Join the paragraphs with paragraph breaks and assign the restored text back to the state.text property + state.text = restoredParagraphs.join('\n\n'); + } catch (error) { + state.postError("Something went wrong while processing the text: " + error.message); + } +} + +function restoreParagraph(commentedParagraph) { + // Remove comment markers and leading whitespace from each line + const lines = commentedParagraph.split('\n').map(line => line.replace(/\/\/\s?/g, '').trim()); + + return lines.join(' '); +} diff --git a/Uncomment Text Block.js b/Uncomment Text Block.js new file mode 100644 index 00000000..8ddc081a --- /dev/null +++ b/Uncomment Text Block.js @@ -0,0 +1,28 @@ +/** + { + "api": 1, + "name": "Uncomment and Restore", + "description": "Removes comment markers and restores the commented text to a cohesive paragraph.", + "author": "benniefolyfe", + "icon": "quote", + "tags": "uncomment,text" + } +**/ + +function main(state) { + try { + // Extract the text from the state + const text = state.fullText; + + // Remove comment markers and leading whitespace from each line + const uncommentedLines = text.split('\n').map(line => line.replace(/\/\/\s?/g, '').trim()); + + // Join the lines into a cohesive paragraph + const uncommentedText = uncommentedLines.join(' '); + + // Assign the uncommented text back to the state.text property + state.text = uncommentedText; + } catch (error) { + state.postError("Something went wrong while processing the text: " + error.message); + } +} From 2784459c917fed6e31419bfcb0ca62eee8aaeaab Mon Sep 17 00:00:00 2001 From: "Bennie, the Shepherd" <118771676+benniefolyfe@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:08:15 -0700 Subject: [PATCH 2/6] Rename Comment Text Block w Paragraph.js to Script/Comment Text Block w Paragraph.js --- .../Comment Text Block w Paragraph.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Comment Text Block w Paragraph.js => Script/Comment Text Block w Paragraph.js (100%) diff --git a/Comment Text Block w Paragraph.js b/Script/Comment Text Block w Paragraph.js similarity index 100% rename from Comment Text Block w Paragraph.js rename to Script/Comment Text Block w Paragraph.js From dcccdb753bc7b068fba4baaf9e217f330a2a237f Mon Sep 17 00:00:00 2001 From: "Bennie, the Shepherd" <118771676+benniefolyfe@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:08:29 -0700 Subject: [PATCH 3/6] Rename Comment Text Block w Paragraph.js to Comment Text Block w Paragraph.js --- {Script => Scripts}/Comment Text Block w Paragraph.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {Script => Scripts}/Comment Text Block w Paragraph.js (100%) diff --git a/Script/Comment Text Block w Paragraph.js b/Scripts/Comment Text Block w Paragraph.js similarity index 100% rename from Script/Comment Text Block w Paragraph.js rename to Scripts/Comment Text Block w Paragraph.js From df7b822e3617e57d85a33eb361239e2a00b7d5f4 Mon Sep 17 00:00:00 2001 From: "Bennie, the Shepherd" <118771676+benniefolyfe@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:08:42 -0700 Subject: [PATCH 4/6] Rename Comment Text Block.js to Scripts/Comment Text Block.js --- Comment Text Block.js => Scripts/Comment Text Block.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Comment Text Block.js => Scripts/Comment Text Block.js (100%) diff --git a/Comment Text Block.js b/Scripts/Comment Text Block.js similarity index 100% rename from Comment Text Block.js rename to Scripts/Comment Text Block.js From 67a76efd15bc9727e7a6a5fc58626d3e285e8ddf Mon Sep 17 00:00:00 2001 From: "Bennie, the Shepherd" <118771676+benniefolyfe@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:08:58 -0700 Subject: [PATCH 5/6] Rename Uncomment Text Block w Paragraph.js to Scripts/Uncomment Text Block w Paragraph.js --- .../Uncomment Text Block w Paragraph.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Uncomment Text Block w Paragraph.js => Scripts/Uncomment Text Block w Paragraph.js (100%) diff --git a/Uncomment Text Block w Paragraph.js b/Scripts/Uncomment Text Block w Paragraph.js similarity index 100% rename from Uncomment Text Block w Paragraph.js rename to Scripts/Uncomment Text Block w Paragraph.js From 66922176e877a8eeaea8089d5bbb99b15eb7c25b Mon Sep 17 00:00:00 2001 From: "Bennie, the Shepherd" <118771676+benniefolyfe@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:09:17 -0700 Subject: [PATCH 6/6] Rename Uncomment Text Block.js to Scripts/Uncomment Text Block.js --- Uncomment Text Block.js => Scripts/Uncomment Text Block.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Uncomment Text Block.js => Scripts/Uncomment Text Block.js (100%) diff --git a/Uncomment Text Block.js b/Scripts/Uncomment Text Block.js similarity index 100% rename from Uncomment Text Block.js rename to Scripts/Uncomment Text Block.js