Procházet zdrojové kódy

Narrow down escaping MarkDown

Foppe Hemminga před 6 roky
rodič
revize
2f6169d293
1 změnil soubory, kde provedl 8 přidání a 1 odebrání
  1. 8 1
      model.py

+ 8 - 1
model.py

@@ -42,7 +42,14 @@ def create_links(news_string):
 
 
 def escape_markdown(news_string):
-    news_string = news_string.translate(str.maketrans({'_': '\_', '*': '\*', '~': '\~'}))
+    regex_underscore = re.compile(r'_.+_')
+    regex_star = re.compile(r'\*.+\*')
+    regex_tilde = re.compile(r'~.+~')
+    boolean = re.search(regex_underscore, news_string) or \
+        re.search(regex_star, news_string) or \
+        re.search(regex_tilde, news_string)
+    if boolean:
+        news_string = news_string.translate(str.maketrans({'_': '\_', '*': '\*', '~': '\~'}))
     return news_string