forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown-to-telegram.py
executable file
·100 lines (86 loc) · 3.73 KB
/
markdown-to-telegram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Convert Markdown to Telegram Format
# @raycast.mode silent
# Optional parameters:
# @raycast.icon 🔄
# @raycast.packageName Conversions
# @raycast.description Convert Markdown formatting to Telegram format, excluding processing inside code blocks or quotes
# Documentation:
# @raycast.author Maxim Borzov
# @raycast.authorURL https://github.com/borzov
import re
import subprocess
# Set environment variables and encoding
env_vars = {'LANG': 'en_US.UTF-8'}
encoding = 'utf-8'
def paste():
"""Read text from the clipboard."""
return subprocess.check_output('pbpaste', env=env_vars).decode(encoding)
def copy(text):
"""Write text to the clipboard."""
process = subprocess.Popen('pbcopy', env=env_vars, stdin=subprocess.PIPE)
process.communicate(text.encode(encoding))
def convert_markdown(text):
"""Convert Markdown formatting to Telegram format."""
lines = text.split('\n')
converted_lines = []
in_code_block = False
for line in lines:
# Check if the line is a code block delimiter
if line.startswith('```'):
in_code_block = not in_code_block
converted_lines.append(line)
continue
# Skip quotes and only format if not in a code block
if not in_code_block and not line.startswith('>'):
# Format headers with emojis and bold text
if line.startswith('# '):
line = f"⚫️ **{line[2:].strip()}**\n"
elif line.startswith('## '):
line = f"◾️ **{line[3:].strip()}**\n"
elif line.startswith('### '):
line = f"▪️ **{line[4:].strip()}**\n"
elif line.startswith('#### '):
line = f"🔹 **{line[5:].strip()}**\n"
elif line.startswith('##### '):
line = f"📌 **{line[6:].strip()}**\n"
elif line.startswith('###### '):
line = f"🔰 **{line[7:].strip()}**\n"
else:
# Format bold text
line = re.sub(r'(?<!\\)\*{2}(.*?)\*{2}', r'**\1**', line)
line = re.sub(r'(?<!\\)_{2}(.*?)_{2}', r'**\1**', line)
# Format italic text
line = re.sub(r'(?<!\\|\*)\*(?!\*)(.+?)(?<!\*)\*(?![\*_])', r'__\1__', line)
line = re.sub(r'(?<!\\|_)_(?!_)(.+?)(?<!_)_(?![\*_])', r'__\1__', line)
# Format strikethrough text
line = re.sub(r'(?<!\\)~{2}(.*?)~{2}', r'~~\1~~', line)
# Format spoilers
line = re.sub(r'(?<!\\)\|\|(.*?)\|\|', r'||\1||', line)
# Format underline text
line = re.sub(r'(?<!\\)-{2}(.*?)-{2}', r'--\1--', line)
# Format links
line = re.sub(r'(?<!\\)\[(.*?)\]\((.*?)\)', r'[\1](\2)', line)
# Format inline code
line = re.sub(r'(?<!\\)`(.*?)`', r'`\1`', line)
converted_lines.append(line)
# Add empty lines between paragraphs
result = []
for i in range(len(converted_lines)):
result.append(converted_lines[i])
if i < len(converted_lines) - 1:
cur = converted_lines[i].strip()
next = converted_lines[i+1].strip()
if cur and next and not cur.startswith(('*', '-', '```', '|', '>')) and not next.startswith(('*', '-', '```', '|', '>')):
result.append('')
return '\n'.join(result)
if __name__ == "__main__":
try:
markdown_text = paste()
converted_text = convert_markdown(markdown_text)
copy(converted_text)
print("✅ Markdown converted and copied to clipboard")
except Exception as e:
print(f"❌ Error during conversion: {str(e)}")