Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contribute 4 files to custom Boop scripts #367

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Scripts/Comment Text Block w Paragraph.js
Original file line number Diff line number Diff line change
@@ -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;
}
70 changes: 70 additions & 0 deletions Scripts/Comment Text Block.js
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 35 additions & 0 deletions Scripts/Uncomment Text Block w Paragraph.js
Original file line number Diff line number Diff line change
@@ -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(' ');
}
28 changes: 28 additions & 0 deletions Scripts/Uncomment Text Block.js
Original file line number Diff line number Diff line change
@@ -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);
}
}