chat-client/scripts/formatting.js

227 lines
4.7 KiB
JavaScript

// This function was copied from https://stackoverflow.com/a/4835406/5302652
function escapeHtml(text) {
var map = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#039;'
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
// Get a list of links sent to the user
var sent_chat_links = [];
function open_link_id(id)
{
// Open the saved link
open(sent_chat_links[id]);
}
function is_url(text) {
return /^(ftp|http|https):\/\/[^ "]+$/.test(text);
}
function fixInt(num, size)
{
// Convert the number to a string
num = num.toString()
// Loop over the number until its big enough
while(num.length < size)
{
// Add a zero to the start
num = "0" + num;
}
// Return the number
return num;
}
function text_to_html(text)
{
// Make the text XSS safe
text = escapeHtml(text);
// Split the text into an array
text = text.split(" ");
// Some varibles for remembering parts of the string
var underline_check = false;
var strikethrough_check = false;
var italic_check = false;
var bold_check = false;
var code_check = false;
var ignore_check = 0;
var last_word = "";
// Create an out varible
var out = "";
// Loop over the rows
for(var r=0;r<text.length;r++)
{
// Is this a URL
if(is_url(text[r]))
{
// Push the link to the array
sent_chat_links.push(text[r]);
var id = sent_chat_links.length-1;
// Display a link
out += "<a href='#' onclick='open_link_id("+id+")'>"+text[r]+"</a>";
}
else
{
// Loop over the text
for(var i=0;i<text[r].length;i++)
{
// Is the ignore check in place
if(ignore_check > 0)
{
// Remove 1
ignore_check -= 1;
// Add the next character, ignoring the formatting
out += text[r][i];
}
else
{
// Is the text a backslash (ignore)
if(text[r][i] == "\\" && !code_check)
{
// Set ignore check
ignore_check = 1;
}
// Is the text a dash (underline)
else if(text[r][i] == "-" && !code_check)
{
// Invert the varible
underline_check = !underline_check;
if(underline_check)
{
// Open the underline element
out += "<u>";
}
else
{
// Close the underline element
out += "</u>";
}
}
// Is the text a star (bold)
else if(text[r][i] == "*" && !code_check)
{
// Invert the varible
bold_check = !bold_check;
if(bold_check)
{
// Open the bold element
out += "<b>";
}
else
{
// Close the bold element
out += "</b>";
}
}
// Is the text an underline (italic)
else if(text[r][i] == "_" && !code_check)
{
// Invert the varible
italic_check = !italic_check;
if(italic_check)
{
// Open the bold element
out += "<i>";
}
else
{
// Close the bold element
out += "</i>";
}
}
// Is the text a ~ (strikethrough)
else if(text[r][i] == "~" && !code_check)
{
// Invert the varible
strikethrough_check = !strikethrough_check;
if(strikethrough_check)
{
// Open the strikethrough element
out += "<del>";
}
else
{
// Close the strikethrough element
out += "</del>";
}
}
// Is this a newline
else if(text[r][i] == "\n")
{
// Add a break
out += "<br/>";
}
// Is this a code marker
else if(text[r][i] == "`")
{
// Invert the varible
code_check = !code_check;
if(code_check)
{
// Open the code element
out += "<code>";
}
else
{
// Close the code element
out += "</code>";
}
}
else
{
out += text[r][i];
}
}
}
}
// Add a space
out += " ";
}
// Close some unclosed elements
if(bold_check) out += "</b>";
if(italic_check) out += "</i>";
if(underline_check) out += "</u>";
if(strikethrough_check) out += "</del>";
if(code_check) out += "</code>";
// Send out back
return out;
}