Skip to content

Commit

Permalink
Preserve trailing underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Mar 17, 2022
1 parent 9b08e69 commit 20b4f5c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Reserved URL characters can be included using escape patterns:

For example, <https://api.memegen.link/images/ugandanknuck/~hspecial_characters~q/underscore__-dash--.png> produces:

![Escaped Characters](https://api.memegen.link/images/ugandanknuck/~hspecial_characters~q/underscore___dash--.png?width=800&token=imyyyyk8qex6m2jz8bar)
![Escaped Characters](https://api.memegen.link/images/ugandanknuck/~hspecial_characters~q/underscore__-dash--.png?width=800&token=imyyyyk8qex6m2jz8bar)

All of the `POST` endpoints will return image URLs with special characters replaced with these alternatives.

Expand Down
1 change: 1 addition & 0 deletions app/tests/test_utils_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
(['That\'s not how "this" works'], "That's_not_how_''this''_works"),
(["git commit --no-verify"], "git_commit_----no--verify"),
(["_username likes _charname"], "__username_likes___charname"),
(["underscore_ dash-"], "underscore__-dash--"),
]


Expand Down
57 changes: 34 additions & 23 deletions app/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,7 @@ def encode(lines: list[str]) -> str:
if line == "/":
encoded_lines.append("_")
elif line:
encoded = unquote(line)
for before, after in [
("_", "__"),
("-", "--"),
(" ", "_"),
("?", "~q"),
("%", "~p"),
("#", "~h"),
('"', "''"),
("/", "~s"),
("\\", "~b"),
("\n", "~n"),
("&", "~a"),
("<", "~l"),
(">", "~g"),
("‘", "'"),
("’", "'"),
("“", '"'),
("”", '"'),
("–", "-"),
]:
encoded = encoded.replace(before, after)
encoded_lines.append(encoded)
encoded_lines.append(_encode(line))
else:
encoded_lines.append("_")

Expand All @@ -41,6 +19,39 @@ def encode(lines: list[str]) -> str:
return slug or "_"


def _encode(line):
has_trailing_under = "_ " in line

encoded = unquote(line)

for before, after in [
("_", "__"),
("-", "--"),
(" ", "_"),
("?", "~q"),
("%", "~p"),
("#", "~h"),
('"', "''"),
("/", "~s"),
("\\", "~b"),
("\n", "~n"),
("&", "~a"),
("<", "~l"),
(">", "~g"),
("‘", "'"),
("’", "'"),
("“", '"'),
("”", '"'),
("–", "-"),
]:
encoded = encoded.replace(before, after)

if has_trailing_under:
encoded = encoded.replace("___", "__-")

return encoded


def decode(slug: str) -> list[str]:
has_dash = "_----" in slug
has_arrow = "_--~g" in slug
Expand Down

0 comments on commit 20b4f5c

Please sign in to comment.