Skip to content

Commit

Permalink
IRC formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
LaineZ committed Oct 23, 2023
1 parent 6737e7b commit f235b76
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
45 changes: 45 additions & 0 deletions static/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,58 @@ function showErrorModal(message) {
a.play();
}

function renderIRCFormatting(text) {
text = text.replace(/\x03(\d{1,2}(,\d{1,2})?)?/g, function(match, color) {
if (color) {
return `<span style="color: ${getHTMLColorFromIRC(color)};">`;
} else {
return '</span>';
}
});

text = text.replace(/\x0F/g, '</span>');
text = text.replace(/\x02(.*?)\x02/g, '<strong>$1</strong>');
text = text.replace(/\x1D(.*?)\x1D/g, '<em>$1</em>');

return text;
}

function autolinkText(text) {
const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$])/gi;
return text.replace(urlRegex, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>');
}

function getHTMLColorFromIRC(ircColor) {
const ircColors = [
'var(--fg)', '#000000', '#00007F', '#009300', '#ff0000', 'var(--error-bg)', '#9C009C', '#FC7F00', '#FFFF00',
'#00FC00', '#009393', '#00FFFF', '#0000FC', '#FF00FF', '#7F7F7F', '#D2D2D2'
];

const colorCodes = ircColor.split(',').map(Number);
let htmlColor = "var(--fg)";

if (colorCodes.length === 1) {
htmlColor = `${ircColors[colorCodes[0]]}`;
} else if (colorCodes.length === 2) {
htmlColor = `${ircColors[colorCodes[1]]}`;
}

return htmlColor;
}

function colorize() {
const nicks = document.querySelectorAll(".from");
nicks.forEach(element => {
const hashCode = hashCodeFromString(element.textContent);
const color = getColorIndex(hashCode);
element.style.color = colorPalette[color];
});

const messages = document.querySelectorAll(".text");
messages.forEach(element => {
element.innerHTML = autolinkText(element.textContent);
element.innerHTML = renderIRCFormatting(element.innerHTML);
});
}

function getColorIndex(hashCode) {
Expand Down
4 changes: 4 additions & 0 deletions static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ a {
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

h1 {
margin: 0;
padding: 0;
Expand Down

0 comments on commit f235b76

Please sign in to comment.