mdreader / app.py
RP-Azul's picture
Update app.py
da9117a verified
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 == "Boxing Method":
def parse_markdown_into_boxes(text):
# Remove ```markdown block if it exists
cleaned = text.strip()
if cleaned.startswith("```markdown"):
cleaned = cleaned[len("```markdown"):].strip()
if cleaned.endswith("```"):
cleaned = cleaned[:-3].strip()
# Split based on Markdown headers (## Box or ## Summary)
pattern = r'(^## .*$)'
parts = re.split(pattern, cleaned, 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("πŸ“¦ Boxing Method Output")
all_sections = parse_markdown_into_boxes(markdown_text)
boxes = [s for s in all_sections if not s["header"].lower().startswith("## summary")]
summary = next((s for s in all_sections if s["header"].lower().startswith("## summary")), None)
# Display boxes in two columns
col1, col2 = st.columns(2)
for i, section in enumerate(boxes):
target_col = col1 if i % 2 == 0 else col2
with target_col.container(border=True):
st.markdown(f"#### {section['header']}")
st.markdown(section["content"])
# Display summary below full width
if summary:
st.markdown("---")
with st.container(border=True):
st.markdown(f"### {summary['header']}")
st.markdown(summary['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")
# Remove ```markdown code fences if present
cleaned_markdown = markdown_text.strip()
if cleaned_markdown.startswith("```markdown"):
cleaned_markdown = cleaned_markdown[len("```markdown"):].strip()
if cleaned_markdown.endswith("```"):
cleaned_markdown = cleaned_markdown[:-3].strip()
# Split into sections using '## ' headers
sections = cleaned_markdown.split("## ")
for raw_section in sections:
if not raw_section.strip():
continue # skip empty sections
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:
stripped = line.lstrip()
indent_level = (len(line) - len(stripped)) // 2 # assume 2 spaces per indent
if stripped.startswith("- "):
st.markdown(f"{'&nbsp;' * 4 * indent_level}{stripped}")
else:
st.markdown(stripped)
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()}")