Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
st.set_page_config(page_title="📦 Markdown Formatter", layout="centered")
|
| 5 |
+
st.title("📄 Markdown Formatter App")
|
| 6 |
+
|
| 7 |
+
method = st.selectbox("Choose a formatting method:", ["Select a method", "Boxing Method", "Raw Markdown"])
|
| 8 |
+
|
| 9 |
+
uploaded_file = st.file_uploader("Upload a Markdown (.md) file", type=["md"])
|
| 10 |
+
|
| 11 |
+
if uploaded_file and method != "Select a method":
|
| 12 |
+
markdown_text = uploaded_file.read().decode("utf-8")
|
| 13 |
+
|
| 14 |
+
if method == "Raw Markdown":
|
| 15 |
+
st.markdown(markdown_text)
|
| 16 |
+
|
| 17 |
+
elif method == "Boxing Method":
|
| 18 |
+
# Parse markdown into boxes based on headers
|
| 19 |
+
def parse_markdown_into_boxes(text):
|
| 20 |
+
pattern = r'(^#+ .*$)'
|
| 21 |
+
parts = re.split(pattern, text, flags=re.MULTILINE)
|
| 22 |
+
sections = []
|
| 23 |
+
current_section = {"header": None, "content": ""}
|
| 24 |
+
for part in parts:
|
| 25 |
+
if re.match(r'^#+ ', part):
|
| 26 |
+
if current_section["header"] or current_section["content"]:
|
| 27 |
+
sections.append(current_section)
|
| 28 |
+
current_section = {"header": None, "content": ""}
|
| 29 |
+
current_section["header"] = part.strip()
|
| 30 |
+
else:
|
| 31 |
+
current_section["content"] += part.strip() + "\n"
|
| 32 |
+
if current_section["header"] or current_section["content"]:
|
| 33 |
+
sections.append(current_section)
|
| 34 |
+
return sections
|
| 35 |
+
|
| 36 |
+
st.subheader("📦 Formatted Output")
|
| 37 |
+
for section in parse_markdown_into_boxes(markdown_text):
|
| 38 |
+
with st.container(border=True):
|
| 39 |
+
if section["header"]:
|
| 40 |
+
st.markdown(f"#### {section['header']}")
|
| 41 |
+
st.markdown(section["content"])
|