Spaces:
Sleeping
Sleeping
Upload utils.py
Browse files- app_modules/utils.py +37 -0
app_modules/utils.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def convert_to_markdown(text):
|
| 5 |
+
text = text.replace("$", "$")
|
| 6 |
+
|
| 7 |
+
def replace_leading_tabs_and_spaces(line):
|
| 8 |
+
new_line = []
|
| 9 |
+
|
| 10 |
+
for char in line:
|
| 11 |
+
if char == "\t":
|
| 12 |
+
new_line.append("	")
|
| 13 |
+
elif char == " ":
|
| 14 |
+
new_line.append(" ")
|
| 15 |
+
else:
|
| 16 |
+
break
|
| 17 |
+
return "".join(new_line) + line[len(new_line):]
|
| 18 |
+
|
| 19 |
+
markdown_text = ""
|
| 20 |
+
lines = text.split("\n")
|
| 21 |
+
in_code_block = False
|
| 22 |
+
|
| 23 |
+
for line in lines:
|
| 24 |
+
if in_code_block is False and line.startswith("```"):
|
| 25 |
+
in_code_block = True
|
| 26 |
+
markdown_text += f"{line}\n"
|
| 27 |
+
elif in_code_block is True and line.startswith("```"):
|
| 28 |
+
in_code_block = False
|
| 29 |
+
markdown_text += f"{line}\n"
|
| 30 |
+
elif in_code_block:
|
| 31 |
+
markdown_text += f"{line}\n"
|
| 32 |
+
else:
|
| 33 |
+
line = replace_leading_tabs_and_spaces(line)
|
| 34 |
+
line = re.sub(r"^(#)", r"\\\1", line)
|
| 35 |
+
markdown_text += f"{line} \n"
|
| 36 |
+
|
| 37 |
+
return markdown_text
|