Skip to content

Instantly share code, notes, and snippets.

@KenDB3
Last active November 10, 2019 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KenDB3/792f5f0cf0d164bbc40d to your computer and use it in GitHub Desktop.
Save KenDB3/792f5f0cf0d164bbc40d to your computer and use it in GitHub Desktop.
//colorize-text.js -by- KenDB3 of KD3net (http://bbs.kd3.us)
//This script is meant for use with Synchronet BBSes.
//It requires JSexec.exe (from SBBS) to be run from command line.
//It was written in the spirit of the 1991 program A2A - ASCII to ANSI file converter by David Kauffman A.K.A. Ansi.
//colorize-text.js takes a plain text file and adds Synchronet Ctrl-A colorization.
//If you prefer ANSI color encoding, you could convert the output file with the SBBS utility asc2ans.exe.
//Usage:
//Pick a color and intensity for Text, a color and intensity for Numbers, and a color and intensity for Non-Text Characters
//Add the colorize-text.js script to your mods (/sbbs/mods/) or your exec (/sbbs/exec/) directory.
//Command Line: (be sure you are in the correct directory to call jsexec.exe, ie /sbbs/exec/)
//jsexec.exe colorize-text.js input-plain-text output-ctrl-a-ascii color1 H/L color2 H/L color3 H/L
//---All options are required---
//Input & Output files
// input-plain-text should be a plain text file with no ANSI or any other color codes (ctrl-a, pipe, wildcat, etc...)
// output-ctrl-a-ascii will be a Control-A encoded ASCII file (Synchronet Color Codes)
//Colors are one letter and can be any of the following:
// Red=R, Green=G, Yellow=Y, Blue=B, Magenta=M, Cyan=C, White=W
// color1 is for TEXT: A-Z, a-z
// color2 is for Numbers: 0-9
// color3 is for Non-Text Characters: .,:;-() etc...
//H/L is for High or Low Intensity ANSI Colors
// This should also be one letter for each of the 3
// H - High Intensity (Bright) Color
// L - Low Intensity (Dull) Color
//Example Command Line - Low Intensity Cyan Text, High Intensity Cyan Numbers, High Intensity Blue Non-Text
// jsexec.exe colorize-text.js C:\sbbs\text\riweathr.msg C:\sbbs\text\riweathr.asc C L C H B H
//See this in use on my BBS here:
// http://bbs.kd3.us/weather/weather.ssjs
//DISCLAIMER: This script is just meant to add Ctrl-A encoding. I wrote it to be used with JSexec.exe,
//but, it could possibly be used by other JS scripts. I do not expect it to mangle any of your text, but
//if it does, I do not take any responsibility. PLEASE have a backup copy of your file BEFORE using this
//script, just in case.
//Big thanks to digital man, I did not know half of this stuff was possible until I saw his JS script
//called sbbsecho_upgrade.js (http://cvs.synchro.net/cgi-bin/viewcvs.cgi/exec/sbbsecho_upgrade.js?view=log).
//It showed me the basics I needed to make this script do what it does.
//Make sure we have enough arguments to handle the conversion
if(argc < 8) {
alert(format("!!Not Enough Arguments!!! \r\n" +
"Format should look like this: \r\n" +
"jsexec.exe colorize-text.js input output color1 H/L color2 H/L color3 H/L\r\n\r\n" +
"Colors are one letter: \r\nRed=R, Green=G, Yellow=Y, Blue=B, Magenta=M, Cyan=C, White=W\r\n" +
" color1 is for TEXT: A-Z, a-z\r\n" +
" color2 is for Numbers: 0-9\r\n" +
" color3 is for Non-Text Characters: .,:;-() etc...\r\n" +
"H/L is for High or Low Intensity ANSI Colors\r\n" +
" This should also be one letter for each of the 3\r\n" +
" The H/L argument will affect the color that preceeds it on the command line\r\n" +
" H - High Intensity (Bright) Color\r\n" +
" L - Low Intensity (Dull) Color\r\n", errno, myInputText));
exit();
}
var myInputText = argv[0];
var myOutputTextCtrlA = argv[1];
var myTextColorCode = argv[2];
var myTextIntensity = argv[3];
var myNumColorCode = argv[4];
var myNumIntensity = argv[5];
var mySymbolColorCode = argv[6];
var mySymbolIntensity = argv[7];
//make sure color1 (Text) is valid
if (myTextColorCode.length != 1 || myTextColorCode.search(/^R|^G|^Y|^B|^M|^C|^W/i)) {
alert(format("(color1 alert) Colors are one letter and can be any of the following: \r\nRed=R, Green=G, Yellow=Y, Blue=B, Magenta=M, Cyan=C, White=W\r\n", errno, myInputText));
exit();
}
//make sure H/L for color1 (Text) is valid
if (myTextIntensity.length != 1 || myTextIntensity.search(/^H|^L/i)) {
alert(format("(H/L 1 alert) H/L is for High or Low Intensity ANSI Colors: \r\n This should be a one letter\r\n H - High Intensity (Bright) Color\r\n L - Low Intensity (Dull) Color\r\n", errno, myInputText));
exit();
} else if (myTextIntensity == 'H') {
myTextIntensity = '\001h\001';
} else {
myTextIntensity = '\001n\001';
}
//make sure color2 (Numbers) is valid
if (myNumColorCode.length != 1 || myNumColorCode.search(/^R|^G|^Y|^B|^M|^C|^W/i)) {
alert(format("(color2 alert) Colors are one letter and can be any of the following: \r\nRed=R, Green=G, Yellow=Y, Blue=B, Magenta=M, Cyan=C, White=W\r\n", errno, myInputText));
exit();
}
//make sure H/L for color2 (Numbers) is valid
if (myNumIntensity.length != 1 || myNumIntensity.search(/^H|^L/i)) {
alert(format("(H/L 2 alert) H/L is for High or Low Intensity ANSI Colors: \r\n This should be a one letter\r\n H - High Intensity (Bright) Color\r\n L - Low Intensity (Dull) Color\r\n", errno, myInputText));
exit();
} else if (myNumIntensity == 'H') {
myNumIntensity = '\001h\001';
} else {
myNumIntensity = '\001n\001';
}
//make sure color3 (Symbols) is valid
if (mySymbolColorCode.length != 1 || mySymbolColorCode.search(/^R|^G|^Y|^B|^M|^C|^W/i)) {
alert(format("(color3 alert) Colors are one letter and can be any of the following: \r\nRed=R, Green=G, Yellow=Y, Blue=B, Magenta=M, Cyan=C, White=W\r\n", errno, myInputText));
exit();
}
//make sure H/L for color3 (Symbols) is valid
if (mySymbolIntensity.length != 1 || mySymbolIntensity.search(/^H|^L/i)) {
alert(format("(H/L 3 alert) H/L is for High or Low Intensity ANSI Colors: \r\n This should be a one letter\r\n H - High Intensity (Bright) Color\r\n L - Low Intensity (Dull) Color\r\n", errno, myInputText));
exit();
} else if (mySymbolIntensity == 'H') {
mySymbolIntensity = '\001h\001';
} else {
mySymbolIntensity = '\001n\001';
}
//Our input file
var file = new File(myInputText);
//Does the file open? Or are you messing with me?
if(!file.open("r")) {
alert(format("Error %d opening %s\n", errno, myInputText));
exit();
}
var colormytext = file.readAll(4096);
var line_num;
var line_num_out;
//RegEx to find and replace the text
for(line_num in colormytext) {
colormytext[line_num] = colormytext[line_num].replace(/[a-z,A-Z]+/g, myTextIntensity + myTextColorCode + '$&');
colormytext[line_num] = colormytext[line_num].replace(/\d+/g, myNumIntensity + myNumColorCode + '$&');
//I couldn't use \W because it was catching the Ctrl-A (SOH) character and messing everything up.
//So use ^\w instead and add in the \001 (SOH) as part of the negation
colormytext[line_num] = colormytext[line_num].replace(/[^\w\001]+/g, mySymbolIntensity + mySymbolColorCode + '$&');
}
var file = new File(myOutputTextCtrlA);
//Does the new file open?
if(!file.open("w")) {
alert(format("Error %d opening %s\n", errno, myOutputTextCtrlA));
exit();
}
for(line_num_out in colormytext) {
var lineout = colormytext[line_num_out];
file.writeln(lineout);
}
file.close();
print(myInputText + " successfully converted to " + myOutputTextCtrlA);
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment