Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Some text manipulation refactor and fixes (Aurorastation#17683)
Browse files Browse the repository at this point in the history
* sdfsad

* sdfsda

* fourspaces begone
  • Loading branch information
FluffyGhoster authored Nov 13, 2023
1 parent 2f355a1 commit 605b104
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 19 deletions.
1 change: 1 addition & 0 deletions aurorastation.dme
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#include "code\__defines\subsystem-priority.dm"
#include "code\__defines\targeting.dm"
#include "code\__defines\technomancer.dm"
#include "code\__defines\text.dm"
#include "code\__defines\tgs.dm"
#include "code\__defines\tgui.dm"
#include "code\__defines\time.dm"
Expand Down
4 changes: 4 additions & 0 deletions code/__defines/_macros.dm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#define SPAN_VOTE(X) ("<span class='vote'>" + X + "</span>")
#define SPAN_HEAR(X) ("<span class='hear'>" + X + "</span>")

#define SPAN_RED(x) "<span style='color:[COLOR_RED]'>[x]</span>"
#define SPAN_YELLOW(x) "<span style='color:[COLOR_YELLOW]'>[x]</span>"
#define SPAN_GREEN(x) "<span style='color:[COLOR_GREEN]'>[x]</span>"

#define SPAN_SIZE(size, text) ("<span style=\"font-size: [size]\">" + text + "</span>")

#define SPAN_HIGHDANGER(X) (FONT_LARGE(SPAN_DANGER(X)))
Expand Down
3 changes: 1 addition & 2 deletions code/__defines/misc.dm
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,7 @@ example:
#define CONTAINER_EMPTY 0
#define CONTAINER_SINGLE 1
#define CONTAINER_MANY 2
//Misc text define. Does 4 spaces. Used as a makeshift tabulator.
#define FOURSPACES "&nbsp;&nbsp;&nbsp;&nbsp;"

#define CLIENT_FROM_VAR(I) (ismob(I) ? I:client : (isclient(I) ? I : (istype(I, /datum/mind) ? I:current?:client : null)))

// check_items/check_reagents/check_fruits return values
Expand Down
19 changes: 19 additions & 0 deletions code/__defines/text.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// Macro from Lummox used to get height from a MeasureText proc
#define WXH_TO_HEIGHT(x) text2num(copytext(x, findtextEx(x, "x") + 1))

#define SMALL_FONTS(FONTSIZE, MSG) "<span style=\"font-family: 'Small Fonts'; -dm-text-outline: 1 black; font-size: [FONTSIZE]px;\">[MSG]</span>"


//Since we do not have GLOB (yet), this will have to do
var/regex/html_tags = regex(@"<.*?>", "g")
var/regex/angular_brackets = regex(@"[<>]", "g")
var/regex/filename_forbidden_chars = regex(@{""|[\\\n\t/?%*:|<>]|\.\."}, "g")

/// Removes characters incompatible with file names.
#define SANITIZE_FILENAME(text) (filename_forbidden_chars.Replace(text, ""))

/// Simply removes the < and > characters, and limits the length of the message.
#define STRIP_HTML_SIMPLE(text, limit) (angular_brackets.Replace(copytext(text, 1, limit), ""))

/// Removes everything enclose in < and > inclusive of the bracket, and limits the length of the message.
#define STRIP_HTML_FULL(text, limit) (html_tags.Replace(copytext(text, 1, limit), ""))
53 changes: 43 additions & 10 deletions code/_helpers/text.dm
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
#define SMALL_FONTS(FONTSIZE, MSG) "<span style=\"font-family: 'Small Fonts'; -dm-text-outline: 1 black; font-size: [FONTSIZE]px;\">[MSG]</span>"

/// Macro from Lummox used to get height from a MeasureText proc
#define WXH_TO_HEIGHT(x) text2num(copytext(x, findtextEx(x, "x") + 1))

#define SPAN_RED(x) "<span style='color:[COLOR_RED]'>[x]</span>"
#define SPAN_YELLOW(x) "<span style='color:[COLOR_YELLOW]'>[x]</span>"
#define SPAN_GREEN(x) "<span style='color:[COLOR_GREEN]'>[x]</span>"

/*
* Holds procs designed to help with filtering text
* Contains groups:
Expand All @@ -31,7 +22,49 @@
* Text sanitization
*/

//Used for preprocessing entered text
/**
* Same as `strip_html_full`, but readds the newlines after the stripping
*
* Not SQL escaping safe, do not use alone for SQL sanitizing operations
*
* Returns a string
*/
/proc/strip_html_readd_newlines(text, limit = MAX_MESSAGE_LEN)
SHOULD_NOT_SLEEP(TRUE)
SHOULD_BE_PURE(TRUE)
//For some reason at the first replace the backslash doubles, go figure... Either way, this is how to match it
var/static/regex/regex_newlines = regex(@"(\\n)", "g")
//And this matches the two newlines after the HTML strip and encoding
var/static/regex/regex_newlines_readd = regex(@"(##)", "g")

//We make them double, so we know the # is not part of the special ascii encoding of characters
var/message = regex_newlines.Replace(text, @"\n\n")

//Strip the HTML from the message
message = strip_html_full(message, limit)

//Bring the newlines back
return regex_newlines_readd.Replace(message, "<br>")

/// Runs STRIP_HTML_SIMPLE and sanitize.
/proc/strip_html(text, limit = MAX_MESSAGE_LEN)
return sanitize_tg(STRIP_HTML_SIMPLE(text, limit))

/// Runs STRIP_HTML_FULL and sanitize.
/proc/strip_html_full(text, limit = MAX_MESSAGE_LEN)
return sanitize_tg(STRIP_HTML_FULL(text, limit))

/// Runs byond's html encoding sanitization proc, after replacing new-lines and tabs for the # character.
/// This is ported from tg, hence the name
/proc/sanitize_tg(text)
var/static/regex/regex = regex(@"[\n\t]", "g")
return html_encode(regex.Replace(text, "#"))

/**
* DEPRECATED, USE `strip_html_full` / `strip_html` / `sanitize_tg` / `strip_html_readd_newlines` WHERE POSSIBLE
*
* Used for preprocessing entered text
*/
/proc/sanitize(var/input, var/max_length = MAX_MESSAGE_LEN, var/encode = 1, var/trim = 1, var/extra = 1)
if(!input)
return
Expand Down
10 changes: 5 additions & 5 deletions code/datums/uplink/announcements.dm
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
desc = "Causes a falsified [current_map.boss_name] Update. Triggers immediately after supplying additional data."

/datum/uplink_item/abstract/announcements/fake_centcom/extra_args(var/mob/user)
var/title = sanitize(input("Enter your announcement title.", "Announcement Title") as null|text)
var/title = tgui_input_text(user, "Enter your announcement title.", "Announcement Title", encode = FALSE)
if(!title)
return
var/message = sanitize(input("Enter your announcement message.", "Announcement Title") as null|message)
var/message = tgui_input_text(user, "Enter your announcement message.", "Announcement Title", multiline = TRUE, encode = FALSE)
if(!message)
return
return list("title" = title, "message" = message)
return list("title" = strip_html_readd_newlines(title), "message" = strip_html_readd_newlines(message))

/datum/uplink_item/abstract/announcements/fake_centcom/get_goods(var/obj/item/device/uplink/U, var/loc, var/mob/user, var/list/args)
command_announcement.Announce(args["message"], args["title"], do_newscast=1, do_print=1)
return 1
command_announcement.Announce(args["message"], args["title"], do_newscast=1, do_print=1, msg_sanitized=TRUE)
return TRUE

/datum/uplink_item/abstract/announcements/fake_crew_arrival
name = "Crew Arrival Announcement/Records"
Expand Down
2 changes: 1 addition & 1 deletion code/defines/procs/announce.dm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

if(!msg_sanitized)
message = sanitize(message, extra = 0)
message_title = sanitizeSafe(message_title)
message_title = sanitizeSafe(message_title)

var/msg = FormMessage(message, message_title)
for(var/mob/M in player_list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@
if(!P || (P.program_state == PROGRAM_STATE_KILLED && P.service_state == PROGRAM_STATE_KILLED))
return
if(P.focused_conv)
P.focused_conv.cl_send(P, text, M)
P.focused_conv.cl_send(P, html_decode(text), M)
registered_message = text

/obj/item/modular_computer/examine(mob/user, distance, is_adjacent)
Expand Down
45 changes: 45 additions & 0 deletions html/changelogs/fluffyghost-fixantagannouncements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
################################
# Example Changelog File
#
# Note: This file, and files beginning with ".", and files that don't end in ".yml" will not be read. If you change this file, you will look really dumb.
#
# Your changelog will be merged with a master changelog. (New stuff added only, and only on the date entry for the day it was merged.)
# When it is, any changes listed below will disappear.
#
# Valid Prefixes:
# bugfix
# wip (For works in progress)
# tweak
# soundadd
# sounddel
# rscadd (general adding of nice things)
# rscdel (general deleting of nice things)
# imageadd
# imagedel
# maptweak
# spellcheck (typo fixes)
# experiment
# balance
# admin
# backend
# security
# refactor
#################################

# Your name.
author: FluffyGhost

# Optional: Remove this file after generating master changelog. Useful for PR changelogs that won't get used again.
delete-after: True

# Any changes you've made. See valid prefix list above.
# INDENT WITH TWO SPACES. NOT TABS. SPACES.
# SCREW THIS UP AND IT WON'T WORK.
# Also, all entries are changed into a single [] after a master changelog generation. Just remove the brackets when you add new entries.
# Please surround your changes in double quotes ("), as certain characters otherwise screws up compiling. The quotes will not show up in the changelog.
changes:
- rscadd: "Added text manipulation macros from TG, and a new one to have newlines working."
- refactor: "Moved around some macros in the appropriate files."
- bugfix: "Antag uplink announcements now shows apostrophes correctly, and can have newlines in them."
- refactor: "Antag uplink announcements now uses tgui inputs."
- bugfix: "PDA STT now respects the apostrophes and other characters too."

0 comments on commit 605b104

Please sign in to comment.