Skip to content

Commit

Permalink
Improve chat markdown and regex filters
Browse files Browse the repository at this point in the history
  • Loading branch information
out-of-phaze committed Jun 3, 2024
1 parent b3aa0ca commit 844329c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions code/modules/chat_filter/_chat_filter.dm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var/global/list/chat_modifiers_in_use
var/list/all_filters = decls_repository.get_decls_of_type(/decl/chat_filter)
for(var/filtertype in all_filters)
var/decl/chat_filter/chat_filter = all_filters[filtertype]
if(!chat_filter.disabled && chat_filter.filter_category != chat_filter.type)
if(!chat_filter.disabled)
if(chat_filter.can_deny_message)
global.chat_blockers_in_use += chat_filter
if(chat_filter.can_modify_message)
Expand All @@ -31,10 +31,10 @@ var/global/list/chat_modifiers_in_use
. = chat_filter.replace(., match)

/decl/chat_filter
abstract_type = /decl/chat_filter
var/name
var/disabled
var/summary
var/filter_category = /decl/chat_filter
var/can_modify_message = FALSE
var/can_deny_message = FALSE

Expand Down
5 changes: 3 additions & 2 deletions code/modules/chat_filter/_chat_filter_regex.dm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/decl/chat_filter/regexp
filter_category = /decl/chat_filter/regexp
abstract_type = /decl/chat_filter/regexp
var/regex/filter_regex

/decl/chat_filter/regexp/match(var/message)
. = filter_regex && findtext(message, filter_regex)
filter_regex.index = 0 // we use the global flag, so we need to reset this for every match or else repeat messages will break
. = filter_regex && filter_regex.Find(message)
10 changes: 5 additions & 5 deletions code/modules/chat_filter/filter_markdown.dm
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/decl/chat_filter/regexp/markdown
filter_category = /decl/chat_filter/regexp/markdown
abstract_type = /decl/chat_filter/regexp/markdown
can_modify_message = TRUE
var/format_char
var/format_replace_proc

/decl/chat_filter/regexp/markdown/Initialize()
. = ..()
filter_regex = regex("([format_char])(.+?)([format_char])", "g")
filter_regex = regex("([REGEX_QUOTE(format_char)])(.+?)([REGEX_QUOTE(format_char)])", "g")

/decl/chat_filter/regexp/markdown/replace(var/message, var/match)
. = filter_regex.Replace(message, format_replace_proc)

/proc/chatFilterRegexBold(full_match, prefix_char, message_body, suffix_char)
. = "<b>[message_body]</b>"
. = "<b>[trim(message_body)]</b>"

/decl/chat_filter/regexp/markdown/bold
name = "Bold"
summary = "Applies <b>bold</b> to speech and emote text that is surrounded by *asterisks*."
format_char = "\\*"
format_char = "*"
format_replace_proc = /proc/chatFilterRegexBold

/proc/chatFilterRegexItalic(full_match, prefix_char, message_body, suffix_char)
. = "<i>[message_body]</i>"
. = "<i>[trim(message_body)]</i>"

/decl/chat_filter/regexp/markdown/italic
name = "Italics"
Expand Down

0 comments on commit 844329c

Please sign in to comment.