Skip to content

Instantly share code, notes, and snippets.

@patfinder
Last active February 2, 2025 02:35
Show Gist options
  • Select an option

  • Save patfinder/7fe39ea2fe9ec2545d8c4783af6dce24 to your computer and use it in GitHub Desktop.

Select an option

Save patfinder/7fe39ea2fe9ec2545d8c4783af6dce24 to your computer and use it in GitHub Desktop.
Simple script to convert markdown bold text to Unicode 'bold' characters. You can exend the script with other ranges (like bold AND itatic) (explore chars after and properly before 119808(A) .. 119859(z)). This can be used for formatting Facebook text.
import re
text = "Todayโ€™s world is driven by **technology** and **connectivity**, reshaping interactions and work. **Environmental issues** and social change call for urgent, **collective action** toward sustainability."
## Output: Todayโ€™s world is driven by ๐ญ๐ž๐œ๐ก๐ง๐จ๐ฅ๐จ๐ ๐ฒ and ๐œ๐จ๐ง๐ง๐ž๐œ๐ญ๐ข๐ฏ๐ข๐ญ๐ฒ, reshaping interactions and work. ๐„๐ง๐ฏ๐ข๐ซ๐จ๐ง๐ฆ๐ž๐ง๐ญ๐š๐ฅ ๐ข๐ฌ๐ฌ๐ฎ๐ž๐ฌ and social change call for urgent, ๐œ๐จ๐ฅ๐ฅ๐ž๐œ๐ญ๐ข๐ฏ๐ž ๐š๐œ๐ญ๐ข๐จ๐ง toward sustainability.
# text = "_000 **A** 111 **b** 222"
def convert(text):
# Split input to couple of 'normal' and 'bold'
# Process each couple, then continue with remaining text
rex = r'(.*?)\*\*(.*?)\*\*'
result = ''
while True:
# Find couple
match = re.match(rex, text)
# No more match, combine remaining with previous result.
if not match:
result += text
break
# 2. For each bold range, convert them to 'bold' format
groups = match.groups()
normal = groups[0]
bold = groups[1]
# Transformed of `bold`
t_bold = ''
for ch in bold:
# Bold A..Z <--> 119808..119833
if ch >= 'A' and ch <= 'Z':
ch = chr(ord(ch) - ord('A') + 119808)
# Bold a..z <--> 119834..119859
elif ch >= 'a' and ch <= 'z':
ch = chr(ord(ch) - ord('a') + 119834)
t_bold += ch
# 3. Combine ranges back as original string.
text = text[match.span()[1]:]
result += (normal + t_bold)
return result
converted= convert(text)
print(converted)
## Todayโ€™s world is driven by ๐ญ๐ž๐œ๐ก๐ง๐จ๐ฅ๐จ๐ ๐ฒ and ๐œ๐จ๐ง๐ง๐ž๐œ๐ญ๐ข๐ฏ๐ข๐ญ๐ฒ, reshaping interactions and work. ๐„๐ง๐ฏ๐ข๐ซ๐จ๐ง๐ฆ๐ž๐ง๐ญ๐š๐ฅ ๐ข๐ฌ๐ฌ๐ฎ๐ž๐ฌ and social change call for urgent, ๐œ๐จ๐ฅ๐ฅ๐ž๐œ๐ญ๐ข๐ฏ๐ž ๐š๐œ๐ญ๐ข๐จ๐ง toward sustainability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment