Skip to content

Commit

Permalink
Wrap titles in Japanese quotes if appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
norinorin committed Oct 24, 2024
1 parent 269c302 commit acd1694
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
40 changes: 40 additions & 0 deletions anime_rpc/formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
ranges = [
{"from": ord("\u3300"), "to": ord("\u33ff")}, # compatibility ideographs
{"from": ord("\ufe30"), "to": ord("\ufe4f")}, # compatibility ideographs
{"from": ord("\uf900"), "to": ord("\ufaff")}, # compatibility ideographs
{"from": ord("\U0002F800"), "to": ord("\U0002fa1f")}, # compatibility ideographs
{"from": ord("\u3040"), "to": ord("\u309f")}, # Japanese Hiragana
{"from": ord("\u30a0"), "to": ord("\u30ff")}, # Japanese Katakana
{"from": ord("\u2e80"), "to": ord("\u2eff")}, # cjk radicals supplement
{"from": ord("\u4e00"), "to": ord("\u9fff")},
{"from": ord("\u3400"), "to": ord("\u4dbf")},
{"from": ord("\U00020000"), "to": ord("\U0002a6df")},
{"from": ord("\U0002a700"), "to": ord("\U0002b73f")},
{"from": ord("\U0002b740"), "to": ord("\U0002b81f")},
{"from": ord("\U0002b820"), "to": ord("\U0002ceaf")}, # included as of Unicode 8.0
]


def _is_char_cjk(char: str) -> bool:
return any([range["from"] <= ord(char) <= range["to"] for range in ranges])


def _contains_cjk(text: str) -> bool:
return any([_is_char_cjk(i) for i in text])


def _quote_cjk(text: str) -> str:
if text.startswith(("「", "『")) or text.endswith(("」", "』")):
return text

return f"「{text}」"


def quote(text: str) -> str:
if _contains_cjk(text):
return _quote_cjk(text)

if text.startswith('"') or text.endswith('"'):
return text

return f'"{text}"'
7 changes: 1 addition & 6 deletions anime_rpc/presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from anime_rpc.config import Config, APPLICATION_ID
from anime_rpc.mpc import Vars, WatchingState
from anime_rpc.formatting import quote

from discordrpc import RPC # type: ignore

Expand All @@ -27,12 +28,6 @@ def get_ep_title(
return int(ep), title.strip()


def quote(text: str) -> str:
if text.startswith('"') or text.endswith('"'):
return text
return f'"{text}"'


def _maybe_strip_leading_zeros(timestamp: str) -> str:
parts = timestamp.split(":")
if not int(parts[0]):
Expand Down

0 comments on commit acd1694

Please sign in to comment.