Skip to content

Instantly share code, notes, and snippets.

@yunruse
Last active February 4, 2023 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yunruse/1eca1d3f5b7a3bc372803c9dc9c9ed50 to your computer and use it in GitHub Desktop.
Save yunruse/1eca1d3f5b7a3bc372803c9dc9c9ed50 to your computer and use it in GitHub Desktop.
Discord bubble wrap
#! /usr/bin/env python3
# I hope the example input is fairly obvious
# but this is a quick tool to randomly pick emoji from a palette
# and apply it onto a grid.
# Input Discord-markdown in the format of example.txt to stdin.
# Add `spoilers` to argv if you wish to make a cute find-the-emoji puzzle!
# For example:
# Transform clipboard
# pbcopy | bubblewrap spoilers | pbpaste
# From file
# cat puzzle.txt | bubblewrap > puzzle_2.txt
from re import findall
from random import choice
from sys import argv, stdin
def get_grid(lines: list[str]):
while (line := lines.pop(0)) != '```':
yield line.split()
def get_pattern(lines: list[str]):
pattern = {}
for line in lines:
p, emoji_list = line.split(maxsplit=1)
pattern[p] = findall(":([a-z_]+):", emoji_list)
return pattern
def transform(text: str, spoiler=True):
lines = text.splitlines()
assert lines.pop(0) == '```'
grid: list[list[str]] = list(get_grid(lines)) # consume some lines
patterns: dict[str, list[str]] = get_pattern(lines) # consume remaining
def transform_sigil(sigil):
emoji = choice(patterns[sigil])
return f'||:{emoji}:||' if spoiler else f':{emoji}:'
return "\n".join(
" ".join(map(transform_sigil, line))
for line in grid
)
if __name__ == '__main__':
print(transform(stdin.read(), 'spoilers' in argv))
```
S _ ~ ~ ~ ~ ~ _ S
_ ~ X X ~ X X ~ _
_ X ~ ~ X ~ ~ X ~
_ ~ X ~ ~ ~ X ~ _
_ _ ~ X ~ X ~ _ _
_ _ _ ~ X ~ _ _ _
S _ _ _ ~ _ _ _ S
```
_ :four_leaf_clover: :seedling::herb::four_leaf_clover::leaves:
~ :bee::baby_chick::hatched_chick::chipmunk::hedgehog:
X :strawberry:
S :potted_plant::tanabata_tree::bamboo:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment