mdreader / app.py
RP-Azul's picture
Update app.py
e210c0a verified
raw
history blame
4.52 kB
import streamlit as st
import re
st.set_page_config(page_title="πŸ“¦ Markdown Formatter", layout="centered")
st.title("πŸ“„ Markdown Formatter App")
method = st.selectbox("Choose a formatting method:", ["Select a method", "Boxing Method","Outline Method", "Raw Markdown","Flashcard", "Cornell Method"])
uploaded_file = st.file_uploader("Upload a Markdown (.md) file", type=["md"])
if uploaded_file and method != "Select a method":
markdown_text = uploaded_file.read().decode("utf-8")
if method == "Raw Markdown":
st.markdown(markdown_text)
elif method == "Boxing Method":
def parse_markdown_into_boxes(text):
pattern = r'(^#+ .*$)'
parts = re.split(pattern, text, flags=re.MULTILINE)
sections = []
for i in range(1, len(parts), 2):
header = parts[i].strip()
content = parts[i + 1].strip() if i + 1 < len(parts) else ""
sections.append({"header": header, "content": content})
return sections
st.subheader("πŸ“¦ Formatted Output")
for section in parse_markdown_into_boxes(markdown_text):
with st.container(border=True):
if section["header"]:
st.markdown(f"#### {section['header']}")
st.markdown(section["content"])
# elif method == "Outline Method":
# st.subheader("πŸ“ Outline Method Output")
# sections = markdown_text.strip().split("\n\n")
# for section in sections:
# lines = section.strip().split("\n")
# if lines:
# title = lines[0]
# content = "\n".join(lines[1:]) if len(lines) > 1 else ""
# with st.container(border=True):
# st.markdown(f"**{title}**")
# st.markdown(content)
elif method == "Outline Method":
st.subheader("πŸ“ Outline Method Output")
# Split into sections by H2 headings (##)
sections = markdown_text.strip().split("## ")
for raw_section in sections:
if not raw_section.strip():
continue # Skip empty sections
# Extract title and bullet content
lines = raw_section.strip().splitlines()
title_line = lines[0] if lines else "Untitled Section"
content_lines = lines[1:]
with st.container(border=True):
st.markdown(f"### {title_line.strip()}")
for line in content_lines:
# Indentation-based visual enhancement
if line.strip().startswith("- "):
st.markdown(f"{line}")
elif line.strip().startswith(" - ") or line.strip().startswith(" - "):
st.markdown(f"&nbsp;&nbsp;&nbsp;&nbsp;{line}")
else:
st.markdown(line)
elif method == "Cornell Method":
st.subheader("πŸ“š Cornell Notes Formatted")
def parse_markdown_sections(text):
sections = {}
current_section = None
for line in text.splitlines():
if line.strip().startswith("## "):
current_section = line.replace("##", "").strip()
sections[current_section] = ""
elif current_section:
sections[current_section] += line + "\n"
return sections
sections = parse_markdown_sections(markdown_text)
cue_column = sections.get("Cues", "Cues Column not found.")
notes_section = sections.get("Notes", "Notes Section not found.")
summary = sections.get("Summary", "Summary not found.")
col1, col2 = st.columns([1, 2])
with col1:
st.markdown("### Cue Column")
st.markdown(cue_column)
with col2:
st.markdown("### Notes Section")
st.markdown(notes_section)
st.markdown("---")
st.markdown("### Summary")
st.markdown(summary)
elif method == "Flashcard":
st.subheader("πŸ“ Flashcards")
# Extract Q&A pairs using regex
pattern = r"- \*\*Q: (.*?)\*\*.*?\*\*A:\*\* (.*?)\n"
qa_pairs = re.findall(pattern, markdown_text, re.DOTALL)
# Display in expandable flashcards
for i, (question, answer) in enumerate(qa_pairs, 1):
with st.expander(f"Q{i}: {question.strip()}"):
st.markdown(f"**Answer:** {answer.strip()}")