2023-02-16 18:50:30 -05:00
|
|
|
"""This file contains an example of a gradient quirk function."""
|
2022-06-17 20:25:51 -04:00
|
|
|
import re
|
2023-02-16 18:50:30 -05:00
|
|
|
import itertools
|
2022-06-17 20:25:51 -04:00
|
|
|
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2022-06-17 20:25:51 -04:00
|
|
|
def rainbow(text):
|
|
|
|
"""Example implementation of a gradient function,
|
2022-06-21 15:50:48 -04:00
|
|
|
distributes colors over text, accounting for links,
|
|
|
|
#memos, @handles, smilies.
|
2022-06-17 20:25:51 -04:00
|
|
|
|
|
|
|
Add it as:
|
|
|
|
Regexp Replace
|
|
|
|
Regexp: ^(.*)$
|
|
|
|
Replace With: rainbow(\1)
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2022-06-17 20:25:51 -04:00
|
|
|
To customize it:
|
|
|
|
1. Copy this function.
|
|
|
|
2. Replace the hex colors in 'gradient' below with your own colors.
|
|
|
|
3. Replace 'rainbow' above and below with something more fitting :3
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2023-02-16 18:50:30 -05:00
|
|
|
You shouldn't enable the 'exclude links and smilies' option, this function
|
|
|
|
already does that!
|
|
|
|
|
2022-06-17 20:25:51 -04:00
|
|
|
There's lots of implementations of this that predate mine,
|
|
|
|
see: https://paste.0xfc.de/?e60df5a155e93583#AmcgN9cRnCcBycmVMvw6KJ1YLKPXGbaSzZLbgAhoNCQD
|
|
|
|
^ There's more useful info here too :3c
|
|
|
|
"""
|
|
|
|
# Values of 'gradient' can be any amount of hex/RGB colors.
|
2022-10-07 16:51:40 -04:00
|
|
|
gradient = [
|
|
|
|
"#ff0000",
|
|
|
|
"#ff8000",
|
|
|
|
"#ffff00",
|
|
|
|
"#80ff00",
|
|
|
|
"#00ff00",
|
|
|
|
"#00ff80",
|
|
|
|
"#00ffff",
|
|
|
|
"#0080ff",
|
|
|
|
"#0000ff",
|
|
|
|
"#8000ff",
|
|
|
|
"#ff00ff",
|
|
|
|
"#ff0080",
|
|
|
|
]
|
|
|
|
|
2022-06-17 20:25:51 -04:00
|
|
|
# Set base distribution of colors over text,
|
|
|
|
# stored as list of lists.
|
2023-02-16 18:50:30 -05:00
|
|
|
colors_and_positions = []
|
|
|
|
for color_pos, color in enumerate(gradient):
|
2022-06-17 20:25:51 -04:00
|
|
|
ratio = len(text) / len(gradient) # To account for text length.
|
2023-02-16 18:50:30 -05:00
|
|
|
colors_and_positions.append([color, round(color_pos * ratio)])
|
2022-06-17 20:25:51 -04:00
|
|
|
|
2023-02-16 18:50:30 -05:00
|
|
|
# Iterate through match objects representing all links/smilies in text,
|
2022-06-17 20:25:51 -04:00
|
|
|
# if a color tag is going to be placed within it,
|
2023-02-16 18:50:30 -05:00
|
|
|
# move its position to after the link/smilies/thingy.
|
|
|
|
match_chain = itertools.chain(
|
|
|
|
re.finditer(_urlre, text),
|
|
|
|
re.finditer(_smilere, text),
|
|
|
|
re.finditer(_memore, text),
|
|
|
|
re.finditer(_handlere, text),
|
|
|
|
)
|
|
|
|
for match in match_chain:
|
|
|
|
for color_and_position in colors_and_positions:
|
|
|
|
# color_and_position[1] is pos
|
|
|
|
if (
|
|
|
|
color_and_position[1] >= match.start()
|
|
|
|
and color_and_position[1] <= match.end()
|
|
|
|
):
|
|
|
|
# Move to 1 character after link.
|
|
|
|
color_and_position[1] = match.end() + 1
|
2022-10-07 16:51:40 -04:00
|
|
|
|
2022-06-17 20:25:51 -04:00
|
|
|
# Iterate through characters in text and write them to the output,
|
|
|
|
# if a color tag should be placed, add it before the character.
|
2022-10-07 16:51:40 -04:00
|
|
|
output = ""
|
2023-02-16 18:50:30 -05:00
|
|
|
for char_pos, char in enumerate(text):
|
2022-06-17 20:25:51 -04:00
|
|
|
# Add color if at position.
|
2023-02-16 18:50:30 -05:00
|
|
|
for color_and_position in colors_and_positions:
|
|
|
|
# color_and_position[0] is color
|
|
|
|
# color_and_position[1] is pos
|
|
|
|
if char_pos == color_and_position[1]:
|
2022-06-17 20:25:51 -04:00
|
|
|
# Add closing bracket for previous color.
|
|
|
|
output += "</c>"
|
|
|
|
# Add color
|
2023-02-16 18:50:30 -05:00
|
|
|
output += f"<c={color_and_position[0]}>"
|
2022-06-17 20:25:51 -04:00
|
|
|
# Add character.
|
2023-02-16 18:50:30 -05:00
|
|
|
output += char
|
2022-06-17 20:25:51 -04:00
|
|
|
return output
|
2022-10-07 16:51:40 -04:00
|
|
|
|
|
|
|
|
2022-06-17 20:25:51 -04:00
|
|
|
rainbow.command = "rainbow"
|
|
|
|
|
|
|
|
# These can't always be imported from their original functions,
|
|
|
|
# since those functions won't always be accessible from builds.
|
2022-06-20 00:09:14 -04:00
|
|
|
|
|
|
|
# List of smilies.
|
|
|
|
smiledict = {
|
|
|
|
":rancorous:": "pc_rancorous.png",
|
|
|
|
":apple:": "apple.png",
|
|
|
|
":bathearst:": "bathearst.png",
|
|
|
|
":cathearst:": "cathearst.png",
|
|
|
|
":woeful:": "pc_bemused.png",
|
|
|
|
":sorrow:": "blacktear.png",
|
|
|
|
":pleasant:": "pc_pleasant.png",
|
|
|
|
":blueghost:": "blueslimer.gif",
|
|
|
|
":slimer:": "slimer.gif",
|
|
|
|
":candycorn:": "candycorn.png",
|
|
|
|
":cheer:": "cheer.gif",
|
|
|
|
":duhjohn:": "confusedjohn.gif",
|
|
|
|
":datrump:": "datrump.png",
|
|
|
|
":facepalm:": "facepalm.png",
|
|
|
|
":bonk:": "headbonk.gif",
|
|
|
|
":mspa:": "mspa_face.png",
|
|
|
|
":gun:": "mspa_reader.gif",
|
|
|
|
":cal:": "lilcal.png",
|
|
|
|
":amazedfirman:": "pc_amazedfirman.png",
|
|
|
|
":amazed:": "pc_amazed.png",
|
|
|
|
":chummy:": "pc_chummy.png",
|
|
|
|
":cool:": "pccool.png",
|
|
|
|
":smooth:": "pccool.png",
|
2022-09-10 09:27:21 -04:00
|
|
|
":distraughtfirman:": "pc_distraughtfirman.png",
|
2022-06-20 00:09:14 -04:00
|
|
|
":distraught:": "pc_distraught.png",
|
|
|
|
":insolent:": "pc_insolent.png",
|
|
|
|
":bemused:": "pc_bemused.png",
|
|
|
|
":3:": "pckitty.png",
|
|
|
|
":mystified:": "pc_mystified.png",
|
|
|
|
":pranky:": "pc_pranky.png",
|
|
|
|
":tense:": "pc_tense.png",
|
|
|
|
":record:": "record.gif",
|
|
|
|
":squiddle:": "squiddle.gif",
|
|
|
|
":tab:": "tab.gif",
|
|
|
|
":beetip:": "theprofessor.png",
|
|
|
|
":flipout:": "weasel.gif",
|
|
|
|
":befuddled:": "what.png",
|
|
|
|
":pumpkin:": "whatpumpkin.png",
|
|
|
|
":trollcool:": "trollcool.png",
|
|
|
|
":jadecry:": "jadespritehead.gif",
|
|
|
|
":ecstatic:": "ecstatic.png",
|
|
|
|
":relaxed:": "relaxed.png",
|
|
|
|
":discontent:": "discontent.png",
|
|
|
|
":devious:": "devious.png",
|
|
|
|
":sleek:": "sleek.png",
|
|
|
|
":detestful:": "detestful.png",
|
|
|
|
":mirthful:": "mirthful.png",
|
|
|
|
":manipulative:": "manipulative.png",
|
|
|
|
":vigorous:": "vigorous.png",
|
|
|
|
":perky:": "perky.png",
|
|
|
|
":acceptant:": "acceptant.png",
|
|
|
|
":olliesouty:": "olliesouty.gif",
|
|
|
|
":billiards:": "poolballS.gif",
|
|
|
|
":billiardslarge:": "poolballL.gif",
|
|
|
|
":whatdidyoudo:": "whatdidyoudo.gif",
|
|
|
|
":brocool:": "pcstrider.png",
|
|
|
|
":trollbro:": "trollbro.png",
|
|
|
|
":playagame:": "saw.gif",
|
|
|
|
":trollc00l:": "trollc00l.gif",
|
|
|
|
":suckers:": "Suckers.gif",
|
|
|
|
":scorpio:": "scorpio.gif",
|
|
|
|
":shades:": "shades.png",
|
|
|
|
":honk:": "honk.png",
|
2022-10-07 16:51:40 -04:00
|
|
|
}
|
2022-06-20 00:09:14 -04:00
|
|
|
# Regular expression templates for detecting links/smilies.
|
|
|
|
_smilere = re.compile("|".join(list(smiledict.keys())))
|
2022-06-17 20:25:51 -04:00
|
|
|
_urlre = re.compile(r"(?i)(?:^|(?<=\s))(?:(?:https?|ftp)://|magnet:)[^\s]+")
|
2022-06-21 15:50:48 -04:00
|
|
|
_memore = re.compile(r"(\s|^)(#[A-Za-z0-9_]+)")
|
|
|
|
_handlere = re.compile(r"(\s|^)(@[A-Za-z0-9_]+)")
|