Chain iterators in gradient.py and add comments to quirks/ scripts
This commit is contained in:
parent
129d26532f
commit
ab614f636e
2 changed files with 41 additions and 33 deletions
|
@ -1,28 +1,33 @@
|
||||||
|
"""Default quirk functions that are always included with Pesterchum."""
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
def upperrep(text):
|
def upperrep(text: str):
|
||||||
|
"""Returns 'text' as uppercase."""
|
||||||
return text.upper()
|
return text.upper()
|
||||||
|
|
||||||
|
|
||||||
upperrep.command = "upper"
|
upperrep.command = "upper"
|
||||||
|
|
||||||
|
|
||||||
def lowerrep(text):
|
def lowerrep(text: str):
|
||||||
|
"""Returns 'text' as lowercase."""
|
||||||
return text.lower()
|
return text.lower()
|
||||||
|
|
||||||
|
|
||||||
lowerrep.command = "lower"
|
lowerrep.command = "lower"
|
||||||
|
|
||||||
|
|
||||||
def scramblerep(text):
|
def scramblerep(text: str):
|
||||||
|
"""Returns 'text' randomly scrambled."""
|
||||||
return "".join(random.sample(text, len(text)))
|
return "".join(random.sample(text, len(text)))
|
||||||
|
|
||||||
|
|
||||||
scramblerep.command = "scramble"
|
scramblerep.command = "scramble"
|
||||||
|
|
||||||
|
|
||||||
def reverserep(text):
|
def reverserep(text: str):
|
||||||
|
"""Returns the reverse of 'text'."""
|
||||||
return text[::-1]
|
return text[::-1]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
"""This file contains an example of a gradient quirk function."""
|
||||||
import re
|
import re
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
|
||||||
def rainbow(text):
|
def rainbow(text):
|
||||||
|
@ -16,6 +18,9 @@ def rainbow(text):
|
||||||
2. Replace the hex colors in 'gradient' below with your own colors.
|
2. Replace the hex colors in 'gradient' below with your own colors.
|
||||||
3. Replace 'rainbow' above and below with something more fitting :3
|
3. Replace 'rainbow' above and below with something more fitting :3
|
||||||
|
|
||||||
|
You shouldn't enable the 'exclude links and smilies' option, this function
|
||||||
|
already does that!
|
||||||
|
|
||||||
There's lots of implementations of this that predate mine,
|
There's lots of implementations of this that predate mine,
|
||||||
see: https://paste.0xfc.de/?e60df5a155e93583#AmcgN9cRnCcBycmVMvw6KJ1YLKPXGbaSzZLbgAhoNCQD
|
see: https://paste.0xfc.de/?e60df5a155e93583#AmcgN9cRnCcBycmVMvw6KJ1YLKPXGbaSzZLbgAhoNCQD
|
||||||
^ There's more useful info here too :3c
|
^ There's more useful info here too :3c
|
||||||
|
@ -38,46 +43,45 @@ def rainbow(text):
|
||||||
|
|
||||||
# Set base distribution of colors over text,
|
# Set base distribution of colors over text,
|
||||||
# stored as list of lists.
|
# stored as list of lists.
|
||||||
color_and_position = []
|
colors_and_positions = []
|
||||||
for color in range(0, len(gradient)):
|
for color_pos, color in enumerate(gradient):
|
||||||
ratio = len(text) / len(gradient) # To account for text length.
|
ratio = len(text) / len(gradient) # To account for text length.
|
||||||
color_and_position.append([gradient[color], round(color * ratio)])
|
colors_and_positions.append([color, round(color_pos * ratio)])
|
||||||
|
|
||||||
# Iterate through match object representing all links/smilies in text,
|
# Iterate through match objects representing all links/smilies in text,
|
||||||
# if a color tag is going to be placed within it,
|
# if a color tag is going to be placed within it,
|
||||||
# move its position to after the link.
|
# move its position to after the link/smilies/thingy.
|
||||||
for match in re.finditer(_urlre, text):
|
match_chain = itertools.chain(
|
||||||
for cp in color_and_position:
|
re.finditer(_urlre, text),
|
||||||
if (cp[1] >= match.start()) and (cp[1] <= match.end()): # cp[1] is pos
|
re.finditer(_smilere, text),
|
||||||
cp[1] = match.end() + 1 # Move to 1 character after link.
|
re.finditer(_memore, text),
|
||||||
for match in re.finditer(_smilere, text):
|
re.finditer(_handlere, text),
|
||||||
for cp in color_and_position:
|
)
|
||||||
if (cp[1] >= match.start()) and (cp[1] <= match.end()):
|
for match in match_chain:
|
||||||
cp[1] = match.end() + 1
|
for color_and_position in colors_and_positions:
|
||||||
for match in re.finditer(_memore, text):
|
# color_and_position[1] is pos
|
||||||
for cp in color_and_position:
|
if (
|
||||||
if (cp[1] >= match.start()) and (cp[1] <= match.end()):
|
color_and_position[1] >= match.start()
|
||||||
cp[1] = match.end() + 1
|
and color_and_position[1] <= match.end()
|
||||||
for match in re.finditer(_handlere, text):
|
):
|
||||||
for cp in color_and_position:
|
# Move to 1 character after link.
|
||||||
if (cp[1] >= match.start()) and (cp[1] <= match.end()):
|
color_and_position[1] = match.end() + 1
|
||||||
cp[1] = match.end() + 1
|
|
||||||
|
|
||||||
# Iterate through characters in text and write them to the output,
|
# Iterate through characters in text and write them to the output,
|
||||||
# if a color tag should be placed, add it before the character.
|
# if a color tag should be placed, add it before the character.
|
||||||
output = ""
|
output = ""
|
||||||
for char in range(0, len(text)):
|
for char_pos, char in enumerate(text):
|
||||||
# Add color if at position.
|
# Add color if at position.
|
||||||
for cp in color_and_position:
|
for color_and_position in colors_and_positions:
|
||||||
# cp[0] is color
|
# color_and_position[0] is color
|
||||||
# cp[1] is pos
|
# color_and_position[1] is pos
|
||||||
if char == cp[1]:
|
if char_pos == color_and_position[1]:
|
||||||
# Add closing bracket for previous color.
|
# Add closing bracket for previous color.
|
||||||
output += "</c>"
|
output += "</c>"
|
||||||
# Add color
|
# Add color
|
||||||
output += "<c=%s>" % cp[0]
|
output += f"<c={color_and_position[0]}>"
|
||||||
# Add character.
|
# Add character.
|
||||||
output += text[char]
|
output += char
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
@ -155,6 +159,5 @@ smiledict = {
|
||||||
# Regular expression templates for detecting links/smilies.
|
# Regular expression templates for detecting links/smilies.
|
||||||
_smilere = re.compile("|".join(list(smiledict.keys())))
|
_smilere = re.compile("|".join(list(smiledict.keys())))
|
||||||
_urlre = re.compile(r"(?i)(?:^|(?<=\s))(?:(?:https?|ftp)://|magnet:)[^\s]+")
|
_urlre = re.compile(r"(?i)(?:^|(?<=\s))(?:(?:https?|ftp)://|magnet:)[^\s]+")
|
||||||
# _url2re = re.compile(r"(?i)(?<!//)\bwww\.[^\s]+?\.")
|
|
||||||
_memore = re.compile(r"(\s|^)(#[A-Za-z0-9_]+)")
|
_memore = re.compile(r"(\s|^)(#[A-Za-z0-9_]+)")
|
||||||
_handlere = re.compile(r"(\s|^)(@[A-Za-z0-9_]+)")
|
_handlere = re.compile(r"(\s|^)(@[A-Za-z0-9_]+)")
|
||||||
|
|
Loading…
Reference in a new issue