-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
84 lines (77 loc) · 3.17 KB
/
app.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
import re
import streamlit as st
import sqlparse
import json
import textwrap
import pygments
from pygments.lexers import get_lexer_by_name
from pygments.formatters import TerminalFormatter
from yapf.yapflib import yapf_api
import autopep8
from bs4 import BeautifulSoup
st.title("🩺 Code Formatter")
# Add a message to the app to indicate SQL, JSON, HTML, and JavaScript compatibility
st.write("SQL, JSON and JavaScript compatible")
code_input = st.text_area("Enter your code here", height=400)
# Check if code is entered
if not code_input:
st.stop()
# Detect the language of the code
sql_keywords_regex = r'^(SELECT|WITH|INSERT|UPDATE|DELETE|CREATE|DROP|ALTER)'
match = re.search(sql_keywords_regex, code_input.strip(), re.IGNORECASE)
if match:
language = "SQL"
else:
try:
json.loads(code_input)
language = "JSON"
except ValueError:
try:
soup = BeautifulSoup(code_input, 'html.parser')
script_tags = soup.find_all('script')
if len(script_tags) == 1:
script_code = script_tags[0].string.strip()
if re.search(r'(\bconst\b|\blet\b|\bvar\b|\bfunction\b|\bif\b|\belse\b|\bfor\b|\bwhile\b|\bswitch\b)', script_code):
language = "JavaScript"
code_input = script_code
else:
language = "HTML"
else:
formatted_scripts = []
for script in script_tags:
script_code = script.string.strip()
if re.search(r'(\bconst\b|\blet\b|\bvar\b|\bfunction\b|\bif\b|\belse\b|\bfor\b|\bwhile\b|\bswitch\b)', script_code):
formatted_scripts.append(autopep8.fix_code(script_code, options={'max_line_length': 120}).strip())
else:
formatted_scripts.append(script_code)
formatted_code = str(soup).replace(str(script_tags[0]), formatted_scripts[0])
for i in range(1, len(script_tags)):
formatted_code = formatted_code.replace(str(script_tags[i]), formatted_scripts[i])
language = "HTML"
except:
st.error("Unable to detect code language")
st.stop()
# Format the code using the appropriate formatter
if language == "SQL":
formatted_code = sqlparse.format(code_input, reindent=True)
formatted_code = textwrap.indent(formatted_code, " " * 4)
elif language == "JSON":
try:
parsed_json = json.loads(code_input)
formatted_code = json.dumps(parsed_json, indent=4, sort_keys=True)
except ValueError:
st.error("Invalid JSON syntax")
st.stop()
elif language == "HTML":
soup = BeautifulSoup(formatted_code, 'html.parser')
formatted_code = soup.prettify()
elif language == "JavaScript":
try:
formatted_code = autopep8.fix_code(code_input, options={'max_line_length': 120})
formatted_code = formatted_code.strip() # Remove any leading/trailing whitespaces
except:
st.error("Unable to format JavaScript code")
st.stop()
# Display the formatted code and detected language
st.subheader(f"Formatted {language} code:")
st.code(formatted_code, language=language)